protected void rehash() { int oldCapacity = table.length; HashtableEntry oldTable[] = table; int newCapacity = oldCapacity * 2 + 1; HashtableEntry newTable[] = new HashtableEntry[newCapacity]; threshold = (int) (newCapacity * loadFactor); table = newTable; for (int i = oldCapacity; i-- > 0; ) { for (HashtableEntry old = oldTable[i]; old != null; ) { HashtableEntry e = old; old = old.next; int index = (e.hash & 0x7FFFFFFF) % newCapacity; e.next = newTable[index]; newTable[index] = e; } } }
protected Object clone() { HashtableEntry entry = new HashtableEntry(); entry.hash = hash; entry.key = key; entry.value = value; entry.next = (next != null) ? (HashtableEntry) next.clone() : null; return entry; }
/** * Removes the key and its corresponding value. Key is searched case insensitively. * * @param key string key * @return object removed or null if none * @exception IllegalArgumentException if key is not s string */ public synchronized Object remove(Object key) throws IllegalArgumentException { if (table == null) return null; try { HashtableEntry tab[] = table; int hash = hashCode((String) key); int index = (hash & 0x7FFFFFFF) % tab.length; for (HashtableEntry e = tab[index], prev = null; e != null; prev = e, e = e.next) { if ((e.hash == hash) && e.key.equalsIgnoreCase((String) key)) { if (prev != null) { prev.next = e.next; } else { tab[index] = e.next; } count--; return e.value; } } return null; } catch (ClassCastException cce) { throw new IllegalArgumentException("Non string keys are not accepted!"); } }
/** * Puts the key and the value in the table. If there already is a key equal ignore case to the one * passed the new value exchhanes the old one. * * @param key String key * @param value object to put * @return old value if any, or null if none * @exception IllegalArgumentException if key is not a string */ public synchronized Object put(Object key, Object value) throws IllegalArgumentException { if (value == null) { throw new NullPointerException(); } if (table == null) initTable(MIN_CAPACITY); try { // Makes sure the key is not already in the hashtable. int hash = hashCode((String) key); int index; HashtableEntry[] tab = null; do { tab = table; index = (hash & 0x7FFFFFFF) % tab.length; for (HashtableEntry e = tab[index]; e != null; e = e.next) { if ((e.hash == hash) && e.key.equalsIgnoreCase((String) key)) { Object old = e.value; e.value = value; return old; } } if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash(); continue; } break; } while (true); // Creates the new entry. HashtableEntry e = new HashtableEntry(); e.hash = hash; e.key = (String) key; e.value = value; e.next = tab[index]; tab[index] = e; count++; return null; } catch (ClassCastException cce) { throw new IllegalArgumentException("Non string keys are not accepted!"); } }