1 package org.masukomi.tools.utils;
2
3 import java.io.*;
4
5 import java.sql.Clob;
6 import java.sql.Date;
7
8 import java.util.*;
9
10
11 /***
12 * A class the simplifies working with Hashtables of Hashtables by allowing the
13 * user to work directly on the sub-Hashtables.
14 *
15 * <p>
16 * Over Time this class has grown and is now beginning to accomidate things
17 * other than Hashtables. As a result new getXXX methods are being added as
18 * needed. Eventually I plan on adding complete Hashtable of Vector support
19 * too, but I'll probably have to rename it at that point.
20 * </p>
21 *
22 * <p>
23 * To Be Done The .toString() methods need to be improved to handle non-String
24 * objects
25 * </p>
26 *
27 * <p>
28 * This software is copyright 2002 Kate Rhodes. <br>
29 * It is distributed under the MIT License which can be found at <a href="http://www.opensource.org/licenses/mit-license.php">http://www.opensource.org/licenses/mit-license.php</a><br>
30 * Details on this package can be found at <a href="http://tools.masukomi.org/">http://tools.masukomi.org/</a><br>
31 * e-mail <tt>masukomi at masukomi dot org</tt> for questions or details
32 * </p>
33 */
34 public class HashtableOfHashtables implements Serializable {
35 /*** DOCUMENT ME! */
36 private Hashtable rootHashtable;
37
38 /***
39 * Constructs a new, empty hashtable of hashtables with a default initial
40 * capacity (11) and load factor, which is 0.75.
41 */
42 public HashtableOfHashtables() {
43 rootHashtable = new Hashtable();
44 }
45
46 /***
47 * Constructs a new, empty hashtable of hashtables with the specified
48 * initial capacity and default load factor, which is 0.75.
49 *
50 * @param initialCapacity the initial capacity of the root hashtable
51 */
52 public HashtableOfHashtables(int initialCapacity) {
53 rootHashtable = new Hashtable(initialCapacity);
54 }
55
56 /***
57 * Constructs a new, empty hashtable of hashtables with the specified
58 * initial capacity and the specified load factor.
59 *
60 * @param initialCapacity the initial capacity of the root hashtable
61 * @param loadFactor the load factor of the root hashtable
62 */
63 public HashtableOfHashtables(int initialCapacity, float loadFactor) {
64 rootHashtable = new Hashtable(initialCapacity, loadFactor);
65 }
66
67 /***
68 * Constructs a new hashtable of hashtables with the same mappings as the
69 * given Map.
70 *
71 * @param theMap the Map you wish to use in creation of the root Hashtable
72 */
73 public HashtableOfHashtables(Map theMap) {
74 rootHashtable = new Hashtable(theMap);
75 }
76
77 /***
78 * Constructs a new hashtable of hashtables with the rootHashtable set to a
79 * deep copy of the passed Hashtable.
80 *
81 * @param starterHashtable A Hashtable you wish to copy and use as the root
82 * Hashtable
83 */
84 public HashtableOfHashtables(Hashtable starterHashtable) {
85 rootHashtable = new Hashtable();
86
87 Enumeration keysEnum = starterHashtable.keys();
88
89 while ((keysEnum != null) && keysEnum.hasMoreElements()) {
90 Object currentKey = keysEnum.nextElement();
91 rootHashtable.put(currentKey, starterHashtable.get(currentKey));
92 }
93
94 //whee :)
95 }
96
97 /***
98 * removes <i>All</i> entries in the rootHashtable. This, of course,
99 * includes the subHashtables
100 */
101 public void clear() {
102 rootHashtable.clear();
103 }
104
105 /***
106 * Clears the subHashtable specified by key so that it contains no keys.
107 *
108 * @param key the key of the sub Hashtable you wish to clear
109 */
110 public void clear(Object key) {
111 Hashtable subHashtable = get(key);
112
113 if (subHashtable != null) {
114 subHashtable.clear();
115 }
116 }
117
118 /***
119 * Creates a shallow copy of this hashtable.
120 *
121 * @return a shallow copy of the root hashtable
122 */
123 public Object clone() {
124 return rootHashtable.clone();
125 }
126
127 /***
128 * Creates a shallow copy of this subHashtable.
129 *
130 * @param key the key for the sub Hashtable you wish a shallow copy of
131 *
132 * @return a shallow copy of the specified sub Hashtable
133 */
134 public Object clone(Object key) {
135 Hashtable subHashtable = get(key);
136
137 return subHashtable.clone();
138 }
139
140 /***
141 * Tests if some key maps into the specified value in the root Hashtable
142 *
143 * @param value the value you wish to test for in the root hashtable
144 *
145 * @return true if the value exists in the root Hashtable
146 */
147 public boolean contains(Object value) {
148 return rootHashtable.contains(value);
149 }
150
151 /***
152 * Tests if some key maps into the specified value in the subHashtable
153 * specified by key.
154 *
155 * @param key the key of the sub Hashtable you wish to test for value in
156 * @param value the value you wish to test for the presence of in the sub
157 * Hashtable
158 *
159 * @return true if the value exists in the specified Hashtable
160 */
161 public boolean contains(Object key, Object value) {
162 Hashtable subHashtable = get(key);
163
164 return subHashtable.contains(value);
165 }
166
167 /***
168 * Tests if some key maps into the specified value in this hashtable or any
169 * of the subHashtables. This can be an expensive operation depending on
170 * the size of this HashtableOfHashtables
171 *
172 * @param value the value you wish to test for the presence of
173 *
174 * @return true if the value exists in the root Hashtable or any of the sub
175 * Hashtables
176 */
177 public boolean containedAnywhere(Object value) {
178 if (value == null) {
179 value = this;
180 }
181
182 for (Enumeration enum = rootHashtable.keys(); enum.hasMoreElements();) {
183 try {
184 Hashtable subHashtable =
185 (Hashtable) rootHashtable.get(enum.nextElement());
186
187 if (subHashtable.contains(value)) {
188 return true;
189 }
190 } catch (Exception e) {
191 // just in case one of those sub elements isn't a Hashtable
192 }
193 }
194
195 return false;
196 }
197
198 /***
199 * Tests if the specified object is a key in the root Hashtable.
200 *
201 * @param key the key you wish to test for in the root Hashtable
202 *
203 * @return true if the root Hashtable contains the specified key
204 */
205 public boolean containsKey(Object key) {
206 if (key == null) {
207 return false;
208 }
209
210 return rootHashtable.containsKey(key);
211 }
212
213 /***
214 * Tests if the specified object is a key in the subHashtable specified by
215 * key.
216 *
217 * @param key the key of the sub Hashtable you wish to test for the subKey
218 * in
219 * @param subKey the key you wish to test for the presence of in the sub
220 * Hashtable specified by Key
221 *
222 * @return true if the subKey was found in the sub Hashtable specified by
223 * key
224 */
225 public boolean containsKey(Object key, Object subKey) {
226 if ((subKey == null) || (key == null)) {
227 return false;
228 }
229
230 Hashtable subHashtable = get(key);
231
232 if (subHashtable != null) {
233 return subHashtable.containsKey(subKey);
234 } else {
235 return false;
236 }
237
238 //return (subHashtable != null) ? subHashtable.containsKey(subKey) : false;
239 }
240
241 /***
242 * Returns true if this Hashtable maps one or more keys to this value. Note
243 * that this method is identical in functionality to contains (which
244 * predates the Map interface).
245 *
246 * @param value the value whose presence in the root Hashtable is to be
247 * tested.
248 *
249 * @return true if this map maps one or more keys to the specified value.
250 */
251 public boolean containsValue(Object value) {
252 return rootHashtable.containsValue(value);
253 }
254
255 /***
256 * Returns true if this Hashtable maps one or more keys to this value. Note
257 * that this method is identical in functionality to contains (which
258 * predates the Map interface).
259 *
260 * @param key the key of the subtable you wish to test for the value in.
261 * @param value the value whose presence in the sub Hashtable indicated by
262 * key is to be tested.
263 *
264 * @return true if this map maps one or more keys to the specified value.
265 */
266 public boolean containsValue(Object key, Object value) {
267 /*if ((value == null) || (key == null)) {
268 return false;
269 }*/
270
271 // I'd prefer it to throw the exception myself
272 Hashtable subHashtable = get(key);
273
274 if (subHashtable != null) {
275 return subHashtable.containsValue(key);
276 } else {
277 return false;
278 }
279 }
280
281 /***
282 * Returns an enumeration of the values in the root Hashtable. Use the
283 * Enumeration methods on the returned object to fetch the elements
284 * sequentially.
285 *
286 * @return an enumeration of the values in the root Hashtable.
287 */
288 public Enumeration elements() {
289 return rootHashtable.elements();
290 }
291
292 /***
293 * Returns an enumeration of the values in the root Hashtable. Use the
294 * Enumeration methods on the returned object to fetch the elements
295 * sequentially.
296 *
297 * @param key the key of the sub Hashtable you wish to get the values of
298 *
299 * @return an enumeration of the values in the root Hashtable.
300 */
301 public Enumeration elements(Object key) {
302 if (key == null) {
303 return null;
304 }
305
306 Hashtable subHashtable = get(key);
307
308 if (subHashtable != null) {
309 return subHashtable.elements();
310 } else {
311 return null;
312 }
313 }
314
315 /***
316 * Returns a Set view of the entries contained in the root Hashtable. Each
317 * element in this collection is a Map.Entry. The Set is backed by the
318 * Hashtable, so changes to the Hashtable are reflected in the Set, and
319 * vice-versa. The Set supports element removal (which removes the
320 * corresponding entry from the Hashtable), but not element addition.
321 *
322 * @return a set view of the mappings contained in the root Hashtable's
323 * map.
324 */
325 public Set entrySet() {
326 return rootHashtable.entrySet();
327 }
328
329 /***
330 * Returns a Set view of the entries contained in the subHashtable
331 * indicated by key.
332 *
333 * @param key the key of the sub Hashtable you wish to retreive a Set view
334 * of.
335 *
336 * @return a set view of the mappings contained in the sub Hashtable's map.
337 */
338 public Set entrySet(Object key) {
339 if (key == null) {
340 return null;
341 }
342
343 Hashtable subHashtable = get(key);
344
345 if (subHashtable != null) {
346 return subHashtable.entrySet();
347 } else {
348 return null;
349 }
350 }
351
352 /***
353 * Compares the specified Object with this Map for equality, as per the
354 * definition in the Map interface.
355 *
356 * @param anObject object to be compared for equality with the root
357 * Hashtable
358 *
359 * @return true if the specified Object is equal to the Root Hashtable's
360 * Map.
361 */
362 public boolean equals(Object anObject) {
363 return rootHashtable.equals(anObject);
364 }
365
366 /***
367 * Compares the specified Object with this Map for equality, as per the
368 * definition in the Map interface.
369 *
370 * @param key the key indicating the sub Hashtable you wish to test for
371 * equality
372 * @param anObject object to be compared for equality with the root
373 * Hashtable
374 *
375 * @return true if the specified Object is equal to the Root Hashtable's
376 * Map.
377 */
378 public boolean equals(Object key, Object anObject) {
379 if (key == null) {
380 return false;
381 }
382
383 Hashtable subHashtable = get(key);
384
385 if (subHashtable != null) {
386 return subHashtable.equals(anObject);
387 } else {
388 return false;
389 }
390 }
391
392 /***
393 * Returns the Hashtable indicated by key
394 *
395 * @param key the key of the subHashtable you wish to retreive
396 *
397 * @return the Hashtable indicated by key
398 */
399 public Hashtable get(Object key) {
400 if (key == null) {
401 return null;
402 }
403
404 return (Hashtable) rootHashtable.get(key);
405 }
406
407 /***
408 * Returns the value associated with the specified key in the root
409 * Hashtable
410 *
411 * @param key the key in the root Hashtable that coresponds to the value
412 * you wish to retreive
413 *
414 * @return an object from the rootHashtable that may or may not be a
415 * Hashtable. Just because it's called HashtableOfHashtables
416 * doesn't mean that's really all you can put in it if you're
417 * careful
418 */
419 public Object getObject(Object key) {
420 if (key == null) {
421 return null;
422 }
423
424 return rootHashtable.get(key);
425 }
426
427 /***
428 * Returns an object from the sub hashtable specified by key and associated
429 * with the the subKey key in the sub Hashtable
430 *
431 * @param key the key of the sub Hashtable
432 * @param subKey the key within the sub Hashtable
433 *
434 * @return the value to which the specified subKey is mapped in the
435 * subHashtable indicated by key. null if non-existent or null
436 * value was stored
437 */
438 public Object get(Object key, Object subKey) {
439 if ((subKey == null) || (key == null)) {
440 return null;
441 }
442
443 Hashtable tempHash = get(key);
444
445 if (tempHash.containsKey(subKey)) {
446 return tempHash.get(subKey);
447 } else {
448 return null;
449 }
450
451 //return (value == this) ? null : value;
452 }
453
454 /***
455 * Tries to return you a Boolean object. If the object stored is a Boolean
456 * it will just be cast from an Object to a Boolean and returned. If the
457 * Object is not a Boolean it will try to create a new Boolean from it and
458 * return that.
459 *
460 * @param key the key of the sub Hashtable
461 * @param subKey the key in the subtable of the object you wish to retreive
462 *
463 * @return the value of the subKey as a Boolean.
464 */
465 public Boolean getBoolean(Object key, Object subKey) {
466 try {
467 return (Boolean) get(key, subKey);
468 } catch (ClassCastException e) {
469 return new Boolean(getString(key, subKey));
470 }
471 }
472
473 /***
474 * Tries to return you a Boolean object. If the object stored is a Boolean
475 * it will just be cast from an Object to a Boolena and returned. If the
476 * Object is not a Boolean it will try to create a new Boolean from it and
477 * return that.
478 *
479 * @param key the key of the object in the root hashtable you wish to
480 * retreive
481 *
482 * @return the value of the key as a Boolean
483 */
484 public Boolean getBoolean(Object key) {
485 try {
486 return (Boolean) getObject(key);
487 } catch (ClassCastException e) {
488 return new Boolean(getString(key));
489 }
490 }
491
492 /***
493 * Returns the specified object as a Clob
494 *
495 * @param key the key of the sub Hashtable
496 * @param subKey the key in the subtable of the object you wish to retreive
497 *
498 * @return the value of the subKey as a Clob
499 */
500 public Clob getClob(Object key, Object subKey) {
501 return (Clob) get(key, subKey);
502 }
503
504 /***
505 * Returns the specified object as a Clob
506 *
507 * @param key the key of the object in the root hashtable you wish to
508 * retreive
509 *
510 * @return the value of the key as a Clob
511 */
512 public Clob getClob(Object key) {
513 return (Clob) getObject(key);
514 }
515
516 /***
517 * Returns the specified object as a Double
518 *
519 * @param key the key of the sub Hashtable
520 * @param subKey the key in the subtable of the object you wish to retreive
521 *
522 * @return the value of the subKey as a Double
523 */
524 public Double getDate(Object key, Object subKey) {
525 return (Double) get(key, subKey);
526 }
527
528 /***
529 * Returns the specified object as a Date
530 *
531 * @param key the key of the object in the root hashtable you wish to
532 * retreive
533 *
534 * @return the value of the key as a Date
535 */
536 public Date getDate(Object key) {
537 return (Date) getObject(key);
538 }
539
540 /***
541 * Returns the specified object as a Double
542 *
543 * @param key the key of the sub Hashtable
544 * @param subKey the key in the subtable of the object you wish to retreive
545 *
546 * @return the value of the subKey as a Double
547 */
548 public Double getDouble(Object key, Object subKey) {
549 return (Double) get(key, subKey);
550 }
551
552 /***
553 * Returns the specified object as a Double
554 *
555 * @param key the key of the object in the root hashtable you wish to
556 * retreive
557 *
558 * @return the value of the key as a Double
559 */
560 public Double getDouble(Object key) {
561 return (Double) getObject(key);
562 }
563
564 /***
565 * Returns the specified object as a Float
566 *
567 * @param key the key of the sub Hashtable
568 * @param subKey the key in the subtable of the object you wish to retreive
569 *
570 * @return the value of the subKey as a Float
571 */
572 public Float getFloat(Object key, Object subKey) {
573 return (Float) get(key, subKey);
574 }
575
576 /***
577 * Returns the specified object as a Float
578 *
579 * @param key the key of the object in the root hashtable you wish to
580 * retreive
581 *
582 * @return the value of the key as a Float
583 */
584 public Float getFloat(Object key) {
585 return (Float) getObject(key);
586 }
587
588 /***
589 * Returns the specified object as a Hashtable
590 *
591 * @param key the key of the sub Hashtable
592 * @param subKey the key in the subtable of the object you wish to retreive
593 *
594 * @return the value of the subKey as a Float
595 */
596 public Hashtable getHashtable(Object key, Object subKey) {
597 return (Hashtable) get(key, subKey);
598 }
599
600 /***
601 * Returns the specified object as a Float
602 *
603 * @param key the key of the object in the root hashtable you wish to
604 * retreive
605 *
606 * @return the value of the key as a Float
607 */
608 public Hashtable getHashtable(Object key) {
609 return (Hashtable) getObject(key);
610 }
611
612
613 /***
614 * Returns the specified object as an Integer
615 *
616 * @param key the key of the sub Hashtable
617 * @param subKey the key in the subtable of the object you wish to retreive
618 *
619 * @return the value of the subKey as an Integer
620 */
621 public Integer getInteger(Object key, Object subKey) {
622 return (Integer) get(key, subKey);
623 }
624
625 /***
626 * Returns the specified object as an Integer
627 *
628 * @param key the key of the object in the root hashtable you wish to
629 * retreive
630 *
631 * @return the value of the key as an Integer
632 */
633 public Integer getInteger(Object key) {
634 return (Integer) getObject(key);
635 }
636
637 /***
638 * Returns the specified object as a String
639 *
640 * @param key the key of the sub Hashtable
641 * @param subKey the key in the subtable of the object you wish to retreive
642 *
643 * @return the value of the subKey as a string
644 */
645 public String getString(Object key, Object subKey) {
646 return (String) get(key, subKey);
647 }
648
649 /***
650 * Returns the specified object as a String
651 *
652 * @param key the key of the object in the root hashtable you wish to
653 * retreive
654 *
655 * @return the value of the key as a String
656 */
657 public String getString(Object key) {
658 return (String) getObject(key);
659 }
660
661 /***
662 * Returns the specified object as a Vector
663 *
664 * @param key the key of the sub Hashtable
665 * @param subKey the key in the subtable of the object you wish to retreive
666 *
667 * @return the value of the subKey as a Vector
668 */
669 public Vector getVector(Object key, Object subKey) {
670 return (Vector) get(key, subKey);
671 }
672
673 /***
674 * Returns the specified object as a Vector
675 *
676 * @param key the key of the object in the root hashtable you wish to
677 * retreive
678 *
679 * @return the value of the key as a Vector
680 */
681 public Vector getVector(Object key) {
682 return (Vector) getObject(key);
683 }
684
685 /***
686 * Returns the hash code value for the root Hashtable's Map as per the
687 * definition in the Map interface.
688 *
689 * @return the hash code value for this Map in the subHashtable indicated
690 * by key as per the definition in the Map interface.
691 */
692 public int hashCode() {
693 return rootHashtable.hashCode();
694 }
695
696 /***
697 * Returns the hash code value for the specified Hashtable's Map as per the
698 * definition in the Map interface.
699 *
700 * @param key the key of the object in the root hashtable you wish to
701 * retreive
702 *
703 * @return the hash code value for this Map as per the definition in the
704 * Map interface or -1 in the case of a missing subHashtable.
705 */
706 public int hashCode(Object key) {
707 if (key == null) {
708 return -1;
709 }
710
711 Hashtable subHashtable = get(key);
712
713 if (subHashtable != null) {
714 return subHashtable.hashCode();
715 } else {
716 return -1;
717 }
718 }
719
720 /***
721 * Tests if this hashtable maps no keys to values.
722 *
723 * @return true if table is empty
724 */
725 public boolean isEmpty() {
726 return rootHashtable.isEmpty();
727 }
728
729 /***
730 * Tests if this hashtable maps no keys to values.
731 *
732 * @param key the key of the sub Hashtable you wish to test
733 *
734 * @return true if the subHashtable indicated by key is empty, or true in
735 * the case of a missing subHashtable
736 */
737 public boolean isEmpty(Object key) {
738 if (key == null) {
739 return true;
740 }
741
742 Hashtable subHashtable = get(key);
743
744 if (subHashtable != null) {
745 return subHashtable.isEmpty();
746 } else {
747 return true;
748 }
749 }
750
751 /***
752 * Returns an enumeration of the keys in the root hashtable.
753 *
754 * @return an enumeration of the keys in the root Hashtable.
755 */
756 public Enumeration keys() {
757 return rootHashtable.keys();
758 }
759
760 /***
761 * Returns an enumeration of the keys in the specified hashtable.
762 *
763 * @param key the key of the sub Hashtable you wish to retreive the keys of
764 *
765 * @return an enumeration of the keys in the subHashtable indicated by key
766 */
767 public Enumeration keys(Object key) {
768 if (key == null) {
769 return null;
770 }
771
772 Hashtable subHashtable = get(key);
773
774 if (subHashtable != null) {
775 return subHashtable.keys();
776 } else {
777 return null;
778 }
779 }
780
781 /***
782 * Returns a Set view of the keys contained in the root Hashtable.
783 *
784 * @return a Set view of the keys contained in the root Hashtable.
785 */
786 public Set keySet() {
787 return rootHashtable.keySet();
788 }
789
790 /***
791 * Returns a Set view of the keys contained in the specified Hashtable.
792 *
793 * @param key the key of the sub Hashtable you wish to retreive the key Set
794 * for
795 *
796 * @return a Set view of the keys contained in the subHashtable indicated
797 * by key.
798 */
799 public Set keySet(Object key) {
800 if (key == null) {
801 return null;
802 }
803
804 Hashtable subHashtable = get(key);
805
806 if (subHashtable != null) {
807 return subHashtable.keySet();
808 } else {
809 return null;
810 }
811 }
812
813 /***
814 * Maps the specified key to the specified value in the root hashtable. if
815 * the value passed is null it will put in an empty string
816 *
817 * @param key the key in the root Hashtable you wish to associate a value
818 * with
819 * @param theObject wish wish to associate with the key
820 */
821 public void put(Object key, Object theObject) {
822 if (key != null) {
823 if (theObject != null) {
824 rootHashtable.put(key, theObject);
825 } else {
826 rootHashtable.put(key, ""); //stupid hashtables not accepting null
827 }
828 }
829 }
830
831 /***
832 * Maps the specified key to the specified value in the subHshtable
833 * indicated by key
834 *
835 * @param key the key identifying the subHashtable to call put on.
836 * @param subKey the key you want the value object mapped to
837 * @param value the value you want mapped to subKey
838 */
839 public void put(Object key, Object subKey, Object value) {
840 if (!rootHashtable.containsKey(key)) {
841 rootHashtable.put(key, new Hashtable());
842 }
843
844 Hashtable subHashtable = get(key);
845
846 if (subHashtable == null) {
847 subHashtable = new Hashtable();
848 }
849
850 subHashtable.put(subKey, value);
851 }
852
853 /***
854 * Copies all of the mappings from the specified Map to this Hashtable
855 * These mappings will replace any mappings that this Hashtable had for
856 * any of the keys currently in the specified Map.
857 *
858 * @param aMap Mappings to be stored in the root Hashtable's Map
859 */
860 public void putAll(Map aMap) {
861 rootHashtable.putAll(aMap);
862 }
863
864 /***
865 * Copies all of the mappings from the specified Map to the subHashtable
866 * indicated by key These mappings will replace any mappings that this
867 * Hashtable had for any of the keys currently in the specified Map.
868 *
869 * @param key the key of the subHashtable you wish to putAll in
870 * @param aMap Mappings to be stored in the specified sub Hashtable
871 */
872 public void putAll(Object key, Map aMap) {
873 if (key != null) {
874 Hashtable subHashtable = get(key);
875
876 if (subHashtable == null) {
877 subHashtable = new Hashtable();
878 subHashtable.putAll(aMap);
879 } else {
880 subHashtable.putAll(aMap);
881 }
882 }
883 }
884
885 /***
886 * Removes the key (and its corresponding value) from this hashtable.
887 *
888 * @param key the key of the object in the root Hashtable you wish to
889 * remove
890 *
891 * @return the value to which the key had been mapped in this hashtable, or
892 * null if the key did not have a mapping.
893 */
894 public Object remove(Object key) {
895 return rootHashtable.remove(key);
896 }
897
898 /***
899 * removes value from sub-table specified by key and subkey.
900 *
901 * @param key the key indicating which subHashtable to remove from
902 * @param subKey the key in the subHashtable you wish to remove
903 *
904 * @return the value to which the key had been mapped in this hashtable, or
905 * null if the key did not have a mapping.
906 */
907 public Object remove(Object key, Object subKey) {
908 if ((key == null) || (subKey == null)) {
909 return null;
910 }
911
912 Hashtable subHashtable = get(key);
913
914 if (subHashtable != null) {
915 return subHashtable.remove(subKey);
916 } else {
917 return null;
918 }
919 }
920
921 /***
922 * Returns the number of keys in the root Hashtable.
923 *
924 * @return the number of keys in the root Hashtable.
925 */
926 public int size() {
927 return rootHashtable.size();
928 }
929
930 /***
931 * Returns the number of keys in the specified sub Hashtable
932 *
933 * @param key the key indicating which subHashtable you wish to get the
934 * size of
935 *
936 * @return the number of keys in the subHashtable indicated by key or -1 in
937 * the case of a null value.
938 */
939 public int size(Object key) {
940 if (key == null) {
941 return -1;
942 }
943
944 Hashtable subHashtable = get(key);
945
946 if (subHashtable != null) {
947 return subHashtable.size();
948 } else {
949 return -1;
950 }
951 }
952
953 /***
954 * Returns the number of keys in all the sub Hashtables
955 *
956 * @return the combined total size of all tables
957 */
958 public int totalSize() {
959 int count = 0;
960
961 for (Enumeration enum = rootHashtable.keys(); enum.hasMoreElements();) {
962 Object key = enum.nextElement();
963 Hashtable subHashtable = get(key);
964 count += subHashtable.size();
965 }
966
967 return count;
968 }
969
970 /***
971 * Returns a string representation of this Hashtable object and all its sub
972 * Hashtables.
973 *
974 * @return Returns a string representation of this Hashtable object and all
975 * of its subHashtables in the form of a set of entries, enclosed
976 * in braces and separated by the ASCII characters ", " (comma and
977 * space). Each one will be preceded by the key of the hashtable
978 * that contains them followed bya space. ex. subHastableKey
979 * (this, that, etc.)
980 */
981 public String toString() {
982 StringBuffer buffer = new StringBuffer();
983
984 for (Enumeration e = rootHashtable.keys(); e.hasMoreElements();) {
985 Object key = e.nextElement();
986 buffer.append("\n");
987 buffer.append(key.toString());
988 buffer.append(" ");
989
990 Hashtable subHashtable = get(key);
991 buffer.append(subHashtable.toString());
992 }
993
994 buffer.deleteCharAt(1);
995
996 return buffer.toString();
997 }
998
999 /***
1000 * Returns a string representation of the subHashtable indicated by key
1001 *
1002 * @param key the key indicating the sub Hashtable you wish to retreive a
1003 * string representation of
1004 *
1005 * @return a string representation of the subHashtable indicated by key
1006 * object in the form of a set of entries, enclosed in braces and
1007 * separated by the ASCII characters ", " (comma and space).
1008 */
1009 public String toString(Object key) {
1010 if (key == null) {
1011 return null;
1012 }
1013
1014 Hashtable subHashtable = get(key);
1015
1016 if (subHashtable != null) {
1017 return subHashtable.toString();
1018 } else {
1019 return null;
1020 }
1021 }
1022
1023 /***
1024 * return a Collection view of the values contained in the root Hashtable.
1025 *
1026 * @return a Collection view of the values contained in the root Hashtable.
1027 */
1028 public Collection values() {
1029 return rootHashtable.values();
1030 }
1031
1032 /***
1033 * Returns a Collection view of the values contained in the specified sub
1034 * Hashtable indicated by key.
1035 *
1036 * @param key the key indicating the sub Hashtable whose values you wish to
1037 * retreive a Collection of.
1038 *
1039 * @return a Collection view of the values contained in the specified sub
1040 * Hashtable
1041 */
1042 public Collection values(Object key) {
1043 if (key == null) {
1044 return null;
1045 }
1046
1047 Hashtable subHashtable = get(key);
1048
1049 if (subHashtable != null) {
1050 return subHashtable.values();
1051 } else {
1052 return null;
1053 }
1054 }
1055 }
This page was automatically generated by Maven