comparison src/luan/LuanCloner.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents d69d3c51c44e
children e1a13e707bf3
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
1 package luan; 1 package luan;
2 2
3 import java.util.Map; 3 import java.util.Map;
4 import java.util.HashMap; 4 import java.util.Collection;
5 import java.util.IdentityHashMap; 5 import java.util.IdentityHashMap;
6 6
7 7
8 public final class LuanCloner { 8 public final class LuanCloner {
9 public enum Type { COMPLETE, INCREMENTAL } 9 public enum Type { COMPLETE, INCREMENTAL }
10 10
11 public final Type type; 11 public final Type type;
12 private final Map cloned = new IdentityHashMap(); 12 private final Map cloned = new IdentityHashMap();
13 private Luan luan = null;
13 14
14 public LuanCloner(Type type) { 15 public LuanCloner(Type type) {
15 this.type = type; 16 this.type = type;
16 } 17 }
17 18
18 public LuanCloneable clone(LuanCloneable obj) { 19 public LuanCloneable clone(LuanCloneable obj) {
19 if( obj==null ) 20 if( obj==null )
20 return null; 21 return null;
21 LuanCloneable rtn = (LuanCloneable)cloned.get(obj); 22 LuanCloneable rtn = (LuanCloneable)cloned.get(obj);
22 if( rtn == null ) { 23 if( rtn == null ) {
24 if( obj instanceof Luan ) {
25 if( luan != null )
26 throw new RuntimeException("2 luans in "+type+" "+this+" - "+luan+" "+obj);
27 luan = (Luan)obj;
28 }
23 rtn = obj.shallowClone(); 29 rtn = obj.shallowClone();
24 cloned.put(obj,rtn); 30 cloned.put(obj,rtn);
25 obj.deepenClone(rtn,this); 31 obj.deepenClone(rtn,this);
26 } 32 }
27 return rtn; 33 return rtn;
40 } 46 }
41 return rtn; 47 return rtn;
42 } 48 }
43 49
44 public Map clone(Map obj) { 50 public Map clone(Map obj) {
45 if( !obj.getClass().equals(HashMap.class) )
46 throw new RuntimeException("can only clone HashMap");
47 Map rtn = (Map)cloned.get(obj); 51 Map rtn = (Map)cloned.get(obj);
48 if( rtn == null ) { 52 if( rtn == null ) {
49 rtn = new HashMap(); 53 try {
54 rtn = obj.getClass().newInstance();
55 } catch(InstantiationException e) {
56 throw new RuntimeException(e);
57 } catch(IllegalAccessException e) {
58 throw new RuntimeException(e);
59 }
50 for( Object stupid : obj.entrySet() ) { 60 for( Object stupid : obj.entrySet() ) {
51 Map.Entry entry = (Map.Entry)stupid; 61 Map.Entry entry = (Map.Entry)stupid;
52 rtn.put( get(entry.getKey()), get(entry.getValue()) ); 62 rtn.put( get(entry.getKey()), get(entry.getValue()) );
63 }
64 }
65 return rtn;
66 }
67
68 public Collection clone(Collection obj) {
69 Collection rtn = (Collection)cloned.get(obj);
70 if( rtn == null ) {
71 try {
72 rtn = obj.getClass().newInstance();
73 } catch(InstantiationException e) {
74 throw new RuntimeException(e);
75 } catch(IllegalAccessException e) {
76 throw new RuntimeException(e);
77 }
78 for( Object entry : (Collection)obj ) {
79 rtn.add( get(entry) );
53 } 80 }
54 } 81 }
55 return rtn; 82 return rtn;
56 } 83 }
57 84
60 return clone((LuanCloneable)obj); 87 return clone((LuanCloneable)obj);
61 if( obj instanceof Object[] ) 88 if( obj instanceof Object[] )
62 return clone((Object[])obj); 89 return clone((Object[])obj);
63 if( obj instanceof Map ) 90 if( obj instanceof Map )
64 return clone((Map)obj); 91 return clone((Map)obj);
92 if( obj instanceof Collection )
93 return clone((Collection)obj);
65 return obj; 94 return obj;
66 } 95 }
67 } 96 }