comparison 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
comparison
equal deleted inserted replaced
1560:33a53c43e2f7 1561:e1a13e707bf3
1 package luan;
2
3 import java.util.Map;
4 import java.util.Set;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.IdentityHashMap;
8
9
10 public final class LuanImmutabler {
11 private final Set handled = Collections.newSetFromMap(new IdentityHashMap());
12 private Luan luan = null;
13
14 public void makeImmutable(LuanCloneable obj) throws LuanException {
15 if( obj==null || !handled.add(obj) )
16 return;
17 if( obj instanceof Luan ) {
18 if( luan != null )
19 throw new RuntimeException("2 luans in "+this+" - "+luan+" "+obj);
20 luan = (Luan)obj;
21 }
22 obj.makeImmutable(this);
23 }
24
25 public void makeImmutable(Object[] obj) throws LuanException {
26 if( obj==null || !handled.add(obj) )
27 return;
28 for( Object entry : obj ) {
29 makeImmutable(entry);
30 }
31 }
32
33 public void makeImmutable(Map obj) throws LuanException {
34 if( obj==null || !handled.add(obj) )
35 return;
36 for( Object stupid : obj.entrySet() ) {
37 Map.Entry entry = (Map.Entry)stupid;
38 makeImmutable(entry.getKey());
39 makeImmutable(entry.getValue());
40 }
41 }
42
43 public void makeImmutable(Collection obj) throws LuanException {
44 if( obj==null || !handled.add(obj) )
45 return;
46 for( Object entry : (Collection)obj ) {
47 makeImmutable(entry);
48 }
49 }
50
51 public void makeImmutable(Object obj) throws LuanException {
52 if( obj instanceof LuanCloneable )
53 makeImmutable((LuanCloneable)obj);
54 else if( obj instanceof Object[] )
55 makeImmutable((Object[])obj);
56 else if( obj instanceof Map )
57 makeImmutable((Map)obj);
58 else if( obj instanceof Collection )
59 makeImmutable((Collection)obj);
60 }
61 }