KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtHashTable


1
2
3 package Jt;
4 import java.util.*;
5 import java.lang.reflect.*;
6 import java.beans.*;
7 import java.io.*;
8
9 /**
10   * Handles hash tables.
11   */

12
13 public class JtHashTable extends JtObject {
14
15   protected HashMap hashmap = null;
16   private int size = 0;
17
18
19   public JtHashTable() {
20   }
21
22
23   /**
24     * Void operation.
25     */

26
27   public void setSize (int size) {
28      this.size = this.size; // void operation
29
}
30
31   /**
32     * Returns the number of elements in the hash table.
33     */

34
35   public int getSize () {
36      return (hashmap != null ? hashmap.size (): 0);
37   }
38
39
40   /**
41     * Specifies the HashMap object used to represent this object
42     */

43
44   public void setHashmap (HashMap hashmap) {
45      this.hashmap = hashmap; // void operation
46
}
47
48   /**
49     * Returns the HashMap.
50     */

51
52   public HashMap getHashmap () {
53      return (hashmap);
54   }
55
56
57   /**
58    * Returns a JtIterator.
59    */

60
61   public Object JavaDoc getIterator () {
62      JtIterator jit;
63      Collection values;
64
65      jit = new JtIterator ();
66      
67      if (hashmap == null)
68        return (null);
69
70      values = hashmap.values ();
71
72      if (values == null)
73        return (null);
74      jit.setIterator(values.iterator ());
75      
76      return (jit);
77   }
78
79
80
81   // Broadcast a message
82

83   private void broadcast_message (JtMessage msg)
84   {
85     Collection values;
86     Iterator it;
87
88     if (msg == null || hashmap == null)
89       return;
90
91     values = hashmap.values ();
92     if (values == null)
93       return;
94     it = values.iterator ();
95
96     while (it.hasNext()) {
97       sendMessage (it.next (), msg);
98     }
99
100   }
101
102   /**
103     * Process object messages.
104     * <ul>
105     * <li> JtPUT - Adds the object specified by msgContent to this hash table. It associates
106     * this object with the key specified by msgData
107     * <li> JtCLEAR - Removes all the objects from this hash table
108     * <li> JtGET - Returns the value to which the specified key (msgData) is mapped to
109     * <li>JtBROADCAST - Broadcast the message specified by msgContent to all the objects
110     * in this hash table
111     * </ul>
112     * @param message Jt Message
113     */

114
115   public Object JavaDoc processMessage (Object JavaDoc message) {
116
117    String JavaDoc msgid = null;
118    JtMessage e = (JtMessage) message;
119    Object JavaDoc content;
120    Object JavaDoc data;
121
122
123      if (e == null)
124     return null;
125
126      msgid = (String JavaDoc) e.getMsgId ();
127
128      if (msgid == null)
129     return null;
130
131      content = e.getMsgContent();
132      data = e.getMsgData ();
133
134      // Remove this object
135
if (msgid.equals ("JtREMOVE")) {
136        return (null);
137      }
138
139      if (msgid.equals ("JtPUT")) {
140         // Add an object to the hash table
141

142         if (content == null) {
143           handleWarning
144             ("JtHashTable.processMessage(JtPUT):adding null value");
145           //return (this);
146

147         }
148         if (hashmap == null)
149           hashmap = new HashMap ();
150 // col = new Hashtable ();
151

152 // size++;
153
hashmap.put (data, content);
154         return (this);
155      }
156
157
158      if (msgid.equals ("JtGET")) {
159
160
161         if (data == null) {
162           handleWarning
163             ("JtHashTable.processMessage(JtGET):invalid value (null)");
164           return (this);
165
166         }
167         if (hashmap == null)
168           return (null);
169         
170
171         return (hashmap.get (data));
172
173      }
174
175
176      if (msgid.equals ("JtCLEAR")) {
177      
178        if (hashmap != null) {
179          hashmap.clear ();
180        }
181        size = 0;
182
183        return (this);
184      }
185
186      // Broadcast a message to all the members
187
// of the hash table
188

189      if (msgid.equals ("JtBROADCAST")) {
190      
191        if (hashmap == null) {
192          return (this);
193        }
194
195        broadcast_message ((JtMessage) content);
196
197        return (this);
198      }
199           
200      handleError ("JtHashTable.processMessage: invalid message id:" + msgid);
201      return (null);
202
203   }
204
205
206
207   /**
208     * Unit tests the messages processed by JtHashTable.
209     */

210
211   public static void main(String JavaDoc[] args) {
212
213     JtObject main = new JtObject ();
214     JtMessage msg, msg1;
215     Integer JavaDoc count;
216
217     //main.setObjTrace (1);
218
//main.setLogFile ("log.txt");
219

220
221     // Create JtColletion
222

223     main.createObject ("Jt.JtHashTable", "hashtable");
224
225
226     msg = (JtMessage) main.createObject ("Jt.JtMessage", "message");
227     main.setValue ("message", "msgId", "JtPUT");
228     main.setValue ("message", "msgContent", new Integer JavaDoc (1));
229     main.setValue ("message", "msgData", "one");
230
231
232     // Add object to the hashtable
233

234
235     main.sendMessage ("hashtable", "message");
236
237     main.setValue ("message", "msgId", "JtGET");
238     main.setValue ("message", "msgData", "one");
239
240     System.err.println ("JtHashTable(JtGET):" + main.sendMessage ("hashtable", "message"));
241
242     
243     count = (Integer JavaDoc) main.getValue ("hashtable", "size");
244
245     if (count.intValue () == 1)
246       System.err.println ("JtHashTable(JtPUT):GO");
247     else
248       System.err.println ("JtHashTable:FAILED");
249
250     // Clear the hashtable
251

252     main.setValue ("message", "msgId", "JtCLEAR");
253     main.sendMessage ("hashtable", "message");
254
255     count = (Integer JavaDoc) main.getValue ("hashtable", "size");
256
257     if (count.intValue() == 0)
258       System.err.println ("JtHashTable (JtCLEAR):GO");
259     else
260       System.err.println ("JtHashTable:FAILED");
261
262     msg1 = (JtMessage) main.createObject ("Jt.JtMessage", "message1");
263     main.setValue ("message1", "msgId", "JtOBJECT");
264
265     main.setValue ("message", "msgId", "JtBROADCAST");
266     main.setValue ("message", "msgContent", msg1);
267
268     main.sendMessage ("hashtable", "message");
269     main.removeObject ("hashtable");
270
271
272   }
273
274 }
275
276
277
Popular Tags