KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtIterator


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   * Iterator over the elements of a Jt collection.
11   */

12
13
14 public class JtIterator extends JtObject {
15   //private Hashtable col = null;// Object table
16
//private HashMap col = null;
17
Iterator iterator;
18   JtCollection col;
19
20
21   public JtIterator() {
22   }
23
24
25
26   /**
27     * Void operation.
28     */

29
30   public void setIterator (Iterator iterator) {
31      this.iterator = iterator; // void operation
32
}
33
34
35   /**
36    * Returns the Java Iterator.
37    */

38
39   public Iterator getIterator () {
40      return (iterator);
41   }
42
43
44   // next: next element
45

46   private Object JavaDoc next () {
47     if (iterator == null) // check
48
return (null);
49     if (iterator.hasNext()) {
50       return (iterator.next ());
51     }
52
53     return (null);
54   }
55
56   /**
57     * Process object messages.
58     * <ul>
59     * <li>JtNEXT - returns the next element in the iteration
60     * </ul>
61     */

62
63   public Object JavaDoc processMessage (Object JavaDoc event) {
64
65    String JavaDoc msgid = null;
66    JtMessage e = (JtMessage) event;
67    Object JavaDoc content;
68    Object JavaDoc data;
69
70
71      if (e == null)
72     return null;
73
74      msgid = (String JavaDoc) e.getMsgId ();
75
76      if (msgid == null)
77     return null;
78
79      content = e.getMsgContent();
80      data = e.getMsgData ();
81
82      // Remove this object
83
if (msgid.equals ("JtREMOVE")) {
84        return (null);
85      }
86
87      if (msgid.equals ("JtNEXT")) {
88        return (next ());
89      }
90
91      return (super.processMessage (event));
92      
93      //handleError ("JtCollection.processMessage: invalid message id:" + msgid);
94
//return (null);
95

96   }
97
98  
99   /**
100     * Unit tests the messages processed by JtIterator.
101     */

102
103   public static void main(String JavaDoc[] args) {
104
105     JtObject main = new JtObject ();
106     JtMessage msg, msg1;
107     Integer JavaDoc count;
108     JtIterator it;
109     Object JavaDoc obj;
110
111     //main.setObjTrace (1);
112
//main.setLogFile ("log.txt");
113

114
115     // Create a JtColletion
116

117     main.createObject ("Jt.JtCollection", "collection");
118
119
120     msg = (JtMessage) main.createObject ("Jt.JtMessage", "message");
121     main.setValue (msg, "msgId", "JtADD");
122     main.setValue (msg, "msgContent", new Integer JavaDoc (1));
123
124     // Add objects to the collection
125

126     main.sendMessage ("collection", msg);
127
128     // Add another object to the collection
129

130     main.setValue (msg, "msgId", "JtADD");
131     main.setValue (msg, "msgContent", new Integer JavaDoc (2));
132     main.sendMessage ("collection", msg);
133    
134     it = (JtIterator) main.getValue ("collection", "iterator");
135
136
137     main.setValue (msg, "msgId", "JtNEXT");
138
139     while ((obj = main.sendMessage (it, msg)) != null)
140       System.out.println ("Object=" + obj);
141
142     main.removeObject ("collection");
143
144
145
146   }
147
148 }
149
150
151
Popular Tags