< prev index next >
src/java.base/share/classes/java/util/EnumMap.java
Print this page
*** 48,58 ****
* <p>Null keys are not permitted. Attempts to insert a null key will
* throw {@link NullPointerException}. Attempts to test for the
* presence of a null key or to remove one will, however, function properly.
* Null values are permitted.
! * <P>Like most collection implementations <tt>EnumMap</tt> is not
* synchronized. If multiple threads access an enum map concurrently, and at
* least one of the threads modifies the map, it should be synchronized
* externally. This is typically accomplished by synchronizing on some
* object that naturally encapsulates the enum map. If no such object exists,
* the map should be "wrapped" using the {@link Collections#synchronizedMap}
--- 48,58 ----
* <p>Null keys are not permitted. Attempts to insert a null key will
* throw {@link NullPointerException}. Attempts to test for the
* presence of a null key or to remove one will, however, function properly.
* Null values are permitted.
! * <P>Like most collection implementations {@code EnumMap} is not
* synchronized. If multiple threads access an enum map concurrently, and at
* least one of the threads modifies the map, it should be synchronized
* externally. This is typically accomplished by synchronizing on some
* object that naturally encapsulates the enum map. If no such object exists,
* the map should be "wrapped" using the {@link Collections#synchronizedMap}
*** 78,88 ****
*/
public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V>
implements java.io.Serializable, Cloneable
{
/**
! * The <tt>Class</tt> object for the enum type of all the keys of this map.
*
* @serial
*/
private final Class<K> keyType;
--- 78,88 ----
*/
public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V>
implements java.io.Serializable, Cloneable
{
/**
! * The {@code Class} object for the enum type of all the keys of this map.
*
* @serial
*/
private final Class<K> keyType;
*** 129,139 ****
/**
* Creates an empty enum map with the specified key type.
*
* @param keyType the class object of the key type for this enum map
! * @throws NullPointerException if <tt>keyType</tt> is null
*/
public EnumMap(Class<K> keyType) {
this.keyType = keyType;
keyUniverse = getKeyUniverse(keyType);
vals = new Object[keyUniverse.length];
--- 129,139 ----
/**
* Creates an empty enum map with the specified key type.
*
* @param keyType the class object of the key type for this enum map
! * @throws NullPointerException if {@code keyType} is null
*/
public EnumMap(Class<K> keyType) {
this.keyType = keyType;
keyUniverse = getKeyUniverse(keyType);
vals = new Object[keyUniverse.length];
*** 142,171 ****
/**
* Creates an enum map with the same key type as the specified enum
* map, initially containing the same mappings (if any).
*
* @param m the enum map from which to initialize this enum map
! * @throws NullPointerException if <tt>m</tt> is null
*/
public EnumMap(EnumMap<K, ? extends V> m) {
keyType = m.keyType;
keyUniverse = m.keyUniverse;
vals = m.vals.clone();
size = m.size;
}
/**
* Creates an enum map initialized from the specified map. If the
! * specified map is an <tt>EnumMap</tt> instance, this constructor behaves
* identically to {@link #EnumMap(EnumMap)}. Otherwise, the specified map
* must contain at least one mapping (in order to determine the new
* enum map's key type).
*
* @param m the map from which to initialize this enum map
! * @throws IllegalArgumentException if <tt>m</tt> is not an
! * <tt>EnumMap</tt> instance and contains no mappings
! * @throws NullPointerException if <tt>m</tt> is null
*/
public EnumMap(Map<K, ? extends V> m) {
if (m instanceof EnumMap) {
EnumMap<K, ? extends V> em = (EnumMap<K, ? extends V>) m;
keyType = em.keyType;
--- 142,171 ----
/**
* Creates an enum map with the same key type as the specified enum
* map, initially containing the same mappings (if any).
*
* @param m the enum map from which to initialize this enum map
! * @throws NullPointerException if {@code m} is null
*/
public EnumMap(EnumMap<K, ? extends V> m) {
keyType = m.keyType;
keyUniverse = m.keyUniverse;
vals = m.vals.clone();
size = m.size;
}
/**
* Creates an enum map initialized from the specified map. If the
! * specified map is an {@code EnumMap} instance, this constructor behaves
* identically to {@link #EnumMap(EnumMap)}. Otherwise, the specified map
* must contain at least one mapping (in order to determine the new
* enum map's key type).
*
* @param m the map from which to initialize this enum map
! * @throws IllegalArgumentException if {@code m} is not an
! * {@code EnumMap} instance and contains no mappings
! * @throws NullPointerException if {@code m} is null
*/
public EnumMap(Map<K, ? extends V> m) {
if (m instanceof EnumMap) {
EnumMap<K, ? extends V> em = (EnumMap<K, ? extends V>) m;
keyType = em.keyType;
*** 192,206 ****
public int size() {
return size;
}
/**
! * Returns <tt>true</tt> if this map maps one or more keys to the
* specified value.
*
* @param value the value whose presence in this map is to be tested
! * @return <tt>true</tt> if this map maps one or more keys to this value
*/
public boolean containsValue(Object value) {
value = maskNull(value);
for (Object val : vals)
--- 192,206 ----
public int size() {
return size;
}
/**
! * Returns {@code true} if this map maps one or more keys to the
* specified value.
*
* @param value the value whose presence in this map is to be tested
! * @return {@code true} if this map maps one or more keys to this value
*/
public boolean containsValue(Object value) {
value = maskNull(value);
for (Object val : vals)
*** 209,223 ****
return false;
}
/**
! * Returns <tt>true</tt> if this map contains a mapping for the specified
* key.
*
* @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;
}
--- 209,223 ----
return false;
}
/**
! * Returns {@code true} if this map contains a mapping for the specified
* key.
*
* @param key the key whose presence in this map is to be tested
! * @return {@code true} if this map contains a mapping for the specified
* key
*/
public boolean containsKey(Object key) {
return isValidKey(key) && vals[((Enum<?>)key).ordinal()] != null;
}
*** 256,268 ****
*
* @param key the key with which the specified value is to be associated
* @param value the value to be associated with the specified key
*
* @return the previous value associated with specified key, or
! * <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);
--- 256,268 ----
*
* @param key the key with which the specified value is to be associated
* @param value the value to be associated with the specified key
*
* @return the previous value associated with specified key, or
! * {@code null} if there was no mapping for key. (A {@code null}
* return can also indicate that the map previously associated
! * {@code null} with the specified key.)
* @throws NullPointerException if the specified key is null
*/
public V put(K key, V value) {
typeCheck(key);
*** 277,289 ****
/**
* Removes the mapping for this key from this map if present.
*
* @param key the key whose mapping is to be removed from the map
* @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();
--- 277,289 ----
/**
* Removes the mapping for this key from this map if present.
*
* @param key the key whose mapping is to be removed from the map
* @return the previous value associated with specified key, or
! * {@code null} if there was no entry for key. (A {@code null}
* return can also indicate that the map previously associated
! * {@code null} with the specified key.)
*/
public V remove(Object key) {
if (!isValidKey(key))
return null;
int index = ((Enum<?>)key).ordinal();
*** 642,657 ****
// Comparison and hashing
/**
* Compares the specified object with this map for equality. Returns
! * <tt>true</tt> if the given object is also a map and the two maps
* represent the same mappings, as specified in the {@link
* Map#equals(Object)} contract.
*
* @param o the object to be compared for equality with this map
! * @return <tt>true</tt> if the specified object is equal to this map
*/
public boolean equals(Object o) {
if (this == o)
return true;
if (o instanceof EnumMap)
--- 642,657 ----
// Comparison and hashing
/**
* Compares the specified object with this map for equality. Returns
! * {@code true} if the given object is also a map and the two maps
* represent the same mappings, as specified in the {@link
* Map#equals(Object)} contract.
*
* @param o the object to be compared for equality with this map
! * @return {@code true} if the specified object is equal to this map
*/
public boolean equals(Object o) {
if (this == o)
return true;
if (o instanceof EnumMap)
*** 756,766 ****
}
private static final long serialVersionUID = 458661240069192865L;
/**
! * Save the state of the <tt>EnumMap</tt> instance to a stream (i.e.,
* serialize it).
*
* @serialData The <i>size</i> of the enum map (the number of key-value
* mappings) is emitted (int), followed by the key (Object)
* and value (Object) for each key-value mapping represented
--- 756,766 ----
}
private static final long serialVersionUID = 458661240069192865L;
/**
! * Save the state of the {@code EnumMap} instance to a stream (i.e.,
* serialize it).
*
* @serialData The <i>size</i> of the enum map (the number of key-value
* mappings) is emitted (int), followed by the key (Object)
* and value (Object) for each key-value mapping represented
*** 785,795 ****
}
}
}
/**
! * Reconstitute the <tt>EnumMap</tt> instance from a stream (i.e.,
* deserialize it).
*/
@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException
--- 785,795 ----
}
}
}
/**
! * Reconstitute the {@code EnumMap} instance from a stream (i.e.,
* deserialize it).
*/
@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException
< prev index next >