TestCloner.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2016 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package jme3test.app;
  33. import java.util.*;
  34. import com.jme3.util.clone.*;
  35. /**
  36. *
  37. *
  38. * @author Paul Speed
  39. */
  40. public class TestCloner {
  41. public static void main( String... args ) {
  42. System.out.println("Clone test:");
  43. Cloner cloner = new Cloner();
  44. RegularObject ro = new RegularObject(42);
  45. System.out.println("Regular Object:" + ro);
  46. RegularObject roCloneLegacy = ro.clone();
  47. System.out.println("Regular Object Clone:" + roCloneLegacy);
  48. RegularObject roClone = cloner.clone(ro);
  49. System.out.println("cloner: Regular Object Clone:" + roClone);
  50. System.out.println("------------------------------------");
  51. System.out.println();
  52. cloner = new Cloner();
  53. RegularSubclass rsc = new RegularSubclass(69, "test");
  54. System.out.println("Regular subclass:" + rsc);
  55. RegularSubclass rscCloneLegacy = (RegularSubclass)rsc.clone();
  56. System.out.println("Regular subclass Clone:" + rscCloneLegacy);
  57. RegularSubclass rscClone = cloner.clone(rsc);
  58. System.out.println("cloner: Regular subclass Clone:" + rscClone);
  59. System.out.println("------------------------------------");
  60. System.out.println();
  61. cloner = new Cloner();
  62. Parent parent = new Parent("Foo", 34);
  63. System.out.println("Parent:" + parent);
  64. Parent parentCloneLegacy = parent.clone();
  65. System.out.println("Parent Clone:" + parentCloneLegacy);
  66. Parent parentClone = cloner.clone(parent);
  67. System.out.println("cloner: Parent Clone:" + parentClone);
  68. System.out.println("------------------------------------");
  69. System.out.println();
  70. cloner = new Cloner();
  71. GraphNode root = new GraphNode("root");
  72. GraphNode child1 = root.addLink("child1");
  73. GraphNode child2 = root.addLink("child2");
  74. GraphNode shared = child1.addLink("shared");
  75. child2.addLink(shared);
  76. // Add a circular reference to get fancy
  77. shared.addLink(root);
  78. System.out.println("Simple graph:");
  79. root.dump(" ");
  80. GraphNode rootClone = cloner.clone(root);
  81. System.out.println("clone:");
  82. rootClone.dump(" ");
  83. System.out.println("original:");
  84. root.dump(" ");
  85. GraphNode reclone = Cloner.deepClone(root);
  86. System.out.println("reclone:");
  87. reclone.dump(" ");
  88. System.out.println("------------------------------------");
  89. System.out.println();
  90. cloner = new Cloner();
  91. ArrayHolder arrays = new ArrayHolder(5, 3, 7, 3, 7, 2, 1, 4);
  92. System.out.println("Array holder:" + arrays);
  93. ArrayHolder arraysClone = cloner.clone(arrays);
  94. System.out.println("Array holder clone:" + arraysClone);
  95. }
  96. public static class RegularObject implements Cloneable {
  97. protected int i;
  98. public RegularObject( int i ) {
  99. this.i = i;
  100. }
  101. public RegularObject clone() {
  102. try {
  103. return (RegularObject)super.clone();
  104. } catch( CloneNotSupportedException e ) {
  105. throw new RuntimeException(e);
  106. }
  107. }
  108. public String toString() {
  109. return getClass().getSimpleName() + "@" + System.identityHashCode(this)
  110. + "[i=" + i + "]";
  111. }
  112. }
  113. public static class RegularSubclass extends RegularObject {
  114. protected String name;
  115. public RegularSubclass( int i, String name ) {
  116. super(i);
  117. this.name = name;
  118. }
  119. public String toString() {
  120. return getClass().getSimpleName() + "@" + System.identityHashCode(this)
  121. + "[i=" + i + ", name=" + name + "]";
  122. }
  123. }
  124. public static class Parent implements Cloneable, JmeCloneable {
  125. private RegularObject ro;
  126. private RegularSubclass rsc;
  127. public Parent( String name, int age ) {
  128. this.ro = new RegularObject(age);
  129. this.rsc = new RegularSubclass(age, name);
  130. }
  131. public Parent clone() {
  132. try {
  133. return (Parent)super.clone();
  134. } catch( CloneNotSupportedException e ) {
  135. throw new RuntimeException(e);
  136. }
  137. }
  138. public Parent jmeClone() {
  139. // Ok to delegate to clone() in this case because no deep
  140. // cloning is done there.
  141. return clone();
  142. }
  143. public void cloneFields( Cloner cloner, Object original ) {
  144. this.ro = cloner.clone(ro);
  145. this.rsc = cloner.clone(rsc);
  146. }
  147. public String toString() {
  148. return getClass().getSimpleName() + "@" + System.identityHashCode(this)
  149. + "[ro=" + ro + ", rsc=" + rsc + "]";
  150. }
  151. }
  152. public static class GraphNode implements Cloneable, JmeCloneable {
  153. private String name;
  154. private List<GraphNode> links = new ArrayList<>();
  155. public GraphNode( String name ) {
  156. this.name = name;
  157. }
  158. public void dump( String indent ) {
  159. dump(indent, new HashSet<GraphNode>());
  160. }
  161. private void dump( String indent, Set<GraphNode> visited ) {
  162. if( visited.contains(this) ) {
  163. // already been here
  164. System.out.println(indent + this + " ** circular.");
  165. return;
  166. }
  167. System.out.println(indent + this);
  168. visited.add(this);
  169. for( GraphNode n : links ) {
  170. n.dump(indent + " ", visited);
  171. }
  172. visited.remove(this);
  173. }
  174. public GraphNode addLink( String name ) {
  175. GraphNode node = new GraphNode(name);
  176. links.add(node);
  177. return node;
  178. }
  179. public GraphNode addLink( GraphNode node ) {
  180. links.add(node);
  181. return node;
  182. }
  183. public List<GraphNode> getLinks() {
  184. return links;
  185. }
  186. public GraphNode jmeClone() {
  187. try {
  188. return (GraphNode)super.clone();
  189. } catch( CloneNotSupportedException e ) {
  190. throw new RuntimeException(e);
  191. }
  192. }
  193. public void cloneFields( Cloner cloner, Object original ) {
  194. this.links = cloner.clone(links);
  195. }
  196. public String toString() {
  197. return getClass().getSimpleName() + "@" + System.identityHashCode(this)
  198. + "[name=" + name + "]";
  199. }
  200. }
  201. public static class ArrayHolder implements JmeCloneable {
  202. private int[] intArray;
  203. private int[][] intArray2D;
  204. private Object[] objects;
  205. private RegularObject[] regularObjects;
  206. private String[] strings;
  207. public ArrayHolder( int... values ) {
  208. this.intArray = values;
  209. this.intArray2D = new int[values.length][2];
  210. for( int i = 0; i < values.length; i++ ) {
  211. intArray2D[i][0] = values[i] + 1;
  212. intArray2D[i][1] = values[i] * 2;
  213. }
  214. this.objects = new Object[values.length];
  215. this.regularObjects = new RegularObject[values.length];
  216. this.strings = new String[values.length];
  217. for( int i = 0; i < values.length; i++ ) {
  218. objects[i] = values[i];
  219. regularObjects[i] = new RegularObject(values[i]);
  220. strings[i] = String.valueOf(values[i]);
  221. }
  222. }
  223. public ArrayHolder jmeClone() {
  224. try {
  225. return (ArrayHolder)super.clone();
  226. } catch( CloneNotSupportedException e ) {
  227. throw new RuntimeException(e);
  228. }
  229. }
  230. public void cloneFields( Cloner cloner, Object original ) {
  231. intArray = cloner.clone(intArray);
  232. intArray2D = cloner.clone(intArray2D);
  233. // Boxed types are not cloneable so this will fail
  234. //objects = cloner.clone(objects);
  235. regularObjects = cloner.clone(regularObjects);
  236. // Strings are also not cloneable
  237. //strings = cloner.clone(strings);
  238. }
  239. public String toString() {
  240. StringBuilder sb = new StringBuilder();
  241. sb.append("intArray=" + intArray);
  242. for( int i = 0; i < intArray.length; i++ ) {
  243. if( i == 0 ) {
  244. sb.append("[");
  245. } else {
  246. sb.append(", ");
  247. }
  248. sb.append(intArray[i]);
  249. }
  250. sb.append("], ");
  251. sb.append("intArray2D=" + intArray2D);
  252. for( int i = 0; i < intArray2D.length; i++ ) {
  253. if( i == 0 ) {
  254. sb.append("[");
  255. } else {
  256. sb.append(", ");
  257. }
  258. sb.append("intArray2D[" + i + "]=" + intArray2D[i]);
  259. for( int j = 0; j < 2; j++ ) {
  260. if( j == 0 ) {
  261. sb.append("[");
  262. } else {
  263. sb.append(", ");
  264. }
  265. sb.append(intArray2D[i][j]);
  266. }
  267. sb.append("], ");
  268. }
  269. sb.append("], ");
  270. sb.append("objectArray=" + objects);
  271. for( int i = 0; i < objects.length; i++ ) {
  272. if( i == 0 ) {
  273. sb.append("[");
  274. } else {
  275. sb.append(", ");
  276. }
  277. sb.append(objects[i]);
  278. }
  279. sb.append("], ");
  280. sb.append("objectArray=" + regularObjects);
  281. for( int i = 0; i < regularObjects.length; i++ ) {
  282. if( i == 0 ) {
  283. sb.append("[");
  284. } else {
  285. sb.append(", ");
  286. }
  287. sb.append(regularObjects[i]);
  288. }
  289. sb.append("], ");
  290. sb.append("stringArray=" + strings);
  291. for( int i = 0; i < strings.length; i++ ) {
  292. if( i == 0 ) {
  293. sb.append("[");
  294. } else {
  295. sb.append(", ");
  296. }
  297. sb.append(strings[i]);
  298. }
  299. sb.append("]");
  300. return getClass().getSimpleName() + "@" + System.identityHashCode(this)
  301. + "[" + sb + "]";
  302. }
  303. }
  304. }