Mercurial Hosting > luan
diff src/luan/LuanImmutabler.java @ 1561:e1a13e707bf3
start immutable
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 05 Nov 2020 20:24:09 -0700 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/LuanImmutabler.java Thu Nov 05 20:24:09 2020 -0700 @@ -0,0 +1,61 @@ +package luan; + +import java.util.Map; +import java.util.Set; +import java.util.Collection; +import java.util.Collections; +import java.util.IdentityHashMap; + + +public final class LuanImmutabler { + private final Set handled = Collections.newSetFromMap(new IdentityHashMap()); + private Luan luan = null; + + public void makeImmutable(LuanCloneable obj) throws LuanException { + if( obj==null || !handled.add(obj) ) + return; + if( obj instanceof Luan ) { + if( luan != null ) + throw new RuntimeException("2 luans in "+this+" - "+luan+" "+obj); + luan = (Luan)obj; + } + obj.makeImmutable(this); + } + + public void makeImmutable(Object[] obj) throws LuanException { + if( obj==null || !handled.add(obj) ) + return; + for( Object entry : obj ) { + makeImmutable(entry); + } + } + + public void makeImmutable(Map obj) throws LuanException { + if( obj==null || !handled.add(obj) ) + return; + for( Object stupid : obj.entrySet() ) { + Map.Entry entry = (Map.Entry)stupid; + makeImmutable(entry.getKey()); + makeImmutable(entry.getValue()); + } + } + + public void makeImmutable(Collection obj) throws LuanException { + if( obj==null || !handled.add(obj) ) + return; + for( Object entry : (Collection)obj ) { + makeImmutable(entry); + } + } + + public void makeImmutable(Object obj) throws LuanException { + if( obj instanceof LuanCloneable ) + makeImmutable((LuanCloneable)obj); + else if( obj instanceof Object[] ) + makeImmutable((Object[])obj); + else if( obj instanceof Map ) + makeImmutable((Map)obj); + else if( obj instanceof Collection ) + makeImmutable((Collection)obj); + } +}