KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtList


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 a list of Jt objects.
11  */

12
13 public class JtList extends JtCollection {
14 //private Hashtable col = null;// Object table
15
private LinkedList col = null;
16  
17   public JtList() {
18   }
19
20
21 /**
22  * Returns the LinkedList used to implement this class.
23  */

24   public LinkedList getLinkedList () {
25     return (col);
26   }
27
28
29
30 /**
31  * Sets the LinkedList used to implement this class.
32  */

33
34   public void setLinkedList (LinkedList col) {
35     this.col = col;
36   }
37
38   /**
39    * Returns a JtIterator.
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 */

56      jit.setIterator(col.iterator ());
57      
58      return (jit);
59   }
60
61   /**
62     * Void operation.
63     */

64
65   public void setSize (int size) {
66     // this.size = this.size; // void operation
67
}
68
69   /**
70     * Returns the number of elements in this list.
71     */

72   
73   public int getSize () {
74      return (col != null ? col.size (): 0);
75   }
76
77
78   /**
79     * Process object messages.
80     * <ul>
81     * <li> JtADD - Adds the object specified by msgContent to this list
82     * <li> JtCLEAR - Removes all the objects from this list
83     * <li> JtFIRST - Returns the first element in the list
84     * <li> JtLAST - Returns the last element in the list
85     * <li> JtREMOVE_FIRST - Removes and returns the first element in the list
86     * </ul>
87     * @param message Jt Message
88     */

89
90   public Object JavaDoc processMessage (Object JavaDoc message) {
91
92    String JavaDoc msgid = null;
93    JtMessage e = (JtMessage) message;
94    Object JavaDoc content;
95    Object JavaDoc data;
96
97
98      if (e == null)
99     return null;
100
101      msgid = (String JavaDoc) e.getMsgId ();
102
103      if (msgid == null)
104     return null;
105
106      content = e.getMsgContent();
107      //data = e.getMsgData ();
108

109      // Remove this object
110
if (msgid.equals ("JtREMOVE")) {
111        return (null);
112      }
113
114      if (msgid.equals ("JtADD")) {
115         // Add object to the list
116

117         if (content == null) {
118           handleWarning
119             ("JtCollection.processMessage(JtADD):invalid content (null)");
120           return (this);
121
122         }
123         if (col == null)
124           col = new LinkedList ();
125 // col = new Hashtable ();
126

127 // size++;
128
col.add (content);
129         return (this);
130      }
131
132
133      if (msgid.equals ("JtCLEAR")) {
134      
135        if (col != null) {
136          col.clear ();
137        }
138 // size = 0;
139

140        return (this);
141      }
142
143      if (msgid.equals ("JtFIRST")) {
144
145        if (col == null)
146          return (null);
147      
148        if (col.size () < 1) {
149          return (null);
150        }
151
152        return (col.getFirst ());
153      }
154
155
156      if (msgid.equals ("JtLAST")) {
157
158        if (col == null)
159          return (null);
160      
161        if (col.size () < 1) {
162          return (null);
163        }
164
165        return (col.getLast ());
166      }
167
168
169      if (msgid.equals ("JtREMOVE_FIRST")) {
170
171        if (col == null)
172          return (null);
173      
174        if (col.size () < 1) {
175          return (null);
176        }
177
178        return (col.removeFirst ());
179      }
180          
181      handleError ("JtList.processMessage: invalid message id:" + msgid);
182      return (null);
183
184   }
185
186  
187   /**
188     * Unit tests the messages processed by JtList.
189     */

190
191   public static void main(String JavaDoc[] args) {
192
193     JtObject main = new JtObject ();
194     JtMessage msg, msg1;
195     Integer JavaDoc count;
196     JtIterator it;
197     Object JavaDoc obj;
198
199     //main.setObjTrace (1);
200
//main.setLogFile ("log.txt");
201

202
203     // Create a JtList
204

205     main.createObject ("Jt.JtList", "list");
206
207
208     msg = (JtMessage) main.createObject ("Jt.JtMessage", "message");
209     main.setValue (msg, "msgId", "JtADD");
210     main.setValue (msg, "msgContent", new Integer JavaDoc (1));
211
212     // Add an object to the list
213

214     main.sendMessage ("list", msg);
215
216
217     main.setValue (msg, "msgId", "JtADD");
218     main.setValue (msg, "msgContent", new Integer JavaDoc (2));
219
220  
221     // Add object to the list
222

223     main.sendMessage ("list", msg);
224
225    System.out.println ("Size=" + main.getValue ("list", "size"));
226
227
228     obj = (Object JavaDoc) main.sendMessage ("list", new JtMessage ("JtFIRST"));
229
230     System.out.println ("First=" + obj);
231
232     obj = (Object JavaDoc) main.sendMessage ("list", new JtMessage ("JtLAST"));
233
234     System.out.println ("Last=" + obj);
235
236     it = (JtIterator) main.getValue ("list", "iterator");
237
238     for (;;) {
239       obj = (Object JavaDoc) main.sendMessage (it, new JtMessage ("JtNEXT"));
240       if (obj == null)
241          break;
242       System.out.println (obj);
243     }
244
245
246     // Clear the list
247

248     main.setValue ("message", "msgId", "JtCLEAR");
249     main.sendMessage ("list", "message");
250     main.removeObject ("list");
251   }
252
253 }
254
255
256
Popular Tags