Fields Summary |
---|
private final Class | keyTypeThe Class object for the enum type of all the keys of this map. |
private transient K[] | keyUniverseAll of the values comprising K. (Cached for performance.) |
private transient Object[] | valsArray representation of this map. The ith element is the value
to which universe[i] is currently mapped, or null if it isn't
mapped to anything, or NULL if it's mapped to null. |
private transient int | sizeThe number of mappings in this map. |
private static final Object | NULLDistinguished non-null value for representing null values. |
private static Enum[] | ZERO_LENGTH_ENUM_ARRAY |
private transient Set | entrySetThis field is initialized to contain an instance of the entry set
view the first time this view is requested. The view is stateless,
so there's no reason to create more than one. |
private static final long | serialVersionUID |
Methods Summary |
---|
public void | clear()Removes all mappings from this map.
Arrays.fill(vals, null);
size = 0;
|
public java.util.EnumMap | clone()Returns a shallow copy of this enum map. (The values themselves
are not cloned.
EnumMap<K, V> result = null;
try {
result = (EnumMap<K, V>) super.clone();
} catch(CloneNotSupportedException e) {
throw new AssertionError();
}
result.vals = (Object[]) result.vals.clone();
return result;
|
public boolean | containsKey(java.lang.Object key)Returns true if this map contains a mapping for the specified
key.
return isValidKey(key) && vals[((Enum)key).ordinal()] != null;
|
private boolean | containsMapping(java.lang.Object key, java.lang.Object value)
return isValidKey(key) &&
maskNull(value).equals(vals[((Enum)key).ordinal()]);
|
public boolean | containsValue(java.lang.Object value)Returns true if this map maps one or more keys to the
specified value.
value = maskNull(value);
for (Object val : vals)
if (value.equals(val))
return true;
return false;
|
public java.util.Set | entrySet()Returns a {@link Set} view of the mappings contained in this map.
The returned set obeys the general contract outlined in
{@link Map#keySet()}. The set's iterator will return the
mappings in the order their keys appear in map, which is their
natural order (the order in which the enum constants are declared).
Set<Map.Entry<K,V>> es = entrySet;
if (es != null)
return es;
else
return entrySet = new EntrySet();
|
public boolean | equals(java.lang.Object o)Compares the specified object with this map for equality. Returns
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.
if (!(o instanceof EnumMap))
return super.equals(o);
EnumMap em = (EnumMap)o;
if (em.keyType != keyType)
return size == 0 && em.size == 0;
// Key types match, compare each value
for (int i = 0; i < keyUniverse.length; i++) {
Object ourValue = vals[i];
Object hisValue = em.vals[i];
if (hisValue != ourValue &&
(hisValue == null || !hisValue.equals(ourValue)))
return false;
}
return true;
|
public V | get(java.lang.Object key)Returns the value to which this map maps the specified key, or null
if this map contains no mapping for the specified key.
return (isValidKey(key) ?
unmaskNull(vals[((Enum)key).ordinal()]) : null);
|
private boolean | isValidKey(java.lang.Object key)Returns true if key is of the proper type to be a key in this
enum map.
if (key == null)
return false;
// Cheaper than instanceof Enum followed by getDeclaringClass
Class keyClass = key.getClass();
return keyClass == keyType || keyClass.getSuperclass() == keyType;
|
public java.util.Set | keySet()Returns a {@link Set} view of the keys contained in this map.
The returned set obeys the general contract outlined in
{@link Map#keySet()}. The set's iterator will return the keys
in their natural order (the order in which the enum constants
are declared).
Set<K> ks = keySet;
if (ks != null)
return ks;
else
return keySet = new KeySet();
|
private java.lang.Object | maskNull(java.lang.Object value)
return (value == null ? NULL : value);
|
public V | put(K key, V value)Associates the specified value with the specified key in this map.
If the map previously contained a mapping for this key, the old
value is replaced.
typeCheck(key);
int index = ((Enum)key).ordinal();
Object oldValue = vals[index];
vals[index] = maskNull(value);
if (oldValue == null)
size++;
return unmaskNull(oldValue);
|
public void | putAll(java.util.Map m)Copies all of the mappings from the specified map to this map.
These mappings will replace any mappings that this map had for
any of the keys currently in the specified map.
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);
}
for (int i = 0; i < keyUniverse.length; i++) {
Object emValue = em.vals[i];
if (emValue != null) {
if (vals[i] == null)
size++;
vals[i] = emValue;
}
}
} else {
super.putAll(m);
}
|
private void | readObject(java.io.ObjectInputStream s)Reconstitute the EnumMap instance from a stream (i.e.,
deserialize it).
// Read in the key type and any hidden stuff
s.defaultReadObject();
keyUniverse = keyType.getEnumConstants();
vals = new Object[keyUniverse.length];
// Read in size (number of Mappings)
int size = s.readInt();
// Read the keys and values, and put the mappings in the HashMap
for (int i = 0; i < size; i++) {
K key = (K) s.readObject();
V value = (V) s.readObject();
put(key, value);
}
|
public V | remove(java.lang.Object key)Removes the mapping for this key from this map if present.
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(java.lang.Object key, java.lang.Object value)
if (!isValidKey(key))
return false;
int index = ((Enum)key).ordinal();
if (maskNull(value).equals(vals[index])) {
vals[index] = null;
size--;
return true;
}
return false;
|
public int | size()Returns the number of key-value mappings in this map.
return size;
|
private void | typeCheck(K key)Throws an exception if e is not of the correct type for this enum set.
Class keyClass = key.getClass();
if (keyClass != keyType && keyClass.getSuperclass() != keyType)
throw new ClassCastException(keyClass + " != " + keyType);
|
private V | unmaskNull(java.lang.Object value)
return (V) (value == NULL ? null : value);
|
public java.util.Collection | values()Returns a {@link Collection} view of the values contained in this map.
The returned collection obeys the general contract outlined in
{@link Map#values()}. The collection's iterator will return the
values in the order their corresponding keys appear in map,
which is their natural order (the order in which the enum constants
are declared).
Collection<V> vs = values;
if (vs != null)
return vs;
else
return values = new Values();
|
private void | writeObject(java.io.ObjectOutputStream s)Save the state of the EnumMap instance to a stream (i.e.,
serialize it).
// Write out the key type and any hidden stuff
s.defaultWriteObject();
// Write out size (number of Mappings)
s.writeInt(size);
// Write out keys and values (alternating)
for (Map.Entry<K,V> e : entrySet()) {
s.writeObject(e.getKey());
s.writeObject(e.getValue());
}
|