comparison src/luan/LuanCloner.java @ 1561:e1a13e707bf3

start immutable
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 05 Nov 2020 20:24:09 -0700
parents e0cf0d108a77
children
comparison
equal deleted inserted replaced
1560:33a53c43e2f7 1561:e1a13e707bf3
32 } 32 }
33 return rtn; 33 return rtn;
34 } 34 }
35 35
36 public Object[] clone(Object[] obj) { 36 public Object[] clone(Object[] obj) {
37 if( obj.length == 0 ) 37 if( obj==null || obj.length == 0 )
38 return obj; 38 return obj;
39 Object[] rtn = (Object[])cloned.get(obj); 39 Object[] rtn = (Object[])cloned.get(obj);
40 if( rtn == null ) { 40 if( rtn == null ) {
41 rtn = obj.clone(); 41 rtn = obj.clone();
42 cloned.put(obj,rtn); 42 cloned.put(obj,rtn);
46 } 46 }
47 return rtn; 47 return rtn;
48 } 48 }
49 49
50 public Map clone(Map obj) { 50 public Map clone(Map obj) {
51 if( obj==null )
52 return null;
51 Map rtn = (Map)cloned.get(obj); 53 Map rtn = (Map)cloned.get(obj);
52 if( rtn == null ) { 54 if( rtn == null ) {
53 try { 55 try {
54 rtn = obj.getClass().newInstance(); 56 rtn = obj.getClass().newInstance();
55 } catch(InstantiationException e) { 57 } catch(InstantiationException e) {
56 throw new RuntimeException(e); 58 throw new RuntimeException(e);
57 } catch(IllegalAccessException e) { 59 } catch(IllegalAccessException e) {
58 throw new RuntimeException(e); 60 throw new RuntimeException(e);
59 } 61 }
62 cloned.put(obj,rtn);
60 for( Object stupid : obj.entrySet() ) { 63 for( Object stupid : obj.entrySet() ) {
61 Map.Entry entry = (Map.Entry)stupid; 64 Map.Entry entry = (Map.Entry)stupid;
62 rtn.put( get(entry.getKey()), get(entry.getValue()) ); 65 rtn.put( get(entry.getKey()), get(entry.getValue()) );
63 } 66 }
64 } 67 }
65 return rtn; 68 return rtn;
66 } 69 }
67 70
68 public Collection clone(Collection obj) { 71 public Collection clone(Collection obj) {
72 if( obj==null )
73 return null;
69 Collection rtn = (Collection)cloned.get(obj); 74 Collection rtn = (Collection)cloned.get(obj);
70 if( rtn == null ) { 75 if( rtn == null ) {
71 try { 76 try {
72 rtn = obj.getClass().newInstance(); 77 rtn = obj.getClass().newInstance();
73 } catch(InstantiationException e) { 78 } catch(InstantiationException e) {