Index: /luni/src/main/java/java/util/IdentityHashMap.java =================================================================== --- /luni/src/main/java/java/util/IdentityHashMap.java (revision 409859) +++ /luni/src/main/java/java/util/IdentityHashMap.java (working copy) @@ -34,15 +34,17 @@ * Like HashMap, IdentityHashMap is not thread safe, so access by multiple * threads must be synchronized by an external mechanism such as * Collections.synchronizedMap. + * + * @since 1.4 */ -public class IdentityHashMap extends AbstractMap implements Map, Serializable, +public class IdentityHashMap extends AbstractMap implements Map, Serializable, Cloneable { private static final long serialVersionUID = 8188218128353913216L; /* * The internal data structure to hold key value pairs This array holds keys - * and values alternatingly. + * and values in an alternating fashion. */ transient Object[] elementData; @@ -56,7 +58,7 @@ transient int threshold; /* - * default treshold value that an IdentityHashMap created using the default + * default threshold value that an IdentityHashMap created using the default * constructor would have. */ private static final int DEFAULT_MAX_SIZE = 21; @@ -65,8 +67,8 @@ private static final int loadFactor = 7500; /* - * modification count, to keep track of structural modificiations between - * the Identityhashmap and the iterator + * modification count, to keep track of structural modifications between + * the IdentityHashMap and the iterator */ transient int modCount = 0; @@ -76,8 +78,8 @@ */ private static final Object NULL_OBJECT = new Object(); - static class IdentityHashMapEntry extends MapEntry { - IdentityHashMapEntry(Object theKey, Object theValue) { + static class IdentityHashMapEntry extends MapEntry { + IdentityHashMapEntry(K theKey, V theValue) { super(theKey, theValue); } @@ -105,21 +107,21 @@ } } - static class IdentityHashMapIterator implements Iterator { + static class IdentityHashMapIterator implements Iterator { private int position = 0; // the current position // the position of the entry that was last returned from next() private int lastPosition = 0; - final IdentityHashMap associatedMap; + final IdentityHashMap associatedMap; int expectedModCount; - final MapEntry.Type type; + final MapEntry.Type type; boolean canRemove = false; - IdentityHashMapIterator(MapEntry.Type value, IdentityHashMap hm) { + IdentityHashMapIterator(MapEntry.Type value, IdentityHashMap hm) { associatedMap = hm; type = value; expectedModCount = hm.modCount; @@ -141,11 +143,11 @@ throw new ConcurrentModificationException(); } - public Object next() { + public E next() { checkConcurrentMod(); if (!hasNext()) throw new NoSuchElementException(); - IdentityHashMapEntry result = associatedMap.getEntry(position); + IdentityHashMapEntry result = associatedMap.getEntry(position); lastPosition = position; position += 2; @@ -164,14 +166,14 @@ } } - static class IdentityHashMapEntrySet extends AbstractSet { - private final IdentityHashMap associatedMap; + static class IdentityHashMapEntrySet extends AbstractSet> { + private final IdentityHashMap associatedMap; - public IdentityHashMapEntrySet(IdentityHashMap hm) { + public IdentityHashMapEntrySet(IdentityHashMap hm) { associatedMap = hm; } - IdentityHashMap hashMap() { + IdentityHashMap hashMap() { return associatedMap; } @@ -201,9 +203,9 @@ return false; } - public Iterator iterator() { - return new IdentityHashMapIterator(new MapEntry.Type() { - public Object get(MapEntry entry) { + public Iterator> iterator() { + return new IdentityHashMapIterator,KT,VT>(new MapEntry.Type, KT, VT>() { + public Map.Entry get(MapEntry entry) { return entry; } }, associatedMap); @@ -234,7 +236,7 @@ } private int getThreshold(int maxSize) { - // assign the treshold to maxsize initially, this will change to a + // assign the threshold to maxSize initially, this will change to a // higher value if rehashing occurs. return maxSize > 3 ? maxSize : 3; } @@ -260,7 +262,7 @@ * @param map * A map of (key,value) pairs to copy into the IdentityHashMap */ - public IdentityHashMap(Map map) { + public IdentityHashMap(Map map) { this(map.size() < 6 ? 11 : map.size() * 2); putAll(map); } @@ -327,7 +329,7 @@ * the key * @return the value of the mapping with the specified key */ - public Object get(Object key) { + public V get(Object key) { if (key == null) { key = NULL_OBJECT; } @@ -336,7 +338,7 @@ if (elementData[index] == key) { Object result = elementData[index + 1]; - return (result == NULL_OBJECT) ? null : result; + return (V)((result == NULL_OBJECT) ? null : result); } return null; @@ -359,7 +361,7 @@ * Convenience method for getting the IdentityHashMapEntry without the * NULL_OBJECT elements */ - private IdentityHashMapEntry getEntry(int index) { + private IdentityHashMapEntry getEntry(int index) { Object key = elementData[index]; Object value = elementData[index + 1]; @@ -370,7 +372,7 @@ value = null; } - return new IdentityHashMapEntry(key, value); + return new IdentityHashMapEntry((K)key, (V)value); } /** @@ -401,42 +403,44 @@ /** * Maps the specified key to the specified value. * - * @param key + * @param _key * the key - * @param value + * @param _value * the value * @return the value of any previous mapping with the specified key or null * if there was no mapping */ - public Object put(Object key, Object value) { - if (key == null) { - key = NULL_OBJECT; + public V put(K key, V value) { + Object _key = key; + Object _value = value; + if (_key == null) { + _key = NULL_OBJECT; } - if (value == null) { - value = NULL_OBJECT; + if (_value == null) { + _value = NULL_OBJECT; } - int index = findIndex(key, elementData); + int index = findIndex(_key, elementData); // if the key doesn't exist in the table - if (elementData[index] != key) { + if (elementData[index] != _key) { modCount++; if (++size > threshold) { rehash(); - index = findIndex(key, elementData); + index = findIndex(_key, elementData); } // insert the key and assign the value to null initially - elementData[index] = key; + elementData[index] = _key; elementData[index + 1] = null; } // insert value to where it needs to go, return the old value Object result = elementData[index + 1]; - elementData[index + 1] = value; + elementData[index + 1] = _value; - return (result == NULL_OBJECT) ? null : result; + return (V)((result == NULL_OBJECT) ? null : result); } private void rehash() { @@ -469,7 +473,7 @@ * @return the value of the removed mapping, or null if key is not a key in * this Map */ - public Object remove(Object key) { + public V remove(Object key) { if (key == null) { key = NULL_OBJECT; } @@ -515,31 +519,31 @@ elementData[index] = null; elementData[index + 1] = null; - return (result == NULL_OBJECT) ? null : result; + return (V)((result == NULL_OBJECT) ? null : result); } /** * Answers a Set of the mappings contained in this IdentityHashMap. Each * element in the set is a Map.Entry. The set is backed by this Map so - * changes to one are relected by the other. The set does not support + * changes to one are reflected by the other. The set does not support * adding. * * @return a Set of the mappings */ - public Set entrySet() { - return new IdentityHashMapEntrySet(this); + public Set> entrySet() { + return new IdentityHashMapEntrySet(this); } /** * Answers a Set of the keys contained in this IdentityHashMap. The set is - * backed by this IdentityHashMap so changes to one are relected by the + * backed by this IdentityHashMap so changes to one are reflected by the * other. The set does not support adding. * * @return a Set of the keys */ - public Set keySet() { + public Set keySet() { if (keySet == null) { - keySet = new AbstractSet() { + keySet = new AbstractSet() { public boolean contains(Object object) { return containsKey(object); } @@ -560,11 +564,11 @@ return false; } - public Iterator iterator() { - return new IdentityHashMapIterator(new MapEntry.Type() { - public Object get(MapEntry entry) { - return entry.key; - } + public Iterator iterator() { + return new IdentityHashMapIterator(new MapEntry.Type() { + public K get(MapEntry entry) { + return entry.key; + } }, IdentityHashMap.this); } }; @@ -575,13 +579,13 @@ /** * Answers a Collection of the values contained in this IdentityHashMap. The * collection is backed by this IdentityHashMap so changes to one are - * relected by the other. The collection does not support adding. + * reflected by the other. The collection does not support adding. * * @return a Collection of the values */ - public Collection values() { + public Collection values() { if (valuesCollection == null) { - valuesCollection = new AbstractCollection() { + valuesCollection = new AbstractCollection() { public boolean contains(Object object) { return containsValue(object); } @@ -594,11 +598,11 @@ IdentityHashMap.this.clear(); } - public Iterator iterator() { - return new IdentityHashMapIterator(new MapEntry.Type() { - public Object get(MapEntry entry) { - return entry.value; - } + public Iterator iterator() { + return new IdentityHashMapIterator(new MapEntry.Type() { + public V get(MapEntry entry) { + return entry.value; + } }, IdentityHashMap.this); } @@ -702,8 +706,8 @@ threshold = getThreshold(DEFAULT_MAX_SIZE); elementData = newElementArray(computeElementArraySize()); for (int i = savedSize; --i >= 0;) { - Object key = stream.readObject(); - put(key, stream.readObject()); + K key = (K)stream.readObject(); + put(key, (V)stream.readObject()); } } }