Mercurial Hosting > luan
view core/src/luan/DeepCloner.java @ 422:af82b266fe89
add Io.repr()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 May 2015 16:13:52 -0600 |
parents | 24ede40ee0aa |
children | c6bcb8859b93 |
line wrap: on
line source
package luan; import java.util.Map; import java.util.IdentityHashMap; public final class DeepCloner { private final Map<Object,Object> cloned = new IdentityHashMap<Object,Object>(); public <T extends DeepCloneable<T>> T deepClone(T obj) { if( obj==null ) return null; @SuppressWarnings("unchecked") T rtn = (T)cloned.get(obj); if( rtn == null ) { rtn = obj.shallowClone(); cloned.put(obj,rtn); obj.deepenClone(rtn,this); } return rtn; } public <T> T[] deepClone(T[] obj) { if( obj.length == 0 ) return obj; @SuppressWarnings("unchecked") T[] rtn = (T[])cloned.get(obj); if( rtn == null ) { rtn = obj.clone(); cloned.put(obj,rtn); for( int i=0; i<rtn.length; i++ ) { @SuppressWarnings("unchecked") T t = get(rtn[i]); rtn[i] = t; } } return rtn; } public <T> T get(T obj) { if( !(obj instanceof DeepCloneable) ) return obj; @SuppressWarnings("unchecked") T dc = (T)deepClone((DeepCloneable)obj); return dc; } }