1 55 56 package org.apache.bsf.debug.util; 57 58 import java.util.Enumeration ; 59 import java.util.NoSuchElementException ; 60 76 77 public class IntHashtable implements Cloneable { 78 79 int elementCount; 80 IntHashMapEntry[] elementData; 81 private int loadFactor; 82 private int threshold; 83 84 private static final class HashEnumerator implements Enumeration { 85 IntHashMapEntry array[]; 86 int start, end; 87 IntHashMapEntry entry; 88 HashEnumerator (IntHashMapEntry[] entries) { 89 array = entries; 90 start = 0; 91 end = array.length; 92 } 93 public boolean hasMoreElements () { 94 if (entry != null) return true; 95 while (start < end) { 96 if (array[start] == null) start++; 97 else return true; 98 } 99 return false; 100 } 101 public Object nextElement () { 102 if (hasMoreElements()) { 103 if (entry == null) entry = array[start++]; 104 Object result = entry.value; 105 entry = entry.next; 106 return result; 107 } 108 throw new NoSuchElementException (); 109 } 110 } 111 112 119 public IntHashtable() { 120 this (101); 121 } 122 131 public IntHashtable(int capacity) { 132 if (capacity < 0) throw new IllegalArgumentException (); 133 elementCount = 0; 134 elementData = new IntHashMapEntry[capacity == 0 ? 1 : capacity]; 135 loadFactor = 7500; computeMaxSize(); 137 } 138 148 public IntHashtable(int capacity, float loadFactor) { 149 if (capacity < 0 || loadFactor <= 0) 150 throw new IllegalArgumentException (); 151 elementCount = 0; 152 elementData = new IntHashMapEntry[capacity]; 153 this.loadFactor = (int)(loadFactor * 10000); 154 computeMaxSize(); 155 } 156 166 public synchronized void clear() { 167 elementCount = 0; 168 for (int i = elementData.length; --i >= 0;) { 169 elementData[i] = null; 170 } 171 } 172 183 public synchronized Object clone() { 184 try { 185 IntHashtable hashtable = (IntHashtable) super.clone (); 186 hashtable.elementData = (IntHashMapEntry[])elementData.clone(); 187 IntHashMapEntry entry; 188 for (int i=elementData.length; --i >= 0;) { 189 if ((entry = elementData[i]) != null) 190 hashtable.elementData[i] = (IntHashMapEntry)entry.clone(); 191 } 192 return hashtable; 193 } catch (CloneNotSupportedException e) { 194 return null; 195 } 196 } 197 private void computeMaxSize() { 198 threshold = (int)((long)elementData.length * loadFactor / 10000); 199 } 200 213 public synchronized boolean contains(Object value) { 214 for (int i=elementData.length; --i >= 0;) { 215 IntHashMapEntry entry = elementData[i]; 216 while (entry != null) { 217 if (entry.value == value || entry.value.equals(value)) 218 return true; 219 entry = entry.next; 220 } 221 } 222 return false; 223 } 224 237 public synchronized boolean containsKey(int key) { 238 return getEntry(key) != null; 239 } 240 254 public synchronized Enumeration elements() { 255 return new HashEnumerator (elementData); 256 } 257 270 public synchronized Object get(int key) { 271 int index = (key & 0x7FFFFFFF) % elementData.length; 272 IntHashMapEntry entry = elementData[index]; 273 while (entry != null) { 274 if (entry.key == key) return entry.value; 275 entry = entry.next; 276 } 277 return null; 278 } 279 private IntHashMapEntry getEntry(int key) { 280 int index = (key & 0x7FFFFFFF) % elementData.length; 281 IntHashMapEntry entry = elementData[index]; 282 while (entry != null) { 283 if (entry.key == key) return entry; 284 entry = entry.next; 285 } 286 return null; 287 } 288 298 public boolean isEmpty() { 299 return elementCount == 0; 300 } 301 319 public synchronized Object put(int key, Object value) { 320 if (value == null) throw new NullPointerException (); 321 int index = (key & 0x7FFFFFFF) % elementData.length; 322 IntHashMapEntry entry = elementData[index]; 323 while (entry != null) { 324 if (entry.key == key) break; 325 entry = entry.next; 326 } 327 if (entry == null) { 328 if (++elementCount > threshold) { 329 rehash(); 330 index = (key & 0x7FFFFFFF) % elementData.length; 331 } 332 entry = new IntHashMapEntry(key, value); 333 entry.next = elementData[index]; 334 elementData[index] = entry; 335 return null; 336 } 337 Object result = entry.value; 338 entry.value = value; 339 return result; 340 } 341 348 protected void rehash() { 349 int length = elementData.length<<1; 350 if (length == 0) length = 1; 351 IntHashMapEntry[] newData = new IntHashMapEntry[length]; 352 for (int i=elementData.length; --i >= 0;) { 353 IntHashMapEntry entry = elementData[i]; 354 while (entry != null) { 355 int index = (entry.key & 0x7FFFFFFF) % length; 356 IntHashMapEntry next = entry.next; 357 entry.next = newData[index]; 358 newData[index] = entry; 359 entry = next; 360 } 361 } 362 elementData = newData; 363 computeMaxSize(); 364 } 365 378 public synchronized Object remove(int key) { 379 IntHashMapEntry last = null; 380 int index = (key & 0x7FFFFFFF) % elementData.length; 381 IntHashMapEntry entry = elementData[index]; 382 while (entry != null) { 383 if (entry.key == key) break; 384 last = entry; 385 entry = entry.next; 386 } 387 if (entry != null) { 388 if (last == null) elementData[index] = entry.next; 389 else last.next = entry.next; 390 elementCount--; 391 return entry.value; 392 } 393 return null; 394 } 395 406 public int size() { 407 return elementCount; 408 } 409 417 public synchronized String toString() { 418 Object key; 419 int count = 0; 420 StringBuffer buffer = new StringBuffer (); 421 buffer.append ('{'); 422 for (int i=elementData.length; --i >= 0;) { 423 IntHashMapEntry entry = elementData[i]; 424 while (entry != null) { 425 buffer.append(entry.key); 426 buffer.append('='); 427 buffer.append(entry.value); 428 buffer.append(','); 429 entry = entry.next; 430 } 431 } 432 if (elementCount > 0) buffer.setLength(buffer.length() - 1); 434 buffer.append ('}'); 435 return buffer.toString (); 436 } 437 } 438 | Popular Tags |