src/share/classes/java/util/IdentityHashMap.java

Print this page
rev 4788 : Fix bunch of generics warnings

@@ -325,10 +325,11 @@
      * The {@link #containsKey containsKey} operation may be used to
      * distinguish these two cases.
      *
      * @see #put(Object, Object)
      */
+    @SuppressWarnings("unchecked")
     public V get(Object key) {
         Object k = maskNull(key);
         Object[] tab = table;
         int len = tab.length;
         int i = hash(k, len);

@@ -429,10 +430,11 @@
         int i = hash(k, len);
 
         Object item;
         while ( (item = tab[i]) != null) {
             if (item == k) {
+                @SuppressWarnings("unchecked")
                 V oldValue = (V) tab[i + 1];
                 tab[i + 1] = value;
                 return oldValue;
             }
             i = nextKeyIndex(i, len);

@@ -522,10 +524,11 @@
         while (true) {
             Object item = tab[i];
             if (item == k) {
                 modCount++;
                 size--;
+                @SuppressWarnings("unchecked")
                 V oldValue = (V) tab[i + 1];
                 tab[i + 1] = null;
                 tab[i] = null;
                 closeDeletion(i);
                 return oldValue;

@@ -636,11 +639,11 @@
      */
     public boolean equals(Object o) {
         if (o == this) {
             return true;
         } else if (o instanceof IdentityHashMap) {
-            IdentityHashMap m = (IdentityHashMap) o;
+            IdentityHashMap<?,?> m = (IdentityHashMap<?,?>) o;
             if (m.size() != size)
                 return false;
 
             Object[] tab = m.table;
             for (int i = 0; i < tab.length; i+=2) {

@@ -648,11 +651,11 @@
                 if (k != null && !containsMapping(k, tab[i + 1]))
                     return false;
             }
             return true;
         } else if (o instanceof Map) {
-            Map m = (Map)o;
+            Map<?,?> m = (Map<?,?>)o;
             return entrySet().equals(m.entrySet());
         } else {
             return false;  // o is not a Map
         }
     }

@@ -696,11 +699,11 @@
      *
      * @return a shallow copy of this map
      */
     public Object clone() {
         try {
-            IdentityHashMap<K,V> m = (IdentityHashMap<K,V>) super.clone();
+            IdentityHashMap<?,?> m = (IdentityHashMap<?,?>) super.clone();
             m.entrySet = null;
             m.table = table.clone();
             return m;
         } catch (CloneNotSupportedException e) {
             throw new InternalError(e);

@@ -766,11 +769,11 @@
 
             Object[] tab = traversalTable;
             int len = tab.length;
 
             int d = deletedSlot;
-            K key = (K) tab[d];
+            Object key = tab[d];
             tab[d] = null;        // vacate the slot
             tab[d + 1] = null;
 
             // If traversing a copy, remove in real table.
             // We can skip gap-closure on copy.

@@ -816,16 +819,18 @@
             }
         }
     }
 
     private class KeyIterator extends IdentityHashMapIterator<K> {
+        @SuppressWarnings("unchecked")
         public K next() {
             return (K) unmaskNull(traversalTable[nextIndex()]);
         }
     }
 
     private class ValueIterator extends IdentityHashMapIterator<V> {
+        @SuppressWarnings("unchecked")
         public V next() {
             return (V) traversalTable[nextIndex() + 1];
         }
     }
 

@@ -852,20 +857,23 @@
 
             private Entry(int index) {
                 this.index = index;
             }
 
+            @SuppressWarnings("unchecked")
             public K getKey() {
                 checkIndexForEntryUse();
                 return (K) unmaskNull(traversalTable[index]);
             }
 
+            @SuppressWarnings("unchecked")
             public V getValue() {
                 checkIndexForEntryUse();
                 return (V) traversalTable[index+1];
             }
 
+            @SuppressWarnings("unchecked")
             public V setValue(V value) {
                 checkIndexForEntryUse();
                 V oldValue = (V) traversalTable[index+1];
                 traversalTable[index+1] = value;
                 // if shadowing, force into main table

@@ -878,11 +886,11 @@
                 if (index < 0)
                     return super.equals(o);
 
                 if (!(o instanceof Map.Entry))
                     return false;
-                Map.Entry e = (Map.Entry)o;
+                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                 return (e.getKey() == unmaskNull(traversalTable[index]) &&
                        e.getValue() == traversalTable[index+1]);
             }
 
             public int hashCode() {

@@ -1107,17 +1115,17 @@
             return new EntryIterator();
         }
         public boolean contains(Object o) {
             if (!(o instanceof Map.Entry))
                 return false;
-            Map.Entry entry = (Map.Entry)o;
+            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
             return containsMapping(entry.getKey(), entry.getValue());
         }
         public boolean remove(Object o) {
             if (!(o instanceof Map.Entry))
                 return false;
-            Map.Entry entry = (Map.Entry)o;
+            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
             return removeMapping(entry.getKey(), entry.getValue());
         }
         public int size() {
             return size;
         }

@@ -1211,11 +1219,13 @@
         // Allow for 33% growth (i.e., capacity is >= 2* size()).
         init(capacity((size*4)/3));
 
         // Read the keys and values, and put the mappings in the table
         for (int i=0; i<size; i++) {
+            @SuppressWarnings("unchecked")
             K key = (K) s.readObject();
+            @SuppressWarnings("unchecked")
             V value = (V) s.readObject();
             putForCreate(key, value);
         }
     }
 

@@ -1224,11 +1234,11 @@
      * update modCount, etc.
      */
     private void putForCreate(K key, V value)
         throws IOException
     {
-        K k = (K)maskNull(key);
+        Object k = maskNull(key);
         Object[] tab = table;
         int len = tab.length;
         int i = hash(k, len);
 
         Object item;