Mercurial Hosting > luan
annotate src/luan/LuanCloner.java @ 1277:5ba660381bd5
fix Io.print()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 11 Dec 2018 03:38:43 -0700 |
parents | d69d3c51c44e |
children | e0cf0d108a77 |
rev | line source |
---|---|
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
1 package luan; |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
2 |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
3 import java.util.Map; |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
4 import java.util.HashMap; |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
5 import java.util.IdentityHashMap; |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
6 |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
7 |
781
fbbdd369a13a
rename DeepCloner to LuanCloner
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
8 public final class LuanCloner { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
9 public enum Type { COMPLETE, INCREMENTAL } |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
10 |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
11 public final Type type; |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
12 private final Map cloned = new IdentityHashMap(); |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
13 |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
14 public LuanCloner(Type type) { |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
15 this.type = type; |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
16 } |
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
17 |
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
18 public LuanCloneable clone(LuanCloneable obj) { |
195
24ede40ee0aa
make MetatableGetter DeepCloneable, scoped, and secure
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
19 if( obj==null ) |
24ede40ee0aa
make MetatableGetter DeepCloneable, scoped, and secure
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
20 return null; |
781
fbbdd369a13a
rename DeepCloner to LuanCloner
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
21 LuanCloneable rtn = (LuanCloneable)cloned.get(obj); |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
22 if( rtn == null ) { |
784
6a7c6879158d
revert shallowClone()
Franklin Schmidt <fschmidt@gmail.com>
parents:
782
diff
changeset
|
23 rtn = obj.shallowClone(); |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
24 cloned.put(obj,rtn); |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
25 obj.deepenClone(rtn,this); |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
26 } |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
27 return rtn; |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
28 } |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
29 |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
30 public Object[] clone(Object[] obj) { |
78
7c08b611125d
better deep cloning
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
77
diff
changeset
|
31 if( obj.length == 0 ) |
7c08b611125d
better deep cloning
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
77
diff
changeset
|
32 return obj; |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
33 Object[] rtn = (Object[])cloned.get(obj); |
78
7c08b611125d
better deep cloning
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
77
diff
changeset
|
34 if( rtn == null ) { |
7c08b611125d
better deep cloning
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
77
diff
changeset
|
35 rtn = obj.clone(); |
7c08b611125d
better deep cloning
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
77
diff
changeset
|
36 cloned.put(obj,rtn); |
7c08b611125d
better deep cloning
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
77
diff
changeset
|
37 for( int i=0; i<rtn.length; i++ ) { |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
38 rtn[i] = get(rtn[i]); |
78
7c08b611125d
better deep cloning
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
77
diff
changeset
|
39 } |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
40 } |
78
7c08b611125d
better deep cloning
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
77
diff
changeset
|
41 return rtn; |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
42 } |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
43 |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
44 public Map clone(Map obj) { |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
45 if( !obj.getClass().equals(HashMap.class) ) |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
46 throw new RuntimeException("can only clone HashMap"); |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
47 Map rtn = (Map)cloned.get(obj); |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
48 if( rtn == null ) { |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
49 rtn = new HashMap(); |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
50 for( Object stupid : obj.entrySet() ) { |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
51 Map.Entry entry = (Map.Entry)stupid; |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
52 rtn.put( get(entry.getKey()), get(entry.getValue()) ); |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
53 } |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
54 } |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
55 return rtn; |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
56 } |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
57 |
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
58 public Object get(Object obj) { |
781
fbbdd369a13a
rename DeepCloner to LuanCloner
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
59 if( obj instanceof LuanCloneable ) |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
60 return clone((LuanCloneable)obj); |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
61 if( obj instanceof Object[] ) |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
62 return clone((Object[])obj); |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
63 if( obj instanceof Map ) |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
64 return clone((Map)obj); |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
195
diff
changeset
|
65 return obj; |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
66 } |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
67 } |