1 16 19 20 package org.apache.xalan.xsltc.runtime; 21 22 import java.util.Enumeration ; 23 24 30 31 35 class HashtableEntry { 36 int hash; 37 Object key; 38 Object value; 39 HashtableEntry next; 40 41 protected Object clone() { 42 HashtableEntry entry = new HashtableEntry(); 43 entry.hash = hash; 44 entry.key = key; 45 entry.value = value; 46 entry.next = (next != null) ? (HashtableEntry)next.clone() : null; 47 return entry; 48 } 49 } 50 51 54 public class Hashtable { 55 56 private transient HashtableEntry table[]; private transient int count; private int threshold; private float loadFactor; 61 65 public Hashtable(int initialCapacity, float loadFactor) { 66 if (initialCapacity <= 0) initialCapacity = 11; 67 if (loadFactor <= 0.0) loadFactor = 0.75f; 68 this.loadFactor = loadFactor; 69 table = new HashtableEntry[initialCapacity]; 70 threshold = (int)(initialCapacity * loadFactor); 71 } 72 73 77 public Hashtable(int initialCapacity) { 78 this(initialCapacity, 0.75f); 79 } 80 81 85 public Hashtable() { 86 this(101, 0.75f); 87 } 88 89 92 public int size() { 93 return count; 94 } 95 96 99 public boolean isEmpty() { 100 return count == 0; 101 } 102 103 106 public Enumeration keys() { 107 return new HashtableEnumerator(table, true); 108 } 109 110 115 public Enumeration elements() { 116 return new HashtableEnumerator(table, false); 117 } 118 119 124 public boolean contains(Object value) { 125 126 if (value == null) throw new NullPointerException (); 127 128 int i; 129 HashtableEntry e; 130 HashtableEntry tab[] = table; 131 132 for (i = tab.length ; i-- > 0 ;) { 133 for (e = tab[i] ; e != null ; e = e.next) { 134 if (e.value.equals(value)) { 135 return true; 136 } 137 } 138 } 139 return false; 140 } 141 142 145 public boolean containsKey(Object key) { 146 HashtableEntry e; 147 HashtableEntry tab[] = table; 148 int hash = key.hashCode(); 149 int index = (hash & 0x7FFFFFFF) % tab.length; 150 151 for (e = tab[index] ; e != null ; e = e.next) 152 if ((e.hash == hash) && e.key.equals(key)) 153 return true; 154 155 return false; 156 } 157 158 161 public Object get(Object key) { 162 HashtableEntry e; 163 HashtableEntry tab[] = table; 164 int hash = key.hashCode(); 165 int index = (hash & 0x7FFFFFFF) % tab.length; 166 167 for (e = tab[index] ; e != null ; e = e.next) 168 if ((e.hash == hash) && e.key.equals(key)) 169 return e.value; 170 171 return null; 172 } 173 174 180 protected void rehash() { 181 HashtableEntry e, old; 182 int i, index; 183 int oldCapacity = table.length; 184 HashtableEntry oldTable[] = table; 185 186 int newCapacity = oldCapacity * 2 + 1; 187 HashtableEntry newTable[] = new HashtableEntry[newCapacity]; 188 189 threshold = (int)(newCapacity * loadFactor); 190 table = newTable; 191 192 for (i = oldCapacity ; i-- > 0 ;) { 193 for (old = oldTable[i] ; old != null ; ) { 194 e = old; 195 old = old.next; 196 index = (e.hash & 0x7FFFFFFF) % newCapacity; 197 e.next = newTable[index]; 198 newTable[index] = e; 199 } 200 } 201 } 202 203 211 public Object put(Object key, Object value) { 212 if (value == null) throw new NullPointerException (); 214 215 HashtableEntry e; 217 HashtableEntry tab[] = table; 218 int hash = key.hashCode(); 219 int index = (hash & 0x7FFFFFFF) % tab.length; 220 221 for (e = tab[index] ; e != null ; e = e.next) { 222 if ((e.hash == hash) && e.key.equals(key)) { 223 Object old = e.value; 224 e.value = value; 225 return old; 226 } 227 } 228 229 if (count >= threshold) { 231 rehash(); 232 return put(key, value); 233 } 234 235 e = new HashtableEntry(); 237 e.hash = hash; 238 e.key = key; 239 e.value = value; 240 e.next = tab[index]; 241 tab[index] = e; 242 count++; 243 return null; 244 } 245 246 250 public Object remove(Object key) { 251 HashtableEntry e, prev; 252 HashtableEntry tab[] = table; 253 int hash = key.hashCode(); 254 int index = (hash & 0x7FFFFFFF) % tab.length; 255 for (e = tab[index], prev = null ; e != null ; prev = e, e = e.next) { 256 if ((e.hash == hash) && e.key.equals(key)) { 257 if (prev != null) 258 prev.next = e.next; 259 else 260 tab[index] = e.next; 261 count--; 262 return e.value; 263 } 264 } 265 return null; 266 } 267 268 271 public void clear() { 272 HashtableEntry tab[] = table; 273 for (int index = tab.length; --index >= 0; ) 274 tab[index] = null; 275 count = 0; 276 } 277 278 282 public String toString() { 283 int i; 284 int max = size() - 1; 285 StringBuffer buf = new StringBuffer (); 286 Enumeration k = keys(); 287 Enumeration e = elements(); 288 buf.append("{"); 289 290 for (i = 0; i <= max; i++) { 291 String s1 = k.nextElement().toString(); 292 String s2 = e.nextElement().toString(); 293 buf.append(s1 + "=" + s2); 294 if (i < max) buf.append(", "); 295 } 296 buf.append("}"); 297 return buf.toString(); 298 } 299 300 304 class HashtableEnumerator implements Enumeration { 305 boolean keys; 306 int index; 307 HashtableEntry table[]; 308 HashtableEntry entry; 309 310 HashtableEnumerator(HashtableEntry table[], boolean keys) { 311 this.table = table; 312 this.keys = keys; 313 this.index = table.length; 314 } 315 316 public boolean hasMoreElements() { 317 if (entry != null) { 318 return true; 319 } 320 while (index-- > 0) { 321 if ((entry = table[index]) != null) { 322 return true; 323 } 324 } 325 return false; 326 } 327 328 public Object nextElement() { 329 if (entry == null) { 330 while ((index-- > 0) && ((entry = table[index]) == null)); 331 } 332 if (entry != null) { 333 HashtableEntry e = entry; 334 entry = e.next; 335 return keys ? e.key : e.value; 336 } 337 return null; 338 } 339 } 340 341 } 342 | Popular Tags |