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 JtHashTable extends JtObject { 14 15 protected HashMap hashmap = null; 16 private int size = 0; 17 18 19 public JtHashTable() { 20 } 21 22 23 26 27 public void setSize (int size) { 28 this.size = this.size; } 30 31 34 35 public int getSize () { 36 return (hashmap != null ? hashmap.size (): 0); 37 } 38 39 40 43 44 public void setHashmap (HashMap hashmap) { 45 this.hashmap = hashmap; } 47 48 51 52 public HashMap getHashmap () { 53 return (hashmap); 54 } 55 56 57 60 61 public Object getIterator () { 62 JtIterator jit; 63 Collection values; 64 65 jit = new JtIterator (); 66 67 if (hashmap == null) 68 return (null); 69 70 values = hashmap.values (); 71 72 if (values == null) 73 return (null); 74 jit.setIterator(values.iterator ()); 75 76 return (jit); 77 } 78 79 80 81 83 private void broadcast_message (JtMessage msg) 84 { 85 Collection values; 86 Iterator it; 87 88 if (msg == null || hashmap == null) 89 return; 90 91 values = hashmap.values (); 92 if (values == null) 93 return; 94 it = values.iterator (); 95 96 while (it.hasNext()) { 97 sendMessage (it.next (), msg); 98 } 99 100 } 101 102 114 115 public Object processMessage (Object message) { 116 117 String msgid = null; 118 JtMessage e = (JtMessage) message; 119 Object content; 120 Object data; 121 122 123 if (e == null) 124 return null; 125 126 msgid = (String ) e.getMsgId (); 127 128 if (msgid == null) 129 return null; 130 131 content = e.getMsgContent(); 132 data = e.getMsgData (); 133 134 if (msgid.equals ("JtREMOVE")) { 136 return (null); 137 } 138 139 if (msgid.equals ("JtPUT")) { 140 142 if (content == null) { 143 handleWarning 144 ("JtHashTable.processMessage(JtPUT):adding null value"); 145 147 } 148 if (hashmap == null) 149 hashmap = new HashMap (); 150 152 hashmap.put (data, content); 154 return (this); 155 } 156 157 158 if (msgid.equals ("JtGET")) { 159 160 161 if (data == null) { 162 handleWarning 163 ("JtHashTable.processMessage(JtGET):invalid value (null)"); 164 return (this); 165 166 } 167 if (hashmap == null) 168 return (null); 169 170 171 return (hashmap.get (data)); 172 173 } 174 175 176 if (msgid.equals ("JtCLEAR")) { 177 178 if (hashmap != null) { 179 hashmap.clear (); 180 } 181 size = 0; 182 183 return (this); 184 } 185 186 189 if (msgid.equals ("JtBROADCAST")) { 190 191 if (hashmap == null) { 192 return (this); 193 } 194 195 broadcast_message ((JtMessage) content); 196 197 return (this); 198 } 199 200 handleError ("JtHashTable.processMessage: invalid message id:" + msgid); 201 return (null); 202 203 } 204 205 206 207 210 211 public static void main(String [] args) { 212 213 JtObject main = new JtObject (); 214 JtMessage msg, msg1; 215 Integer count; 216 217 220 221 223 main.createObject ("Jt.JtHashTable", "hashtable"); 224 225 226 msg = (JtMessage) main.createObject ("Jt.JtMessage", "message"); 227 main.setValue ("message", "msgId", "JtPUT"); 228 main.setValue ("message", "msgContent", new Integer (1)); 229 main.setValue ("message", "msgData", "one"); 230 231 232 234 235 main.sendMessage ("hashtable", "message"); 236 237 main.setValue ("message", "msgId", "JtGET"); 238 main.setValue ("message", "msgData", "one"); 239 240 System.err.println ("JtHashTable(JtGET):" + main.sendMessage ("hashtable", "message")); 241 242 243 count = (Integer ) main.getValue ("hashtable", "size"); 244 245 if (count.intValue () == 1) 246 System.err.println ("JtHashTable(JtPUT):GO"); 247 else 248 System.err.println ("JtHashTable:FAILED"); 249 250 252 main.setValue ("message", "msgId", "JtCLEAR"); 253 main.sendMessage ("hashtable", "message"); 254 255 count = (Integer ) main.getValue ("hashtable", "size"); 256 257 if (count.intValue() == 0) 258 System.err.println ("JtHashTable (JtCLEAR):GO"); 259 else 260 System.err.println ("JtHashTable:FAILED"); 261 262 msg1 = (JtMessage) main.createObject ("Jt.JtMessage", "message1"); 263 main.setValue ("message1", "msgId", "JtOBJECT"); 264 265 main.setValue ("message", "msgId", "JtBROADCAST"); 266 main.setValue ("message", "msgContent", msg1); 267 268 main.sendMessage ("hashtable", "message"); 269 main.removeObject ("hashtable"); 270 271 272 } 273 274 } 275 276 277 | Popular Tags |