Mercurial Hosting > luan
comparison src/luan/DeepCloner.java @ 77:4bf3d0c0b6b9
make LuanState cloneable
git-svn-id: https://luan-java.googlecode.com/svn/trunk@78 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 15 Feb 2013 09:55:17 +0000 |
parents | |
children | 7c08b611125d |
comparison
equal
deleted
inserted
replaced
76:97b03fc807ad | 77:4bf3d0c0b6b9 |
---|---|
1 package luan; | |
2 | |
3 import java.util.Map; | |
4 import java.util.IdentityHashMap; | |
5 | |
6 | |
7 public final class DeepCloner { | |
8 private final Map<Object,Object> cloned = new IdentityHashMap<Object,Object>(); | |
9 | |
10 public <T extends DeepCloneable<T>> T deepClone(T obj) { | |
11 @SuppressWarnings("unchecked") | |
12 T rtn = (T)cloned.get(obj); | |
13 if( rtn == null ) { | |
14 rtn = obj.shallowClone(); | |
15 cloned.put(obj,rtn); | |
16 obj.deepenClone(rtn,this); | |
17 } | |
18 return rtn; | |
19 } | |
20 | |
21 public void deepenClone(Object[] a) { | |
22 for( int i=0; i<a.length; i++ ) { | |
23 a[i] = get(a[i]); | |
24 } | |
25 } | |
26 | |
27 public Object get(Object obj) { | |
28 if( !(obj instanceof DeepCloneable) ) | |
29 return obj; | |
30 @SuppressWarnings("unchecked") | |
31 DeepCloneable dc = deepClone((DeepCloneable)obj); | |
32 return dc; | |
33 } | |
34 } |