1
2
3
4
5
6
7
8
9
10
11
12
13
14 package net.wangs.xmlutil;
15
16 import java.util.Enumeration;
17 import java.util.HashSet;
18 import java.util.Hashtable;
19 import java.util.Iterator;
20 import java.util.Set;
21 import java.util.Vector;
22
23 /***
24 * This hashtable stores values using string array.
25 * @author <a href="mailto:yuqingwang_99@yahoo.com">Yuqing Wang</a>
26 * @author <a href="mailto:christian.koelle@bluewin.ch">Christian Kölle</a>
27 */
28 public class MultiKeyHashtable {
29 /***
30 * This method retrieves the value at String array keys.
31 * @param keys - String array, must not be null
32 * @exception NullPointerException thrown if keys are null
33 * @return Object the value or null if not found
34 */
35 public Object get( String[] keys ) {
36 if( keys == null ) {
37 throw new NullPointerException("null keys");
38 }
39
40 if (keys.length == 0) {
41 throw new NullPointerException("no element in keys");
42 }
43
44
45 return _map.get( _composeHashKey(keys) );
46 }
47
48 /***
49 * This method stores the value at String array keys.
50 * @param keys - must not be null
51 * @param value
52 * @exception NullPointerException thrown if key1 or key2 are null
53 */
54 public void put( String[] keys, Object value ) {
55 if( keys == null ) {
56 throw new NullPointerException("null keys");
57 }
58
59 if (keys.length == 0) {
60 throw new NullPointerException("no element in keys");
61 }
62
63
64 _map.put(_composeHashKey(keys), value );
65 }
66
67 /***
68 * Copies all of the mappings from the specified MultiKeyHashtable to
69 * this MultiKeyHashtable.
70 * These mappings will replace any mappings that this MultiKeyHashtable had
71 * for any of the keys currently in the specified MultiKeyHashtable.
72 * @param pHash The MultiKeyHashtable to be copied.
73 */
74 public void putAll(MultiKeyHashtable pHash) {
75 Iterator keyIter = pHash.keySet().iterator();
76 while (keyIter.hasNext()) {
77 String[] keys = (String[]) keyIter.next();
78 this.put(keys, pHash.get(keys));
79 }
80 }
81
82
83 /***
84 * Reset contents. Equals to Map::clear().
85 */
86 public void reset() {
87 _map.clear();
88 }
89
90 /***
91 * Returns an enumeration with the keys.
92 * @return The keys in an Enumeration
93 */
94 public Set keySet() {
95 HashSet keys = new HashSet();
96 for (Enumeration e = _map.keys(); e.hasMoreElements();) {
97 keys.add(_decomposeHashKey((String)e.nextElement()));
98 System.out.println();
99 }
100 return keys;
101 }
102
103 /***
104 * Given a String array, compose a String value that follows the style of hashkey used in
105 * getNodeValuesHashedByNamedAttrs().
106 */
107 protected String _composeHashKey(String[] keys) {
108 String key = "";
109
110 for (int i=0; i<keys.length; i++) {
111 key += _hashToken + keys[i];
112 }
113
114 return key;
115 }
116
117 /***
118 * Given a String, create a string array with the keys as elements
119 */
120 protected String[] _decomposeHashKey(String keys) {
121 if (keys != null && keys.length()>0) {
122 keys = keys.substring(3);
123 Vector keyEnum = new Vector();
124 boolean moreKeys = true;
125 int i = 0;
126 int end = 0;
127 while (moreKeys) {
128 if (keys.indexOf(_hashToken) > 0) {
129 end = keys.indexOf(_hashToken);
130 keyEnum.add(i, keys.substring(0, end));
131 keys = keys.substring(end + 3);
132 i++;
133 } else {
134 keyEnum.add(i, keys);
135 moreKeys = false;
136 }
137 }
138 String[] allKeys = new String[keyEnum.size()];
139 for (int j = 0; j < keyEnum.size(); j++) {
140 allKeys[j] = (String) keyEnum.get(j);
141 }
142 return allKeys;
143 }
144 return null;
145 }
146
147 protected static final String _hashToken = "[&]";
148 protected final Hashtable _map = new Hashtable();
149 }