jdk/src/share/classes/java/util/Hashtable.java
changeset 12448 b95438b17098
parent 10419 12c063b39232
child 12859 c44b88bb9b5e
--- a/jdk/src/share/classes/java/util/Hashtable.java	Tue Apr 17 11:59:12 2012 -0700
+++ b/jdk/src/share/classes/java/util/Hashtable.java	Tue Apr 17 12:21:56 2012 -0700
@@ -129,7 +129,7 @@
     /**
      * The hash table data.
      */
-    private transient Entry[] table;
+    private transient Entry<?,?>[] table;
 
     /**
      * The total number of entries in the hash table.
@@ -182,7 +182,7 @@
         if (initialCapacity==0)
             initialCapacity = 1;
         this.loadFactor = loadFactor;
-        table = new Entry[initialCapacity];
+        table = new Entry<?,?>[initialCapacity];
         threshold = (int)(initialCapacity * loadFactor);
     }
 
@@ -288,9 +288,9 @@
             throw new NullPointerException();
         }
 
-        Entry tab[] = table;
+        Entry<?,?> tab[] = table;
         for (int i = tab.length ; i-- > 0 ;) {
-            for (Entry<K,V> e = tab[i] ; e != null ; e = e.next) {
+            for (Entry<?,?> e = tab[i] ; e != null ; e = e.next) {
                 if (e.value.equals(value)) {
                     return true;
                 }
@@ -326,10 +326,10 @@
      * @see     #contains(Object)
      */
     public synchronized boolean containsKey(Object key) {
-        Entry tab[] = table;
+        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) {
+        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
             if ((e.hash == hash) && e.key.equals(key)) {
                 return true;
             }
@@ -352,13 +352,14 @@
      * @throws NullPointerException if the specified key is null
      * @see     #put(Object, Object)
      */
+    @SuppressWarnings("unchecked")
     public synchronized V get(Object key) {
-        Entry tab[] = table;
+        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) {
+        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
             if ((e.hash == hash) && e.key.equals(key)) {
-                return e.value;
+                return (V)e.value;
             }
         }
         return null;
@@ -379,9 +380,10 @@
      * 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;
+        Entry<?,?>[] oldMap = table;
 
         // overflow-conscious code
         int newCapacity = (oldCapacity << 1) + 1;
@@ -391,19 +393,19 @@
                 return;
             newCapacity = MAX_ARRAY_SIZE;
         }
-        Entry[] newMap = new Entry[newCapacity];
+        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 ; ) {
+            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 = newMap[index];
+                e.next = (Entry<K,V>)newMap[index];
                 newMap[index] = e;
             }
         }
@@ -433,13 +435,15 @@
         }
 
         // Makes sure the key is not already in the hashtable.
-        Entry tab[] = table;
+        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;
+        @SuppressWarnings("unchecked")
+        Entry<K,V> entry = (Entry<K,V>)tab[index];
+        for(; entry != null ; entry = entry.next) {
+            if ((entry.hash == hash) && entry.key.equals(key)) {
+                V old = entry.value;
+                entry.value = value;
                 return old;
             }
         }
@@ -454,7 +458,8 @@
         }
 
         // Creates the new entry.
-        Entry<K,V> e = tab[index];
+        @SuppressWarnings("unchecked")
+        Entry<K,V> e = (Entry<K,V>)tab[index];
         tab[index] = new Entry<>(hash, key, value, e);
         count++;
         return null;
