KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtQueue


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  * Implements a queue of objects.
11  */

12
13 public class JtQueue extends JtList {
14
15   public JtQueue() {
16   }
17
18
19   
20   private Object JavaDoc test () {
21
22     JtMessage msg;
23     Object JavaDoc obj;
24
25
26    // Enqueue couple of objects
27

28     msg = new JtMessage ("JtENQUEUE");
29     msg.setMsgContent (new Integer JavaDoc (1));
30     System.out.println ("Enqueue object ... " + new Integer JavaDoc (1));
31     sendMessage (this, msg);
32
33
34     msg = new JtMessage ("JtENQUEUE");
35     msg.setMsgContent (new Integer JavaDoc (2));
36     System.out.println ("Enqueue object ... " + new Integer JavaDoc (2));
37     sendMessage (this, msg);
38
39
40     // Dequeue all the elements in the queue
41

42     for (;;) {
43       obj = (Object JavaDoc) sendMessage (this, new JtMessage ("JtDEQUEUE"));
44       if (obj == null)
45          break;
46       System.out.println ("Dequeue object ... " + obj);
47     }
48     return (this);
49
50   }
51   
52   /**
53     * Process object messages.
54     * <ul>
55     * <li> JtENQUEUE - Enqueues the object specified by msgContent
56     * <li> JtDEQUEUE - Dequeues and returns the object at the beginning of the queue.
57     * </ul>
58     * @param message Jt Message
59     */

60
61   public Object JavaDoc processMessage (Object JavaDoc message) {
62
63    String JavaDoc msgid = null;
64    JtMessage e = (JtMessage) message;
65    Object JavaDoc content;
66    Object JavaDoc data;
67    JtMessage tmp;
68
69
70      if (e == null)
71     return null;
72
73      msgid = (String JavaDoc) e.getMsgId ();
74
75      if (msgid == null)
76     return null;
77
78      content = e.getMsgContent();
79
80
81      // Remove this object
82
if (msgid.equals ("JtREMOVE")) {
83
84        return (null);
85      }
86
87      // Enqueue message
88
if (msgid.equals ("JtENQUEUE")) {
89         // Add an object to the queue
90

91         tmp = new JtMessage ("JtADD");
92         tmp.setMsgContent (content);
93         return (super.processMessage (tmp));
94      }
95
96
97      // Dequeue message
98

99      if (msgid.equals ("JtDEQUEUE")) {
100
101
102         return (super.processMessage (new JtMessage ("JtREMOVE_FIRST")));
103      }
104
105
106
107      // Test this object
108
if (msgid.equals ("JtTEST")) {
109
110        return (test ());
111      }
112          
113      handleError ("JtQueue.processMessage: invalid message id:" + msgid);
114      return (null);
115
116   }
117
118  
119   /**
120     * Unit tests the messages processed by JtQueue.
121     */

122
123   public static void main(String JavaDoc[] args) {
124
125     JtObject main = new JtObject ();
126     JtQueue queue;
127
128     Object JavaDoc obj;
129
130
131
132     // Create a Queue
133

134     queue = (JtQueue) main.createObject ("Jt.JtQueue", "queue");
135
136     main.sendMessage (queue, new JtMessage ("JtTEST"));
137
138     main.removeObject (queue);
139   }
140
141 }
142
143
144
Free Books   Free Magazines  
Popular Tags