Mercurial Hosting > luan
comparison src/luan/LuanCloner.java @ 782:655280eab1e2
start limited cloning
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 30 Aug 2016 01:29:33 -0600 |
parents | fbbdd369a13a |
children | 6a7c6879158d |
comparison
equal
deleted
inserted
replaced
781:fbbdd369a13a | 782:655280eab1e2 |
---|---|
4 import java.util.HashMap; | 4 import java.util.HashMap; |
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 final boolean deep; | |
9 private final Map cloned = new IdentityHashMap(); | 10 private final Map cloned = new IdentityHashMap(); |
10 | 11 |
11 public LuanCloneable deepClone(LuanCloneable obj) { | 12 public LuanCloner(boolean deep) { |
13 this.deep = deep; | |
14 } | |
15 | |
16 public LuanCloneable clone(LuanCloneable obj) { | |
12 if( obj==null ) | 17 if( obj==null ) |
13 return null; | 18 return null; |
14 LuanCloneable rtn = (LuanCloneable)cloned.get(obj); | 19 LuanCloneable rtn = (LuanCloneable)cloned.get(obj); |
15 if( rtn == null ) { | 20 if( rtn == null ) { |
16 rtn = obj.shallowClone(); | 21 rtn = obj.shallowClone(this); |
17 cloned.put(obj,rtn); | 22 cloned.put(obj,rtn); |
18 obj.deepenClone(rtn,this); | 23 obj.deepenClone(rtn,this); |
19 } | 24 } |
20 return rtn; | 25 return rtn; |
21 } | 26 } |
22 | 27 |
23 public Object[] deepClone(Object[] obj) { | 28 public Object[] clone(Object[] obj) { |
24 if( obj.length == 0 ) | 29 if( obj.length == 0 ) |
25 return obj; | 30 return obj; |
26 Object[] rtn = (Object[])cloned.get(obj); | 31 Object[] rtn = (Object[])cloned.get(obj); |
27 if( rtn == null ) { | 32 if( rtn == null ) { |
28 rtn = obj.clone(); | 33 rtn = obj.clone(); |
32 } | 37 } |
33 } | 38 } |
34 return rtn; | 39 return rtn; |
35 } | 40 } |
36 | 41 |
37 public Map deepClone(Map obj) { | 42 public Map clone(Map obj) { |
38 if( !obj.getClass().equals(HashMap.class) ) | 43 if( !obj.getClass().equals(HashMap.class) ) |
39 throw new RuntimeException("can only clone HashMap"); | 44 throw new RuntimeException("can only clone HashMap"); |
40 Map rtn = (Map)cloned.get(obj); | 45 Map rtn = (Map)cloned.get(obj); |
41 if( rtn == null ) { | 46 if( rtn == null ) { |
42 rtn = new HashMap(); | 47 rtn = new HashMap(); |
48 return rtn; | 53 return rtn; |
49 } | 54 } |
50 | 55 |
51 public Object get(Object obj) { | 56 public Object get(Object obj) { |
52 if( obj instanceof LuanCloneable ) | 57 if( obj instanceof LuanCloneable ) |
53 return deepClone((LuanCloneable)obj); | 58 return clone((LuanCloneable)obj); |
54 if( obj instanceof Object[] ) | 59 if( obj instanceof Object[] ) |
55 return deepClone((Object[])obj); | 60 return clone((Object[])obj); |
56 if( obj instanceof Map ) | 61 if( obj instanceof Map ) |
57 return deepClone((Map)obj); | 62 return clone((Map)obj); |
58 return obj; | 63 return obj; |
59 } | 64 } |
65 /* | |
66 public Object check(Object obj) { | |
67 if( deep ) | |
68 throw new RuntimeException(); | |
69 if( !(obj instanceof LuanCloneable) ) | |
70 return obj; | |
71 LuanCloneable lc = (LuanCloneable)obj; | |
72 return lc.getCloner() == this ? lc : clone(lc); | |
73 } | |
74 */ | |
60 } | 75 } |