Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- a/src/luan/LuanCloner.java Tue Feb 12 22:53:57 2019 -0700 +++ b/src/luan/LuanCloner.java Thu Feb 14 03:10:45 2019 -0700 @@ -1,7 +1,7 @@ package luan; import java.util.Map; -import java.util.HashMap; +import java.util.Collection; import java.util.IdentityHashMap; @@ -10,6 +10,7 @@ public final Type type; private final Map cloned = new IdentityHashMap(); + private Luan luan = null; public LuanCloner(Type type) { this.type = type; @@ -20,6 +21,11 @@ return null; LuanCloneable rtn = (LuanCloneable)cloned.get(obj); if( rtn == null ) { + if( obj instanceof Luan ) { + if( luan != null ) + throw new RuntimeException("2 luans in "+type+" "+this+" - "+luan+" "+obj); + luan = (Luan)obj; + } rtn = obj.shallowClone(); cloned.put(obj,rtn); obj.deepenClone(rtn,this); @@ -42,11 +48,15 @@ } public Map clone(Map obj) { - if( !obj.getClass().equals(HashMap.class) ) - throw new RuntimeException("can only clone HashMap"); Map rtn = (Map)cloned.get(obj); if( rtn == null ) { - rtn = new HashMap(); + try { + rtn = obj.getClass().newInstance(); + } catch(InstantiationException e) { + throw new RuntimeException(e); + } catch(IllegalAccessException e) { + throw new RuntimeException(e); + } for( Object stupid : obj.entrySet() ) { Map.Entry entry = (Map.Entry)stupid; rtn.put( get(entry.getKey()), get(entry.getValue()) ); @@ -55,6 +65,23 @@ return rtn; } + public Collection clone(Collection obj) { + Collection rtn = (Collection)cloned.get(obj); + if( rtn == null ) { + try { + rtn = obj.getClass().newInstance(); + } catch(InstantiationException e) { + throw new RuntimeException(e); + } catch(IllegalAccessException e) { + throw new RuntimeException(e); + } + for( Object entry : (Collection)obj ) { + rtn.add( get(entry) ); + } + } + return rtn; + } + public Object get(Object obj) { if( obj instanceof LuanCloneable ) return clone((LuanCloneable)obj); @@ -62,6 +89,8 @@ return clone((Object[])obj); if( obj instanceof Map ) return clone((Map)obj); + if( obj instanceof Collection ) + return clone((Collection)obj); return obj; } }