1 2 3 package Jt; 4 import java.util.*; 5 import java.lang.reflect.*; 6 import java.beans.*; 7 import java.io.*; 8 9 12 13 public class JtCollection extends JtObject { 14 private HashMap col = null; 16 private int size = 0; 17 18 19 public JtCollection() { 20 } 21 22 25 26 public void setSize (int size) { 27 this.size = this.size; } 29 30 33 34 public int getSize () { 35 return (size); 36 } 37 38 41 42 public Object 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 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 94 95 public Object processMessage (Object event) { 96 97 String msgid = null; 98 JtMessage e = (JtMessage) event; 99 Object content; 100 Object data; 101 102 103 if (e == null) 104 return null; 105 106 msgid = (String ) e.getMsgId (); 107 108 if (msgid == null) 109 return null; 110 111 content = e.getMsgContent(); 112 114 if (msgid.equals ("JtREMOVE")) { 116 return (null); 117 } 118 119 if (msgid.equals ("JtCOLLECTION_ADD") || msgid.equals ("JtADD")) { 120 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 132 size++; 133 col.put (content, content); 134 return (this); 135 } 136 137 138 139 if (msgid.equals ("JtOBJECT") || msgid.equals ("JtMESSAGE")) { 140 142 if (content == null) 143 return (this); 144 if (col == null) 145 col = new HashMap (); 146 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 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 186 187 public static void main(String [] args) { 188 189 JtObject main = new JtObject (); 190 JtMessage msg, msg1; 191 Integer count; 192 193 196 197 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 ) main.getValue ("collection", "size"); 206 207 209 main.sendMessage ("collection", msg); 210 211 212 count = (Integer ) 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 221 main.setValue (msg, "msgId", "JtCLEAR"); 222 main.sendMessage ("collection", msg); 223 224 count = (Integer ) 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
| Popular Tags
|