@@ -470,10 +475,12 @@
      * @throws  NullPointerException  if the key is <code>null</code>
      */
     public synchronized V remove(Object key) {
-        Entry tab[] = table;
+        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) {
+        @SuppressWarnings("unchecked")
+        Entry<K,V> e = (Entry<K,V>)tab[index];
+        for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) {
             if ((e.hash == hash) && e.key.equals(key)) {
                 modCount++;
                 if (prev != null) {
@@ -508,7 +515,7 @@
      * Clears this hashtable so that it contains no keys.
      */
     public synchronized void clear() {
-        Entry tab[] = table;
+        Entry<?,?> tab[] = table;
         modCount++;
         for (int index = tab.length; --index >= 0; )
             tab[index] = null;
@@ -524,11 +531,11 @@
      */
     public synchronized Object clone() {
         try {
-            Hashtable<K,V> t = (Hashtable<K,V>) super.clone();
-            t.table = new Entry[table.length];
+            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<K,V>) table[i].clone() : null;
+                    ? (Entry<?,?>) table[i].clone() : null;
             }
             t.keySet = null;
             t.entrySet = null;
@@ -675,13 +682,13 @@
         public boolean contains(Object o) {
             if (!(o instanceof Map.Entry))
                 return false;
-            Map.Entry entry = (Map.Entry)o;
+            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
             Object key = entry.getKey();
-            Entry[] tab = table;
+            Entry<?,?>[] tab = table;
             int hash = key.hashCode();
             int index = (hash & 0x7FFFFFFF) % tab.length;
 
-            for (Entry e = tab[index]; e != null; e = e.next)
+            for (Entry<?,?> e = tab[index]; e != null; e = e.next)
                 if (e.hash==hash && e.equals(entry))
                     return true;
             return false;
@@ -690,14 +697,15 @@
         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;
+            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<K,V> e = tab[index], prev = null; e != null;
-                 prev = e, e = e.next) {
+            @SuppressWarnings("unchecked")
+            Entry<K,V> e = (Entry<K,V>)tab[index];
+            for(Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
                 if (e.hash==hash && e.equals(entry)) {
                     modCount++;
                     if (prev != null)
@@ -776,7 +784,7 @@
 
         if (!(o instanceof Map))
             return false;
-        Map<K,V> t = (Map<K,V>) o;
+        Map<?,?> t = (Map<?,?>) o;
         if (t.size() != size())
             return false;
 
@@ -826,9 +834,9 @@
             return h;  // Returns zero
 
         loadFactor = -loadFactor;  // Mark hashCode computation in progress
-        Entry[] tab = table;
+        Entry<?,?>[] tab = table;
         for (int i = 0; i < tab.length; i++)
-            for (Entry e = tab[i]; e != null; e = e.next)
+            for (Entry<?,?> e = tab[i]; e != null; e = e.next)
                 h += e.key.hashCode() ^ e.value.hashCode();
         loadFactor = -loadFactor;  // Mark hashCode computation complete
 
@@ -859,7 +867,7 @@
 
             // Stack copies of the entries in the table
             for (int index = 0; index < table.length; index++) {
-                Entry entry = table[index];
+                Entry<?,?> entry = table[index];
 
                 while (entry != null) {
                     entryStack =
@@ -899,14 +907,15 @@
             length--;
         if (origlength > 0 && length > origlength)
             length = origlength;
-
-        Entry[] table = new Entry[length];
+        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();
+            @SuppressWarnings("unchecked")
+                K key = (K)s.readObject();
+            @SuppressWarnings("unchecked")
+                V value = (V)s.readObject();
             // synch could be eliminated for performance
             reconstitutionPut(table, key, value);
         }
@@ -924,7 +933,7 @@
      * because we are creating a new instance. Also, no return value
      * is needed.
      */
-    private void reconstitutionPut(Entry[] tab, K key, V value)
+    private void reconstitutionPut(Entry<?,?>[] tab, K key, V value)
         throws StreamCorruptedException
     {
         if (value == null) {
@@ -934,13 +943,14 @@
         // 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) {
+        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.
-        Entry<K,V> e = tab[index];
+        @SuppressWarnings("unchecked")
+            Entry<K,V> e = (Entry<K,V>)tab[index];
         tab[index] = new Entry<>(hash, key, value, e);
         count++;
     }
@@ -961,6 +971,7 @@
             this.next = next;
         }
 
+        @SuppressWarnings("unchecked")
         protected Object clone() {
             return new Entry<>(hash, key, value,
                                   (next==null ? null : (Entry<K,V>) next.clone()));
@@ -988,7 +999,7 @@
         public boolean equals(Object o) {
             if (!(o instanceof Map.Entry))
                 return false;
-            Map.Entry e = (Map.Entry)o;
+            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()));
@@ -1016,10 +1027,10 @@
      * by passing an Enumeration.
      */
     private class Enumerator<T> implements Enumeration<T>, Iterator<T> {
-        Entry[] table = Hashtable.this.table;
+        Entry<?,?>[] table = Hashtable.this.table;
         int index = table.length;
-        Entry<K,V> entry = null;
-        Entry<K,V> lastReturned = null;
+        Entry<?,?> entry = null;
+        Entry<?,?> lastReturned = null;
         int type;
 
         /**
@@ -1041,9 +1052,9 @@
         }
 
         public boolean hasMoreElements() {
-            Entry<K,V> e = entry;
+            Entry<?,?> e = entry;
             int i = index;
-            Entry[] t = table;
+            Entry<?,?>[] t = table;
             /* Use locals for faster loop iteration */
             while (e == null && i > 0) {
                 e = t[--i];
@@ -1053,10 +1064,11 @@
             return e != null;
         }
 
+        @SuppressWarnings("unchecked")
         public T nextElement() {
-            Entry<K,V> et = entry;
+            Entry<?,?> et = entry;
             int i = index;
-            Entry[] t = table;
+            Entry<?,?>[] t = table;
             /* Use locals for faster loop iteration */
             while (et == null && i > 0) {
                 et = t[--i];
@@ -1064,7 +1076,7 @@
             entry = et;
             index = i;
             if (et != null) {
-                Entry<K,V> e = lastReturned = entry;
+                Entry<?,?> e = lastReturned = entry;
                 entry = e.next;
                 return type == KEYS ? (T)e.key : (type == VALUES ? (T)e.value : (T)e);
             }
@@ -1091,11 +1103,12 @@
                 throw new ConcurrentModificationException();
 
             synchronized(Hashtable.this) {
-                Entry[] tab = Hashtable.this.table;
+                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) {
+                @SuppressWarnings("unchecked")
+                Entry<K,V> e = (Entry<K,V>)tab[index];
+                for(Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
                     if (e == lastReturned) {
                         modCount++;
                         expectedModCount++;