KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > solver > NodeProxyMap


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.solver;
6
7 import java.io.Serializable JavaDoc;
8 import java.io.ObjectInputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InvalidObjectException JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.LinkedHashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import org.omg.CORBA.Object JavaDoc;
16
17 /**
18  * An utility class that manages mappings between nodes and proxies related
19  * to a single Component.
20  * This implementation caches all the proxies, so requests for all the proxies
21  * are optimized.
22  * To change this template use Options | File Templates.
23  */

24 class NodeProxyMap implements Serializable JavaDoc
25 {
26     private static final long serialVersionUID = 100L;
27     private static final NullPointerException JavaDoc NULL_ARGS_EX = new NullPointerException JavaDoc("Null Arguments");
28     private static final String JavaDoc ERR_NULL_MAP = "Null nodeProxyMap";
29     private static final String JavaDoc ERR_NULL_ENTRY = "Null key or value in nodeProxyMap entry";
30     private Map JavaDoc nodeProxyMap; //Map<String node, Object proxy>
31
private transient boolean dirty;
32     private transient Object JavaDoc[] proxies;
33
34     /**
35      * Creates an empty NodeProxyMap.
36      */

37     public NodeProxyMap()
38     {
39         this.nodeProxyMap = new LinkedHashMap JavaDoc();
40         this.dirty = true;
41     }
42
43     /**
44      * Adds a node/proxy pair. If the node already had a proxy, it is substituted.
45      * @param node a string that identifies the node
46      * @param proxy a proxy reference
47      */

48     public void add(String JavaDoc node, Object JavaDoc proxy)
49     {
50         if (node == null || proxy == null) throw NULL_ARGS_EX;
51         nodeProxyMap.put(node, proxy);
52         dirty = true;
53     }
54
55     /**
56      * Removes the entry associated with a specified node.
57      * @param node a string that identifies the node
58      */

59     public void remove(String JavaDoc node)
60     {
61         if (node == null) throw NULL_ARGS_EX;
62         java.lang.Object JavaDoc obj = nodeProxyMap.remove(node);
63         if (obj != null)
64         {
65             dirty = true;
66         }
67     }
68
69     /**
70      * Returns an array with all the proxies of the component.
71      * @return the array of proxy references.
72      */

73     public Object JavaDoc[] getProxies()
74     {
75         if (dirty) updateArray();
76         return proxies;
77     }
78
79     /**
80      * Returns the proxy reference associated with the specified node.
81      * @param node a string that identifies the node
82      * @return the proxy reference
83      */

84     public Object JavaDoc getProxy(String JavaDoc node)
85     {
86         if (node == null) throw NULL_ARGS_EX;
87         return (Object JavaDoc) nodeProxyMap.get(node);
88     }
89
90     /**
91      * Updates an internal array of proxy references.
92      */

93     private void updateArray()
94     {
95         proxies = new Object JavaDoc[nodeProxyMap.values().size()];
96
97         int ind = 0;
98         for (Iterator JavaDoc i = nodeProxyMap.values().iterator(); i.hasNext();)
99         {
100             proxies[ind++] = (Object JavaDoc) i.next();
101         }
102         dirty = false;
103     }
104
105     /**
106      * Verifies the validity of an instance when it is being deserialized.
107      * @param s the input stream
108      * @throws InvalidObjectException when the deserialized object is invalid
109      * @throws IOException when an IO error occurs
110      * @throws ClassNotFoundException when an unexpected error occurs
111      */

112     private void readObject(ObjectInputStream JavaDoc s) throws IOException JavaDoc, ClassNotFoundException JavaDoc
113     {
114         s.defaultReadObject();
115
116         if (this.nodeProxyMap == null)
117         {
118             throw new InvalidObjectException JavaDoc(ERR_NULL_MAP);
119         }
120
121         for (Iterator JavaDoc i = nodeProxyMap.entrySet().iterator(); i.hasNext();)
122         {
123             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
124             if (entry.getKey() == null || entry.getValue() == null)
125             {
126                 throw new InvalidObjectException JavaDoc(ERR_NULL_ENTRY);
127             }
128         }
129         dirty = true;
130     }
131 }
132
Popular Tags