KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > st > ata > util > HashtableAList


1
2 package st.ata.util;
3
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.Serializable JavaDoc;
7 import java.util.Arrays JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.Hashtable JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.NoSuchElementException JavaDoc;
12
13
14 // Tested by TestHashtableAList
15

16 /** Implementation of {@link AList} using simple hashtable. */
17 @SuppressWarnings JavaDoc({"serial", "unchecked"})
18 public class HashtableAList implements MutableAList, Serializable JavaDoc {
19     private final Hashtable JavaDoc mTable = new Hashtable JavaDoc();
20
21     private static class DateArray {
22         public Date JavaDoc[] values;
23         public DateArray(Date JavaDoc[] v) { values = v; }
24         public boolean equals(Object JavaDoc obj) {
25             if (! (obj instanceof DateArray)) return false;
26             return Arrays.equals(values, ((DateArray)obj).values);
27         }
28     }
29
30
31     /** Remove all key-value mappings. */
32     public void clear() {
33         close();
34         mTable.clear();
35     }
36
37     public boolean containsKey(String JavaDoc key) {
38         return mTable.containsKey(key);
39     }
40
41     /**
42      * Deep Clone.
43      *
44      * Limited implementation
45      * @return The cloned object.
46      */

47     public Object JavaDoc clone() {
48         HashtableAList copy = new HashtableAList();
49         String JavaDoc[] keys = getKeyArray();
50         for (int i=0; i<keys.length; i++) {
51             Object JavaDoc 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 JavaDoc[]) {
70                 String JavaDoc[] from = (String JavaDoc[])me;
71                 String JavaDoc[] copyA = new String JavaDoc[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 JavaDoc) {
77                 copy.putObject(keys[i], new Long JavaDoc(((Long JavaDoc)me).longValue()));
78             } else if (me instanceof String JavaDoc) {
79                 copy.putObject(keys[i], me);
80             } else
81                 X.noimpl();
82         }
83         return copy;
84     }
85
86     /**
87      * Shallow copy of fields of <code>other</code> into <code>this</code>.
88      * @param other AList to copy from.
89      */

90     public void copyFrom(AList other) {
91         Iterator JavaDoc keys = other.getKeys();
92         while (keys.hasNext()) {
93             String JavaDoc key = (String JavaDoc)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 JavaDoc keys, AList other) {
141         for (; keys.hasNext();) {
142             String JavaDoc key = (String JavaDoc)keys.next();
143             Object JavaDoc value = other.getObject(key);
144             // TODO: consider shallow or deep copy in some cases?
145
// perhaps controlled by a third parameter?
146
if(value!=null) {
147                 putObject(key,value);
148             }
149         }
150     }
151     
152     public Object JavaDoc getObject(String JavaDoc key) {
153         return mTable.get(key);
154     }
155
156     public void putObject(String JavaDoc key, Object JavaDoc val) {
157         mTable.put(key, val);
158     }
159     public void remove(String JavaDoc key) {
160         mTable.remove(key);
161     }
162     public Iterator JavaDoc getKeys() {
163         return mTable.keySet().iterator();
164     }
165
166     public String JavaDoc[] getKeyArray() {
167         int i = 0;
168         String JavaDoc keys[] = new String JavaDoc[mTable.size()];
169         for(Iterator JavaDoc it = getKeys(); it.hasNext(); ++i)
170             keys[i] = (String JavaDoc)it.next();
171         return keys;
172     }
173
174     public int getInt(String JavaDoc key) {
175         Integer JavaDoc v = (Integer JavaDoc)mTable.get(key);
176         if (v == null) throw new NoSuchElementException JavaDoc(key);
177         return v.intValue();
178     }
179
180     public long getLong(String JavaDoc key) {
181         Long JavaDoc v = (Long JavaDoc)mTable.get(key);
182         if (v == null) throw new NoSuchElementException JavaDoc(key);
183         return v.longValue();
184     }
185
186     public String JavaDoc getString(String JavaDoc key) {
187         String JavaDoc v = (String JavaDoc)mTable.get(key);
188         if (v == null) throw new NoSuchElementException JavaDoc(key);
189         return v;
190     }
191
192     public AList getAList(String JavaDoc key) {
193         AList a = (AList) mTable.get(key);
194         if (a == null) throw new NoSuchElementException JavaDoc(key);
195         return a;
196     }
197
198     public Date JavaDoc getDate(String JavaDoc key) {
199         Date JavaDoc v = (Date JavaDoc)mTable.get(key);
200         if (v == null) throw new NoSuchElementException JavaDoc(key);
201         return v;
202     }
203
204     public InputStream JavaDoc getInputStream(String JavaDoc key) {
205         InputStream JavaDoc v = (InputStream JavaDoc)mTable.get(key);
206         if (v == null) throw new NoSuchElementException JavaDoc(key);
207         return v;
208     }
209
210     public int[] getIntArray(String JavaDoc key) {
211         int[] a = (int[]) mTable.get(key);
212         if (a == null) throw new NoSuchElementException JavaDoc(key);
213         return a;
214     }
215
216     public long[] getLongArray(String JavaDoc key) {
217         long[] a = (long[]) mTable.get(key);
218         if (a == null) throw new NoSuchElementException JavaDoc(key);
219         return a;
220     }
221
222     public String JavaDoc[] getStringArray(String JavaDoc key) {
223         String JavaDoc[] a = (String JavaDoc[]) mTable.get(key);
224         if (a == null) throw new NoSuchElementException JavaDoc(key);
225         return a;
226     }
227
228     public AList[] getAListArray(String JavaDoc key) {
229         AList[] a = (AList[]) mTable.get(key);
230         if (a == null) throw new NoSuchElementException JavaDoc(key);
231         return a;
232     }
233
234     public Date JavaDoc[] getDateArray(String JavaDoc key) {
235         DateArray a = (DateArray) mTable.get(key);
236         if (a == null) throw new NoSuchElementException JavaDoc(key);
237         return a.values;
238     }
239
240     public InputStream JavaDoc[] getInputStreamArray(String JavaDoc key) {
241         InputStream JavaDoc v[] = (InputStream JavaDoc [])mTable.get(key);
242         if (v == null) throw new NoSuchElementException JavaDoc(key);
243         return v;
244     }
245
246     public String JavaDoc[][] getStringArrayArray(String JavaDoc key) {
247         String JavaDoc[][] a = (String JavaDoc[][]) mTable.get(key);
248         if (a == null) throw new NoSuchElementException JavaDoc(key);
249         return a;
250     }
251
252
253     public void putInt(String JavaDoc key, int value) {
254         mTable.put(key, new Integer JavaDoc(value));
255     }
256
257     public void putLong(String JavaDoc key, long value) {
258         mTable.put(key, new Long JavaDoc(value));
259     }
260
261     public void putString(String JavaDoc key, String JavaDoc value) {
262         mTable.put(key, value);
263     }
264
265     public void putAList(String JavaDoc key, AList value) {
266         mTable.put(key, value);
267     }
268
269     public void putDate(String JavaDoc key, Date JavaDoc value) {
270         mTable.put(key, value);
271     }
272
273     public void putInputStream(String JavaDoc key, InputStream JavaDoc value) {
274         mTable.put(key, value);
275     }
276
277     public void putIntArray(String JavaDoc key, int[] value) {
278         mTable.put(key, value);
279     }
280
281     public void putLongArray(String JavaDoc key, long[] value) {
282         mTable.put(key, value);
283     }
284
285     public void putStringArray(String JavaDoc key, String JavaDoc[] value) {
286         mTable.put(key, value);
287     }
288
289     public void putAListArray(String JavaDoc key, AList[] value) {
290         mTable.put(key, value);
291     }
292
293     public void putDateArray(String JavaDoc key, Date JavaDoc[] value) {
294         mTable.put(key, new DateArray(value));
295     }
296
297     public void putInputStreamArray(String JavaDoc key, InputStream JavaDoc[] value) {
298         mTable.put(key, value);
299     }
300
301     public void putStringArrayArray(String JavaDoc key, String JavaDoc[][] value) {
302         mTable.put(key, value);
303     }
304
305
306     /** Deep equals. Arrays need to have same values in same order to
307      * be considered equal.
308      * @param obj
309      * @return True if equals.
310      */

311     public boolean equals(Object JavaDoc obj) {
312         if (! (obj instanceof HashtableAList)) return false;
313         HashtableAList o = (HashtableAList)obj;
314         for (Iterator JavaDoc i = o.getKeys(); i.hasNext(); ) {
315             if (mTable.get(i.next()) == null) return false;
316         }
317         for (Iterator JavaDoc i = getKeys(); i.hasNext(); ) {
318             Object JavaDoc k = i.next();
319             Object JavaDoc v1 = mTable.get(k);
320             Object JavaDoc 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 JavaDoc[])v1, (Object JavaDoc[])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 JavaDoc[]) {
333                     if (! (v2 instanceof String JavaDoc[])) return false;
334                     if (! Arrays.equals((String JavaDoc[])v1, (String JavaDoc[])v2))
335                         return false;
336                 } else return false;
337             }
338         }
339         return true;
340     }
341
342     public int getType(String JavaDoc key) {
343         Object JavaDoc 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 JavaDoc) return T_DATE;
347         else if (o instanceof Integer JavaDoc) return T_INT;
348         else if (o instanceof Long JavaDoc) return T_LONG;
349         else if (o instanceof String JavaDoc) return T_STRING;
350         else if (o instanceof InputStream JavaDoc) 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 JavaDoc[]) return T_STRING | F_ARRAY;
356         else if (o instanceof InputStream JavaDoc[]) return T_INPUTSTREAM | F_ARRAY;
357         else if (o instanceof String JavaDoc[][]) return T_STRING | F_ARRAY_ARRAY;
358         else if (o instanceof Object JavaDoc[]) return T_OBJECT | F_ARRAY;
359         else if (o instanceof Object JavaDoc) return T_OBJECT;
360         else X.fail("Should not get here " + o);
361         return -1;
362     }
363
364     /** Useful for creating test-tables for debugging. The object
365         should be one of an {@link AList}, {@link Date}, {@link
366         Integer}, {@link Long}, {@link String}, {@link AList}[],
367         {@link Date}[], <code>int[]</code>, <code>long[]</code>,
368         <code>{@link String}[]</code>, <code>ZE[]</code>
369         or <code>ZE[][]</code>. In the case of <code>ZE[]</code>,
370         the entry is treated as an {@link AList}. Similaryly
371         if the entry is <code>ZE[][]</code> it is treated as
372         {@link AList}[]. */

373     public static class ZE {
374         public final String JavaDoc key;
375         public final Object JavaDoc val;
376         public ZE(String JavaDoc k, Object JavaDoc 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 JavaDoc[]) {
387             mTable.put(entry.key, new DateArray((Date JavaDoc[])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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc e) {
426             throw X.toRTE(e);
427         }
428     }
429
430     public AList newAList() {
431         return new HashtableAList();
432     }
433
434     public String JavaDoc toString() {
435         return mTable.toString();
436     }
437 }
438
Popular Tags