1 5 package ve.luz.ica.jackass.solver; 6 7 import java.io.Serializable ; 8 import java.io.ObjectInputStream ; 9 import java.io.IOException ; 10 import java.io.InvalidObjectException ; 11 import java.util.Map ; 12 import java.util.LinkedHashMap ; 13 import java.util.Iterator ; 14 15 import org.omg.CORBA.Object ; 16 17 24 class NodeProxyMap implements Serializable 25 { 26 private static final long serialVersionUID = 100L; 27 private static final NullPointerException NULL_ARGS_EX = new NullPointerException ("Null Arguments"); 28 private static final String ERR_NULL_MAP = "Null nodeProxyMap"; 29 private static final String ERR_NULL_ENTRY = "Null key or value in nodeProxyMap entry"; 30 private Map nodeProxyMap; private transient boolean dirty; 32 private transient Object [] proxies; 33 34 37 public NodeProxyMap() 38 { 39 this.nodeProxyMap = new LinkedHashMap (); 40 this.dirty = true; 41 } 42 43 48 public void add(String node, Object proxy) 49 { 50 if (node == null || proxy == null) throw NULL_ARGS_EX; 51 nodeProxyMap.put(node, proxy); 52 dirty = true; 53 } 54 55 59 public void remove(String node) 60 { 61 if (node == null) throw NULL_ARGS_EX; 62 java.lang.Object obj = nodeProxyMap.remove(node); 63 if (obj != null) 64 { 65 dirty = true; 66 } 67 } 68 69 73 public Object [] getProxies() 74 { 75 if (dirty) updateArray(); 76 return proxies; 77 } 78 79 84 public Object getProxy(String node) 85 { 86 if (node == null) throw NULL_ARGS_EX; 87 return (Object ) nodeProxyMap.get(node); 88 } 89 90 93 private void updateArray() 94 { 95 proxies = new Object [nodeProxyMap.values().size()]; 96 97 int ind = 0; 98 for (Iterator i = nodeProxyMap.values().iterator(); i.hasNext();) 99 { 100 proxies[ind++] = (Object ) i.next(); 101 } 102 dirty = false; 103 } 104 105 112 private void readObject(ObjectInputStream s) throws IOException , ClassNotFoundException 113 { 114 s.defaultReadObject(); 115 116 if (this.nodeProxyMap == null) 117 { 118 throw new InvalidObjectException (ERR_NULL_MAP); 119 } 120 121 for (Iterator i = nodeProxyMap.entrySet().iterator(); i.hasNext();) 122 { 123 Map.Entry entry = (Map.Entry ) i.next(); 124 if (entry.getKey() == null || entry.getValue() == null) 125 { 126 throw new InvalidObjectException (ERR_NULL_ENTRY); 127 } 128 } 129 dirty = true; 130 } 131 } 132 | Popular Tags |