src/share/classes/java/util/EnumMap.java
Print this page
rev 4788 : Fix bunch of generics warnings
*** 110,124 ****
private Object maskNull(Object value) {
return (value == null ? NULL : value);
}
! private V unmaskNull(Object value) {
! return (V) (value == NULL ? null : value);
}
! private static final Enum[] ZERO_LENGTH_ENUM_ARRAY = new Enum[0];
/**
* Creates an empty enum map with the specified key type.
*
* @param keyType the class object of the key type for this enum map
--- 110,124 ----
private Object maskNull(Object value) {
return (value == null ? NULL : value);
}
! private Object unmaskNull(Object value) {
! return (value == NULL ? null : value);
}
! private static final Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0];
/**
* Creates an empty enum map with the specified key type.
*
* @param keyType the class object of the key type for this enum map
*** 208,223 ****
* @param key the key whose presence in this map is to be tested
* @return <tt>true</tt> if this map contains a mapping for the specified
* key
*/
public boolean containsKey(Object key) {
! return isValidKey(key) && vals[((Enum)key).ordinal()] != null;
}
private boolean containsMapping(Object key, Object value) {
return isValidKey(key) &&
! maskNull(value).equals(vals[((Enum)key).ordinal()]);
}
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
--- 208,223 ----
* @param key the key whose presence in this map is to be tested
* @return <tt>true</tt> if this map contains a mapping for the specified
* key
*/
public boolean containsKey(Object key) {
! return isValidKey(key) && vals[((Enum<?>)key).ordinal()] != null;
}
private boolean containsMapping(Object key, Object value) {
return isValidKey(key) &&
! maskNull(value).equals(vals[((Enum<?>)key).ordinal()]);
}
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*** 231,243 ****
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*/
public V get(Object key) {
return (isValidKey(key) ?
! unmaskNull(vals[((Enum)key).ordinal()]) : null);
}
// Modification Operations
/**
--- 231,244 ----
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*/
+ @SuppressWarnings("unchecked")
public V get(Object key) {
return (isValidKey(key) ?
! (V)unmaskNull(vals[((Enum<?>)key).ordinal()]) : null);
}
// Modification Operations
/**
*** 252,270 ****
* <tt>null</tt> if there was no mapping for key. (A <tt>null</tt>
* return can also indicate that the map previously associated
* <tt>null</tt> with the specified key.)
* @throws NullPointerException if the specified key is null
*/
public V put(K key, V value) {
typeCheck(key);
int index = key.ordinal();
Object oldValue = vals[index];
vals[index] = maskNull(value);
if (oldValue == null)
size++;
! return unmaskNull(oldValue);
}
/**
* Removes the mapping for this key from this map if present.
*
--- 253,272 ----
* <tt>null</tt> if there was no mapping for key. (A <tt>null</tt>
* return can also indicate that the map previously associated
* <tt>null</tt> with the specified key.)
* @throws NullPointerException if the specified key is null
*/
+ @SuppressWarnings("unchecked")
public V put(K key, V value) {
typeCheck(key);
int index = key.ordinal();
Object oldValue = vals[index];
vals[index] = maskNull(value);
if (oldValue == null)
size++;
! return (V)unmaskNull(oldValue);
}
/**
* Removes the mapping for this key from this map if present.
*
*** 272,296 ****
* @return the previous value associated with specified key, or
* <tt>null</tt> if there was no entry for key. (A <tt>null</tt>
* return can also indicate that the map previously associated
* <tt>null</tt> with the specified key.)
*/
public V remove(Object key) {
if (!isValidKey(key))
return null;
! int index = ((Enum)key).ordinal();
Object oldValue = vals[index];
vals[index] = null;
if (oldValue != null)
size--;
! return unmaskNull(oldValue);
}
private boolean removeMapping(Object key, Object value) {
if (!isValidKey(key))
return false;
! int index = ((Enum)key).ordinal();
if (maskNull(value).equals(vals[index])) {
vals[index] = null;
size--;
return true;
}
--- 274,299 ----
* @return the previous value associated with specified key, or
* <tt>null</tt> if there was no entry for key. (A <tt>null</tt>
* return can also indicate that the map previously associated
* <tt>null</tt> with the specified key.)
*/
+ @SuppressWarnings("unchecked")
public V remove(Object key) {
if (!isValidKey(key))
return null;
! int index = ((Enum<?>)key).ordinal();
Object oldValue = vals[index];
vals[index] = null;
if (oldValue != null)
size--;
! return (V)unmaskNull(oldValue);
}
private boolean removeMapping(Object key, Object value) {
if (!isValidKey(key))
return false;
! int index = ((Enum<?>)key).ordinal();
if (maskNull(value).equals(vals[index])) {
vals[index] = null;
size--;
return true;
}
*** 304,314 ****
private boolean isValidKey(Object key) {
if (key == null)
return false;
// Cheaper than instanceof Enum followed by getDeclaringClass
! Class keyClass = key.getClass();
return keyClass == keyType || keyClass.getSuperclass() == keyType;
}
// Bulk Operations
--- 307,317 ----
private boolean isValidKey(Object key) {
if (key == null)
return false;
// Cheaper than instanceof Enum followed by getDeclaringClass
! Class<?> keyClass = key.getClass();
return keyClass == keyType || keyClass.getSuperclass() == keyType;
}
// Bulk Operations
*** 321,332 ****
* @throws NullPointerException the specified map is null, or if
* one or more keys in the specified map are null
*/
public void putAll(Map<? extends K, ? extends V> m) {
if (m instanceof EnumMap) {
! EnumMap<? extends K, ? extends V> em =
! (EnumMap<? extends K, ? extends V>)m;
if (em.keyType != keyType) {
if (em.isEmpty())
return;
throw new ClassCastException(em.keyType + " != " + keyType);
}
--- 324,335 ----
* @throws NullPointerException the specified map is null, or if
* one or more keys in the specified map are null
*/
public void putAll(Map<? extends K, ? extends V> m) {
if (m instanceof EnumMap) {
! EnumMap<?, ?> em =
! (EnumMap<?, ?>)m;
if (em.keyType != keyType) {
if (em.isEmpty())
return;
throw new ClassCastException(em.keyType + " != " + keyType);
}
*** 466,482 ****
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
! 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;
return removeMapping(entry.getKey(), entry.getValue());
}
public int size() {
return size;
}
--- 469,485 ----
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
! 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;
return removeMapping(entry.getKey(), entry.getValue());
}
public int size() {
return size;
}
*** 547,557 ****
private class ValueIterator extends EnumMapIterator<V> {
public V next() {
if (!hasNext())
throw new NoSuchElementException();
lastReturnedIndex = index++;
! return unmaskNull(vals[lastReturnedIndex]);
}
}
private class EntryIterator extends EnumMapIterator<Map.Entry<K,V>> {
private Entry lastReturnedEntry = null;
--- 550,560 ----
private class ValueIterator extends EnumMapIterator<V> {
public V next() {
if (!hasNext())
throw new NoSuchElementException();
lastReturnedIndex = index++;
! return (V)unmaskNull(vals[lastReturnedIndex]);
}
}
private class EntryIterator extends EnumMapIterator<Map.Entry<K,V>> {
private Entry lastReturnedEntry = null;
*** 583,598 ****
return keyUniverse[index];
}
public V getValue() {
checkIndexForEntryUse();
! return unmaskNull(vals[index]);
}
public V setValue(V value) {
checkIndexForEntryUse();
! V oldValue = unmaskNull(vals[index]);
vals[index] = maskNull(value);
return oldValue;
}
public boolean equals(Object o) {
--- 586,601 ----
return keyUniverse[index];
}
public V getValue() {
checkIndexForEntryUse();
! return (V)unmaskNull(vals[index]);
}
public V setValue(V value) {
checkIndexForEntryUse();
! V oldValue = (V)unmaskNull(vals[index]);
vals[index] = maskNull(value);
return oldValue;
}
public boolean equals(Object o) {
*** 600,611 ****
return o == this;
if (!(o instanceof Map.Entry))
return false;
! Map.Entry e = (Map.Entry)o;
! V ourValue = unmaskNull(vals[index]);
Object hisValue = e.getValue();
return (e.getKey() == keyUniverse[index] &&
(ourValue == hisValue ||
(ourValue != null && ourValue.equals(hisValue))));
}
--- 603,614 ----
return o == this;
if (!(o instanceof Map.Entry))
return false;
! Map.Entry<?,?> e = (Map.Entry<?,?>)o;
! Object ourValue = unmaskNull(vals[index]);
Object hisValue = e.getValue();
return (e.getKey() == keyUniverse[index] &&
(ourValue == hisValue ||
(ourValue != null && ourValue.equals(hisValue))));
}
*** 645,666 ****
*/
public boolean equals(Object o) {
if (this == o)
return true;
if (o instanceof EnumMap)
! return equals((EnumMap)o);
if (!(o instanceof Map))
return false;
! Map<K,V> m = (Map<K,V>)o;
if (size != m.size())
return false;
for (int i = 0; i < keyUniverse.length; i++) {
if (null != vals[i]) {
K key = keyUniverse[i];
! V value = unmaskNull(vals[i]);
if (null == value) {
if (!((null == m.get(key)) && m.containsKey(key)))
return false;
} else {
if (!value.equals(m.get(key)))
--- 648,669 ----
*/
public boolean equals(Object o) {
if (this == o)
return true;
if (o instanceof EnumMap)
! return equals((EnumMap<?,?>)o);
if (!(o instanceof Map))
return false;
! Map<?,?> m = (Map<?,?>)o;
if (size != m.size())
return false;
for (int i = 0; i < keyUniverse.length; i++) {
if (null != vals[i]) {
K key = keyUniverse[i];
! Object value = unmaskNull(vals[i]);
if (null == value) {
if (!((null == m.get(key)) && m.containsKey(key)))
return false;
} else {
if (!value.equals(m.get(key)))