src/share/classes/java/util/Hashtable.java

Print this page
rev 4788 : Fix bunch of generics warnings

*** 127,137 **** implements Map<K,V>, Cloneable, java.io.Serializable { /** * The hash table data. */ ! private transient Entry[] table; /** * The total number of entries in the hash table. */ private transient int count; --- 127,137 ---- implements Map<K,V>, Cloneable, java.io.Serializable { /** * The hash table data. */ ! private transient Entry<?,?>[] table; /** * The total number of entries in the hash table. */ private transient int count;
*** 286,298 **** public synchronized boolean contains(Object value) { if (value == null) { throw new NullPointerException(); } ! Entry tab[] = table; for (int i = tab.length ; i-- > 0 ;) { ! for (Entry<K,V> e = tab[i] ; e != null ; e = e.next) { if (e.value.equals(value)) { return true; } } } --- 286,298 ---- public synchronized boolean contains(Object value) { if (value == null) { throw new NullPointerException(); } ! Entry<?,?> tab[] = table; for (int i = tab.length ; i-- > 0 ;) { ! for (Entry<?,?> e = tab[i] ; e != null ; e = e.next) { if (e.value.equals(value)) { return true; } } }
*** 324,337 **** * <tt>equals</tt> method; <code>false</code> otherwise. * @throws NullPointerException if the key is <code>null</code> * @see #contains(Object) */ public synchronized boolean containsKey(Object key) { ! Entry tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { return true; } } return false; --- 324,337 ---- * <tt>equals</tt> method; <code>false</code> otherwise. * @throws NullPointerException if the key is <code>null</code> * @see #contains(Object) */ public synchronized boolean containsKey(Object key) { ! Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { return true; } } return false;
*** 351,366 **** * {@code null} if this map contains no mapping for the key * @throws NullPointerException if the specified key is null * @see #put(Object, Object) */ public synchronized V get(Object key) { ! Entry tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { ! return e.value; } } return null; } --- 351,366 ---- * {@code null} if this map contains no mapping for the key * @throws NullPointerException if the specified key is null * @see #put(Object, Object) */ public synchronized V get(Object key) { ! Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { ! return (V)e.value; } } return null; }
*** 377,411 **** * hashtable, in order to accommodate and access its entries more * efficiently. This method is called automatically when the * number of keys in the hashtable exceeds this hashtable's capacity * and load factor. */ protected void rehash() { int oldCapacity = table.length; ! Entry[] oldMap = table; // overflow-conscious code int newCapacity = (oldCapacity << 1) + 1; if (newCapacity - MAX_ARRAY_SIZE > 0) { if (oldCapacity == MAX_ARRAY_SIZE) // Keep running with MAX_ARRAY_SIZE buckets return; newCapacity = MAX_ARRAY_SIZE; } ! Entry[] newMap = new Entry[newCapacity]; modCount++; threshold = (int)(newCapacity * loadFactor); table = newMap; for (int i = oldCapacity ; i-- > 0 ;) { ! for (Entry<K,V> old = oldMap[i] ; old != null ; ) { Entry<K,V> e = old; old = old.next; int index = (e.hash & 0x7FFFFFFF) % newCapacity; ! e.next = newMap[index]; newMap[index] = e; } } } --- 377,412 ---- * hashtable, in order to accommodate and access its entries more * efficiently. This method is called automatically when the * number of keys in the hashtable exceeds this hashtable's capacity * and load factor. */ + @SuppressWarnings("unchecked") protected void rehash() { int oldCapacity = table.length; ! Entry<?,?>[] oldMap = table; // overflow-conscious code int newCapacity = (oldCapacity << 1) + 1; if (newCapacity - MAX_ARRAY_SIZE > 0) { if (oldCapacity == MAX_ARRAY_SIZE) // Keep running with MAX_ARRAY_SIZE buckets return; newCapacity = MAX_ARRAY_SIZE; } ! Entry<?,?>[] newMap = new Entry<?,?>[newCapacity]; modCount++; threshold = (int)(newCapacity * loadFactor); table = newMap; for (int i = oldCapacity ; i-- > 0 ;) { ! for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) { Entry<K,V> e = old; old = old.next; int index = (e.hash & 0x7FFFFFFF) % newCapacity; ! e.next = (Entry<K,V>)newMap[index]; newMap[index] = e; } } }
*** 431,444 **** if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. ! Entry tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { V old = e.value; e.value = value; return old; } --- 432,446 ---- if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. ! Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (@SuppressWarnings("unchecked") ! Entry<K,V> e = (Entry<K,V>)tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { V old = e.value; e.value = value; return old; }
*** 452,462 **** tab = table; index = (hash & 0x7FFFFFFF) % tab.length; } // Creates the new entry. ! Entry<K,V> e = tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; return null; } --- 454,465 ---- tab = table; index = (hash & 0x7FFFFFFF) % tab.length; } // Creates the new entry. ! @SuppressWarnings("unchecked") ! Entry<K,V> e = (Entry<K,V>)tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; return null; }
*** 468,481 **** * @return the value to which the key had been mapped in this hashtable, * or <code>null</code> if the key did not have a mapping * @throws NullPointerException if the key is <code>null</code> */ public synchronized V remove(Object key) { ! Entry tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<K,V> e = tab[index], prev = null ; e != null ; prev = e, e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { modCount++; if (prev != null) { prev.next = e.next; } else { --- 471,485 ---- * @return the value to which the key had been mapped in this hashtable, * or <code>null</code> if the key did not have a mapping * @throws NullPointerException if the key is <code>null</code> */ public synchronized V remove(Object key) { ! Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (@SuppressWarnings("unchecked") ! Entry<K,V> e = (Entry<K,V>)tab[index], prev = null ; e != null ; prev = e, e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { modCount++; if (prev != null) { prev.next = e.next; } else {
*** 506,516 **** /** * Clears this hashtable so that it contains no keys. */ public synchronized void clear() { ! Entry tab[] = table; modCount++; for (int index = tab.length; --index >= 0; ) tab[index] = null; count = 0; } --- 510,520 ---- /** * Clears this hashtable so that it contains no keys. */ public synchronized void clear() { ! Entry<?,?> tab[] = table; modCount++; for (int index = tab.length; --index >= 0; ) tab[index] = null; count = 0; }
*** 522,536 **** * * @return a clone of the hashtable */ public synchronized Object clone() { try { ! Hashtable<K,V> t = (Hashtable<K,V>) super.clone(); ! t.table = new Entry[table.length]; for (int i = table.length ; i-- > 0 ; ) { t.table[i] = (table[i] != null) ! ? (Entry<K,V>) table[i].clone() : null; } t.keySet = null; t.entrySet = null; t.values = null; t.modCount = 0; --- 526,540 ---- * * @return a clone of the hashtable */ public synchronized Object clone() { try { ! Hashtable<?,?> t = (Hashtable<?,?>)super.clone(); ! t.table = new Entry<?,?>[table.length]; for (int i = table.length ; i-- > 0 ; ) { t.table[i] = (table[i] != null) ! ? (Entry<?,?>) table[i].clone() : null; } t.keySet = null; t.entrySet = null; t.values = null; t.modCount = 0;
*** 673,704 **** } public boolean contains(Object o) { if (!(o instanceof Map.Entry)) return false; ! Map.Entry entry = (Map.Entry)o; Object key = entry.getKey(); ! Entry[] tab = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry e = tab[index]; e != null; e = e.next) if (e.hash==hash && e.equals(entry)) return true; return false; } public boolean remove(Object o) { if (!(o instanceof Map.Entry)) return false; ! Map.Entry<K,V> entry = (Map.Entry<K,V>) o; ! K key = entry.getKey(); ! Entry[] tab = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<K,V> e = tab[index], prev = null; e != null; prev = e, e = e.next) { if (e.hash==hash && e.equals(entry)) { modCount++; if (prev != null) prev.next = e.next; --- 677,709 ---- } public boolean contains(Object o) { if (!(o instanceof Map.Entry)) return false; ! Map.Entry<?,?> entry = (Map.Entry<?,?>)o; Object key = entry.getKey(); ! Entry<?,?>[] tab = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<?,?> e = tab[index]; e != null; e = e.next) if (e.hash==hash && e.equals(entry)) return true; return false; } public boolean remove(Object o) { if (!(o instanceof Map.Entry)) return false; ! Map.Entry<?,?> entry = (Map.Entry<?,?>) o; ! Object key = entry.getKey(); ! Entry<?,?>[] tab = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (@SuppressWarnings("unchecked") ! Entry<K,V> e = (Entry<K,V>)tab[index], prev = null; e != null; prev = e, e = e.next) { if (e.hash==hash && e.equals(entry)) { modCount++; if (prev != null) prev.next = e.next;
*** 774,784 **** if (o == this) return true; if (!(o instanceof Map)) return false; ! Map<K,V> t = (Map<K,V>) o; if (t.size() != size()) return false; try { Iterator<Map.Entry<K,V>> i = entrySet().iterator(); --- 779,789 ---- if (o == this) return true; if (!(o instanceof Map)) return false; ! Map<?,?> t = (Map<?,?>) o; if (t.size() != size()) return false; try { Iterator<Map.Entry<K,V>> i = entrySet().iterator();
*** 824,836 **** int h = 0; if (count == 0 || loadFactor < 0) return h; // Returns zero loadFactor = -loadFactor; // Mark hashCode computation in progress ! Entry[] tab = table; for (int i = 0; i < tab.length; i++) ! for (Entry e = tab[i]; e != null; e = e.next) h += e.key.hashCode() ^ e.value.hashCode(); loadFactor = -loadFactor; // Mark hashCode computation complete return h; } --- 829,841 ---- int h = 0; if (count == 0 || loadFactor < 0) return h; // Returns zero loadFactor = -loadFactor; // Mark hashCode computation in progress ! Entry<?,?>[] tab = table; for (int i = 0; i < tab.length; i++) ! for (Entry<?,?> e = tab[i]; e != null; e = e.next) h += e.key.hashCode() ^ e.value.hashCode(); loadFactor = -loadFactor; // Mark hashCode computation complete return h; }
*** 857,867 **** s.writeInt(table.length); s.writeInt(count); // Stack copies of the entries in the table for (int index = 0; index < table.length; index++) { ! Entry entry = table[index]; while (entry != null) { entryStack = new Entry<>(0, entry.key, entry.value, entryStack); entry = entry.next; --- 862,872 ---- s.writeInt(table.length); s.writeInt(count); // Stack copies of the entries in the table for (int index = 0; index < table.length; index++) { ! Entry<?,?> entry = table[index]; while (entry != null) { entryStack = new Entry<>(0, entry.key, entry.value, entryStack); entry = entry.next;
*** 898,913 **** if (length > elements && (length & 1) == 0) length--; if (origlength > 0 && length > origlength) length = origlength; ! Entry[] table = new Entry[length]; count = 0; // Read the number of elements and then all the key/value objects for (; elements > 0; elements--) { K key = (K)s.readObject(); V value = (V)s.readObject(); // synch could be eliminated for performance reconstitutionPut(table, key, value); } this.table = table; --- 903,920 ---- if (length > elements && (length & 1) == 0) length--; if (origlength > 0 && length > origlength) length = origlength; ! Entry<?,?>[] table = new Entry[length]; count = 0; // Read the number of elements and then all the key/value objects for (; elements > 0; elements--) { + @SuppressWarnings("unchecked") K key = (K)s.readObject(); + @SuppressWarnings("unchecked") V value = (V)s.readObject(); // synch could be eliminated for performance reconstitutionPut(table, key, value); } this.table = table;
*** 922,948 **** * checking for rehashing is necessary since the number of elements * initially in the table is known. The modCount is not incremented * because we are creating a new instance. Also, no return value * is needed. */ ! private void reconstitutionPut(Entry[] tab, K key, V value) throws StreamCorruptedException { if (value == null) { throw new java.io.StreamCorruptedException(); } // Makes sure the key is not already in the hashtable. // This should not happen in deserialized version. int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { throw new java.io.StreamCorruptedException(); } } // Creates the new entry. ! Entry<K,V> e = tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; } /** --- 929,956 ---- * checking for rehashing is necessary since the number of elements * initially in the table is known. The modCount is not incremented * because we are creating a new instance. Also, no return value * is needed. */ ! private void reconstitutionPut(Entry<?,?>[] tab, K key, V value) throws StreamCorruptedException { if (value == null) { throw new java.io.StreamCorruptedException(); } // Makes sure the key is not already in the hashtable. // This should not happen in deserialized version. int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; ! for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { throw new java.io.StreamCorruptedException(); } } // Creates the new entry. ! @SuppressWarnings("unchecked") ! Entry<K,V> e = (Entry<K,V>)tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; } /**
*** 959,968 **** --- 967,977 ---- this.key = key; this.value = value; this.next = next; } + @SuppressWarnings("unchecked") protected Object clone() { return new Entry<>(hash, key, value, (next==null ? null : (Entry<K,V>) next.clone())); }
*** 986,996 **** } public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; ! Map.Entry e = (Map.Entry)o; return (key==null ? e.getKey()==null : key.equals(e.getKey())) && (value==null ? e.getValue()==null : value.equals(e.getValue())); } --- 995,1005 ---- } public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; ! Map.Entry<?,?> e = (Map.Entry<?,?>)o; return (key==null ? e.getKey()==null : key.equals(e.getKey())) && (value==null ? e.getValue()==null : value.equals(e.getValue())); }
*** 1014,1027 **** * can be created with the Iterator methods disabled. This is necessary * to avoid unintentionally increasing the capabilities granted a user * by passing an Enumeration. */ private class Enumerator<T> implements Enumeration<T>, Iterator<T> { ! Entry[] table = Hashtable.this.table; int index = table.length; ! Entry<K,V> entry = null; ! Entry<K,V> lastReturned = null; int type; /** * Indicates whether this Enumerator is serving as an Iterator * or an Enumeration. (true -> Iterator). --- 1023,1036 ---- * can be created with the Iterator methods disabled. This is necessary * to avoid unintentionally increasing the capabilities granted a user * by passing an Enumeration. */ private class Enumerator<T> implements Enumeration<T>, Iterator<T> { ! Entry<?,?>[] table = Hashtable.this.table; int index = table.length; ! Entry<?,?> entry = null; ! Entry<?,?> lastReturned = null; int type; /** * Indicates whether this Enumerator is serving as an Iterator * or an Enumeration. (true -> Iterator).
*** 1039,1072 **** this.type = type; this.iterator = iterator; } public boolean hasMoreElements() { ! Entry<K,V> e = entry; int i = index; ! Entry[] t = table; /* Use locals for faster loop iteration */ while (e == null && i > 0) { e = t[--i]; } entry = e; index = i; return e != null; } public T nextElement() { ! Entry<K,V> et = entry; int i = index; ! Entry[] t = table; /* Use locals for faster loop iteration */ while (et == null && i > 0) { et = t[--i]; } entry = et; index = i; if (et != null) { ! Entry<K,V> e = lastReturned = entry; entry = e.next; return type == KEYS ? (T)e.key : (type == VALUES ? (T)e.value : (T)e); } throw new NoSuchElementException("Hashtable Enumerator"); } --- 1048,1082 ---- this.type = type; this.iterator = iterator; } public boolean hasMoreElements() { ! Entry<?,?> e = entry; int i = index; ! Entry<?,?>[] t = table; /* Use locals for faster loop iteration */ while (e == null && i > 0) { e = t[--i]; } entry = e; index = i; return e != null; } + @SuppressWarnings("unchecked") public T nextElement() { ! Entry<?,?> et = entry; int i = index; ! Entry<?,?>[] t = table; /* Use locals for faster loop iteration */ while (et == null && i > 0) { et = t[--i]; } entry = et; index = i; if (et != null) { ! Entry<?,?> e = lastReturned = entry; entry = e.next; return type == KEYS ? (T)e.key : (type == VALUES ? (T)e.value : (T)e); } throw new NoSuchElementException("Hashtable Enumerator"); }
*** 1089,1102 **** throw new IllegalStateException("Hashtable Enumerator"); if (modCount != expectedModCount) throw new ConcurrentModificationException(); synchronized(Hashtable.this) { ! Entry[] tab = Hashtable.this.table; int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length; ! for (Entry<K,V> e = tab[index], prev = null; e != null; prev = e, e = e.next) { if (e == lastReturned) { modCount++; expectedModCount++; if (prev == null) --- 1099,1113 ---- throw new IllegalStateException("Hashtable Enumerator"); if (modCount != expectedModCount) throw new ConcurrentModificationException(); synchronized(Hashtable.this) { ! Entry<?,?>[] tab = Hashtable.this.table; int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length; ! for (@SuppressWarnings("unchecked") ! Entry<K,V> e = (Entry<K,V>)tab[index], prev = null; e != null; prev = e, e = e.next) { if (e == lastReturned) { modCount++; expectedModCount++; if (prev == null)