1 2 package st.ata.util; 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.io.Serializable ; 7 import java.util.Arrays ; 8 import java.util.Date ; 9 import java.util.Hashtable ; 10 import java.util.Iterator ; 11 import java.util.NoSuchElementException ; 12 13 14 16 17 @SuppressWarnings ({"serial", "unchecked"}) 18 public class HashtableAList implements MutableAList, Serializable { 19 private final Hashtable mTable = new Hashtable (); 20 21 private static class DateArray { 22 public Date [] values; 23 public DateArray(Date [] v) { values = v; } 24 public boolean equals(Object obj) { 25 if (! (obj instanceof DateArray)) return false; 26 return Arrays.equals(values, ((DateArray)obj).values); 27 } 28 } 29 30 31 32 public void clear() { 33 close(); 34 mTable.clear(); 35 } 36 37 public boolean containsKey(String key) { 38 return mTable.containsKey(key); 39 } 40 41 47 public Object clone() { 48 HashtableAList copy = new HashtableAList(); 49 String [] keys = getKeyArray(); 50 for (int i=0; i<keys.length; i++) { 51 Object me=getObject(keys[i]); 52 if (me instanceof AList) 53 copy.putObject(keys[i], ((AList)me).clone()); 54 else if (me instanceof AList[]) { 55 AList[] from = (AList[])me; 56 int count=from.length; 57 for (int j=0; j<from.length; j++) { 58 if (from[j]==null) { 59 count--; 60 } 61 } 62 63 AList[] copyAList = new AList[count]; 64 for (int j=0; j<count; j++) { 65 if (from[j]==null) continue; 66 copyAList[j]=(AList)from[j].clone(); 67 } 68 copy.putObject(keys[i], copyAList); 69 } else if (me instanceof String []) { 70 String [] from = (String [])me; 71 String [] copyA = new String [from.length]; 72 for (int j=0; j<from.length; j++) 73 copyA[j]=from[j]; 74 copy.putObject(keys[i], copyA); 75 } 76 else if (me instanceof Long ) { 77 copy.putObject(keys[i], new Long (((Long )me).longValue())); 78 } else if (me instanceof String ) { 79 copy.putObject(keys[i], me); 80 } else 81 X.noimpl(); 82 } 83 return copy; 84 } 85 86 90 public void copyFrom(AList other) { 91 Iterator keys = other.getKeys(); 92 while (keys.hasNext()) { 93 String key = (String )keys.next(); 94 switch (other.getType(key)) { 95 case T_ALIST: 96 putAList(key, other.getAList(key)); 97 break; 98 case T_DATE: 99 putDate(key, other.getDate(key)); 100 break; 101 case T_INT: 102 putInt(key, other.getInt(key)); 103 break; 104 case T_LONG: 105 putLong(key, other.getLong(key)); 106 break; 107 case T_STRING: 108 putString(key, other.getString(key)); 109 break; 110 case T_INPUTSTREAM: 111 putInputStream(key, other.getInputStream(key)); 112 break; 113 case F_ARRAY | T_ALIST: 114 putAListArray(key, other.getAListArray(key)); 115 break; 116 case F_ARRAY | T_DATE: 117 putDateArray(key, other.getDateArray(key)); 118 break; 119 case F_ARRAY | T_INT: 120 putIntArray(key, other.getIntArray(key)); 121 break; 122 case F_ARRAY | T_LONG: 123 putLongArray(key, other.getLongArray(key)); 124 break; 125 case F_ARRAY | T_STRING: 126 putStringArray(key, other.getStringArray(key)); 127 break; 128 case F_ARRAY_ARRAY | T_STRING: 129 putStringArrayArray(key, other.getStringArrayArray(key)); 130 break; 131 case F_ARRAY | T_INPUTSTREAM: 132 putInputStreamArray(key, other.getInputStreamArray(key)); 133 break; 134 default: 135 X.fail("Unexpected case"); 136 } 137 } 138 } 139 140 public void copyKeysFrom(Iterator keys, AList other) { 141 for (; keys.hasNext();) { 142 String key = (String )keys.next(); 143 Object value = other.getObject(key); 144 if(value!=null) { 147 putObject(key,value); 148 } 149 } 150 } 151 152 public Object getObject(String key) { 153 return mTable.get(key); 154 } 155 156 public void putObject(String key, Object val) { 157 mTable.put(key, val); 158 } 159 public void remove(String key) { 160 mTable.remove(key); 161 } 162 public Iterator getKeys() { 163 return mTable.keySet().iterator(); 164 } 165 166 public String [] getKeyArray() { 167 int i = 0; 168 String keys[] = new String [mTable.size()]; 169 for(Iterator it = getKeys(); it.hasNext(); ++i) 170 keys[i] = (String )it.next(); 171 return keys; 172 } 173 174 public int getInt(String key) { 175 Integer v = (Integer )mTable.get(key); 176 if (v == null) throw new NoSuchElementException (key); 177 return v.intValue(); 178 } 179 180 public long getLong(String key) { 181 Long v = (Long )mTable.get(key); 182 if (v == null) throw new NoSuchElementException (key); 183 return v.longValue(); 184 } 185 186 public String getString(String key) { 187 String v = (String )mTable.get(key); 188 if (v == null) throw new NoSuchElementException (key); 189 return v; 190 } 191 192 public AList getAList(String key) { 193 AList a = (AList) mTable.get(key); 194 if (a == null) throw new NoSuchElementException (key); 195 return a; 196 } 197 198 public Date getDate(String key) { 199 Date v = (Date )mTable.get(key); 200 if (v == null) throw new NoSuchElementException (key); 201 return v; 202 } 203 204 public InputStream getInputStream(String key) { 205 InputStream v = (InputStream )mTable.get(key); 206 if (v == null) throw new NoSuchElementException (key); 207 return v; 208 } 209 210 public int[] getIntArray(String key) { 211 int[] a = (int[]) mTable.get(key); 212 if (a == null) throw new NoSuchElementException (key); 213 return a; 214 } 215 216 public long[] getLongArray(String key) { 217 long[] a = (long[]) mTable.get(key); 218 if (a == null) throw new NoSuchElementException (key); 219 return a; 220 } 221 222 public String [] getStringArray(String key) { 223 String [] a = (String []) mTable.get(key); 224 if (a == null) throw new NoSuchElementException (key); 225 return a; 226 } 227 228 public AList[] getAListArray(String key) { 229 AList[] a = (AList[]) mTable.get(key); 230 if (a == null) throw new NoSuchElementException (key); 231 return a; 232 } 233 234 public Date [] getDateArray(String key) { 235 DateArray a = (DateArray) mTable.get(key); 236 if (a == null) throw new NoSuchElementException (key); 237 return a.values; 238 } 239 240 public InputStream [] getInputStreamArray(String key) { 241 InputStream v[] = (InputStream [])mTable.get(key); 242 if (v == null) throw new NoSuchElementException (key); 243 return v; 244 } 245 246 public String [][] getStringArrayArray(String key) { 247 String [][] a = (String [][]) mTable.get(key); 248 if (a == null) throw new NoSuchElementException (key); 249 return a; 250 } 251 252 253 public void putInt(String key, int value) { 254 mTable.put(key, new Integer (value)); 255 } 256 257 public void putLong(String key, long value) { 258 mTable.put(key, new Long (value)); 259 } 260 261 public void putString(String key, String value) { 262 mTable.put(key, value); 263 } 264 265 public void putAList(String key, AList value) { 266 mTable.put(key, value); 267 } 268 269 public void putDate(String key, Date value) { 270 mTable.put(key, value); 271 } 272 273 public void putInputStream(String key, InputStream value) { 274 mTable.put(key, value); 275 } 276 277 public void putIntArray(String key, int[] value) { 278 mTable.put(key, value); 279 } 280 281 public void putLongArray(String key, long[] value) { 282 mTable.put(key, value); 283 } 284 285 public void putStringArray(String key, String [] value) { 286 mTable.put(key, value); 287 } 288 289 public void putAListArray(String key, AList[] value) { 290 mTable.put(key, value); 291 } 292 293 public void putDateArray(String key, Date [] value) { 294 mTable.put(key, new DateArray(value)); 295 } 296 297 public void putInputStreamArray(String key, InputStream [] value) { 298 mTable.put(key, value); 299 } 300 301 public void putStringArrayArray(String key, String [][] value) { 302 mTable.put(key, value); 303 } 304 305 306 311 public boolean equals(Object obj) { 312 if (! (obj instanceof HashtableAList)) return false; 313 HashtableAList o = (HashtableAList)obj; 314 for (Iterator i = o.getKeys(); i.hasNext(); ) { 315 if (mTable.get(i.next()) == null) return false; 316 } 317 for (Iterator i = getKeys(); i.hasNext(); ) { 318 Object k = i.next(); 319 Object v1 = mTable.get(k); 320 Object v2 = o.mTable.get(k); 321 if (! v1.equals(v2)) { 322 if (v1 instanceof AList[]) { 323 if (! (v2 instanceof AList[])) return false; 324 if (! Arrays.equals((Object [])v1, (Object [])v2)) 325 return false; 326 } else if (v1 instanceof int[]) { 327 if (! (v2 instanceof int[])) return false; 328 if (! Arrays.equals((int[])v1, (int[])v2)) return false; 329 } else if (v1 instanceof long[]) { 330 if (! (v2 instanceof long[])) return false; 331 if (! Arrays.equals((long[])v1, (long[])v2)) return false; 332 } else if (v1 instanceof String []) { 333 if (! (v2 instanceof String [])) return false; 334 if (! Arrays.equals((String [])v1, (String [])v2)) 335 return false; 336 } else return false; 337 } 338 } 339 return true; 340 } 341 342 public int getType(String key) { 343 Object o = mTable.get(key); 344 if (o == null) return T_UNDEFINED; 345 else if (o instanceof AList) return T_ALIST; 346 else if (o instanceof Date ) return T_DATE; 347 else if (o instanceof Integer ) return T_INT; 348 else if (o instanceof Long ) return T_LONG; 349 else if (o instanceof String ) return T_STRING; 350 else if (o instanceof InputStream ) return T_INPUTSTREAM; 351 else if (o instanceof AList[]) return T_ALIST | F_ARRAY; 352 else if (o instanceof DateArray) return T_DATE | F_ARRAY; 353 else if (o instanceof int[]) return T_INT | F_ARRAY; 354 else if (o instanceof long[]) return T_LONG | F_ARRAY; 355 else if (o instanceof String []) return T_STRING | F_ARRAY; 356 else if (o instanceof InputStream []) return T_INPUTSTREAM | F_ARRAY; 357 else if (o instanceof String [][]) return T_STRING | F_ARRAY_ARRAY; 358 else if (o instanceof Object []) return T_OBJECT | F_ARRAY; 359 else if (o instanceof Object ) return T_OBJECT; 360 else X.fail("Should not get here " + o); 361 return -1; 362 } 363 364 373 public static class ZE { 374 public final String key; 375 public final Object val; 376 public ZE(String k, Object v) { key = k; val = v; } 377 } 378 379 public void zInsert(ZE[] entries) { 380 for (int i = 0; i < entries.length; i++) { 381 zInsert(entries[i]); 382 } 383 } 384 385 public void zInsert(ZE entry) { 386 if (entry.val instanceof Date []) { 387 mTable.put(entry.key, new DateArray((Date [])entry.val)); 388 } else if (entry.val instanceof ZE[]) { 389 HashtableAList v = new HashtableAList(); 390 v.zInsert((ZE[])entry.val); 391 mTable.put(entry.key, v); 392 } else if (entry.val instanceof ZE[][]) { 393 AList v[] = new AList[((ZE[][])entry.val).length]; 394 for(int j = 0; j < v.length; ++j) { 395 HashtableAList h = new HashtableAList(); 396 h.zInsert(((ZE[][])entry.val)[j]); 397 v[j] = h; 398 } 399 mTable.put(entry.key, v); 400 } else { 401 mTable.put(entry.key, entry.val); 402 } 403 } 404 405 public void close() { 406 String [] keys = getKeyArray(); 407 try { 408 for (int i = 0; i < keys.length; i++) { 409 if (getType(keys[i]) == T_INPUTSTREAM) { 410 getInputStream(keys[i]).close(); 411 } else if (getType(keys[i]) == (T_INPUTSTREAM | F_ARRAY)) { 412 InputStream [] ins = getInputStreamArray(keys[i]); 413 for (int j = 0; j < ins.length; j++) { 414 ins[j].close(); 415 } 416 } else if (getType(keys[i]) == T_ALIST) { 417 getAList(keys[i]).close(); 418 } else if (getType(keys[i]) == (T_ALIST | F_ARRAY)) { 419 AList[] als = getAListArray(keys[i]); 420 for (int j = 0; j < als.length; j++) { 421 als[j].close(); 422 } 423 } 424 } 425 } catch (IOException e) { 426 throw X.toRTE(e); 427 } 428 } 429 430 public AList newAList() { 431 return new HashtableAList(); 432 } 433 434 public String toString() { 435 return mTable.toString(); 436 } 437 } 438 | Popular Tags |