KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtCollection


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 collections of objects.
11  */

12
13 public class JtCollection extends JtObject {
14 // private Hashtable col = null;// Object table
15
private HashMap col = null;
16   private int size = 0;
17
18
19   public JtCollection() {
20   }
21
22   /**
23     * Void operation.
24     */

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

33   
34   public int getSize () {
35      return (size);
36   }
37
38   /**
39    * Returns a JtIterartor over the collection.
40    */

41
42   public Object JavaDoc getIterator () {
43      JtIterator jit;
44      Collection values;
45
46      jit = new JtIterator ();
47      
48      if (col == null)
49        return (null);
50
51      values = col.values ();
52
53      if (values == null)
54        return (null);
55      jit.setIterator(values.iterator ());
56      
57      return (jit);
58   }
59
60
61   /**
62     * Broadcasts a message to all the objects in the collection.
63     */

64
65   private void broadcast_message (JtMessage msg)
66   {
67     Collection values;
68     Iterator it;
69
70     if (msg == null || col == null)
71       return;
72
73     values = col.values ();
74     if (values == null)
75       return;
76     it = values.iterator ();
77
78     while (it.hasNext()) {
79       sendMessage (it.next (), msg);
80     }
81
82   }
83
84   /**
85     * Process object messages.
86     * <ul>
87     * <li>JtADD - Adds the object specified by msgContent to this collection
88     * <li>JtMESSAGE - Adds the object specified by msgContent to this collection
89     * <li>JtBROADCAST - Broadcast the message specified by msgContent to all the objects
90     * in this collection
91     * <li>JtCLEAR - Removes all the objects from this collection
92     * </ul>
93     */

94
95   public Object JavaDoc processMessage (Object JavaDoc event) {
96
97    String JavaDoc msgid = null;
98    JtMessage e = (JtMessage) event;
99    Object JavaDoc content;
100    Object JavaDoc data;
101
102
103      if (e == null)
104     return null;
105
106      msgid = (String JavaDoc) e.getMsgId ();
107
108      if (msgid == null)
109     return null;
110
111      content = e.getMsgContent();
112      //data = e.getMsgData ();
113

114      // Destroy this object
115
if (msgid.equals ("JtREMOVE")) {
116        return (null);
117      }
118
119      if (msgid.equals ("JtCOLLECTION_ADD") || msgid.equals ("JtADD")) {
120         // Add object to the collection
121

122         if (content == null) {
123           handleWarning
124             ("JtCollection.processMessage(JtCOLLECTION_ADD):invalid content (null)");
125           return (this);
126
127         }
128         if (col == null)
129           col = new HashMap ();
130 // col = new Hashtable ();
131

132         size++;
133         col.put (content, content);
134         return (this);
135      }
136
137
138
139      if (msgid.equals ("JtOBJECT") || msgid.equals ("JtMESSAGE")) {
140         // Add object to the collection
141

142         if (content == null)
143           return (this);
144         if (col == null)
145           col = new HashMap ();
146 // col = new Hashtable ();
147

148         size++;
149         col.put (content, content);
150         return (this);
151      }
152
153      if (msgid.equals ("JtCLEAR")) {
154      
155        if (col != null) {
156          col.clear ();
157        }
158        size = 0;
159
160        return (this);
161      }
162
163      // Broadcasts a message to all the members
164
// of the collection
165

166      if (msgid.equals ("JtBROADCAST")) {
167      
168        if (col == null) {
169          return (this);
170        }
171
172        broadcast_message ((JtMessage) content);
173
174        return (this);
175      }
176           
177      handleError ("JtCollection.processMessage: invalid message id:" + msgid);
178      return (null);
179
180   }
181
182  
183   /**
184    * Unit tests the messages processed by JtCollection
185    */

186
187   public static void main(String JavaDoc[] args) {
188
189     JtObject main = new JtObject ();
190     JtMessage msg, msg1;
191     Integer JavaDoc count;
192
193     //main.setObjTrace (1);
194
//main.setLogFile ("log.txt");
195

196
197     // Create JtColletion
198

199     main.createObject ("Jt.JtCollection", "collection");
200
201
202     msg = (JtMessage) main.createObject ("Jt.JtMessage", "message");
203     main.setValue (msg, "msgId", "JtADD");
204     main.setValue (msg, "msgContent", "Hello");
205     count = (Integer JavaDoc) main.getValue ("collection", "size");
206
207     // Add object to the collection
208

209     main.sendMessage ("collection", msg);
210
211     
212     count = (Integer JavaDoc) main.getValue ("collection", "size");
213
214     if (count.intValue () == 1)
215       System.err.println ("JtCollection(JtADD):GO");
216     else
217       System.err.println ("JtCollection:FAILED");
218
219     // Clear the collection
220

221     main.setValue (msg, "msgId", "JtCLEAR");
222     main.sendMessage ("collection", msg);
223
224     count = (Integer JavaDoc) main.getValue ("collection", "size");
225
226     if (count.intValue() == 0)
227       System.err.println ("JtCollection(JtCLEAR):GO");
228     else
229       System.err.println ("JtCollection:FAILED");
230
231     msg1 = (JtMessage) main.createObject ("Jt.JtMessage", "message1");
232     main.setValue ("message1", "msgId", "JtOBJECT");
233
234     main.setValue ("message", "msgId", "JtBROADCAST");
235     main.setValue ("message", "msgContent", msg1);
236
237     main.sendMessage ("collection", "message");
238
239     main.removeObject ("collection");
240
241
242   }
243
244 }
245
246
247
Free Books   Free Magazines  
Popular Tags