FileDocCategorySizeDatePackage
EnumMap.javaAPI DocAndroid 1.5 API24458Wed May 06 22:41:04 BST 2009java.util

EnumMap

public class EnumMap extends AbstractMap implements Serializable, Cloneable, Map
An {@code Map} specialized for use with {@code Enum} types as keys.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private Class
keyType
transient Enum[]
keys
transient Object[]
values
transient boolean[]
hasMapping
private transient int
mappingsCount
transient int
enumSize
private transient EnumMapEntrySet
entrySet
Constructors Summary
public EnumMap(Class keyType)
Constructs an empty {@code EnumMap} using the given key type.

param
keyType the class object giving the type of the keys used by this {@code EnumMap}.
since
Android 1.0

        initialization(keyType);
    
public EnumMap(EnumMap map)
Constructs an {@code EnumMap} using the same key type as the given {@code EnumMap} and initially containing the same mappings.

param
map the {@code EnumMap} from which this {@code EnumMap} is initialized.
since
Android 1.0

        initialization(map);
    
public EnumMap(Map map)
Constructs an {@code EnumMap} initialized from the given map. If the given map is an {@code EnumMap} instance, this constructor behaves in the exactly the same way as {@link EnumMap#EnumMap(EnumMap)}}. Otherwise, the given map should contain at least one mapping.

param
map the map from which this {@code EnumMap} is initialized.
throws
IllegalArgumentException if {@code map} is not an {@code EnumMap} instance and does not contain any mappings.
since
Android 1.0

        if (map instanceof EnumMap) {
            initialization((EnumMap<K, V>) map);
        } else {
            if (0 == map.size()) {
                throw new IllegalArgumentException();
            }
            Iterator<K> iter = map.keySet().iterator();
            K enumKey = iter.next();
            Class clazz = enumKey.getClass();
            if (clazz.isEnum()) {
                initialization(clazz);
            } else {
                initialization(clazz.getSuperclass());
            }
            putAllImpl(map);
        }
    
Methods Summary
public voidclear()
Removes all elements from this {@code EnumMap}, leaving it empty.

see
#isEmpty()
see
#size()
since
Android 1.0

        Arrays.fill(values, null);
        Arrays.fill(hasMapping, false);
        mappingsCount = 0;
    
public java.util.EnumMapclone()
Returns a shallow copy of this {@code EnumMap}.

return
a shallow copy of this {@code EnumMap}.
since
Android 1.0

        try {
            EnumMap<K, V> enumMap = (EnumMap<K, V>) super.clone();
            enumMap.initialization(this);
            return enumMap;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    
public booleancontainsKey(java.lang.Object key)
Returns whether this {@code EnumMap} contains the specified key.

param
key the key to search for.
return
{@code true} if this {@code EnumMap} contains the specified key, {@code false} otherwise.
since
Android 1.0

        if (isValidKeyType(key)) {
            int keyOrdinal = ((Enum) key).ordinal();
            return hasMapping[keyOrdinal];
        }
        return false;
    
public booleancontainsValue(java.lang.Object value)
Returns whether this {@code EnumMap} contains the specified value.

param
value the value to search for.
return
{@code true} if this {@code EnumMap} contains the specified value, {@code false} otherwise.
since
Android 1.0

        if (null == value) {
            for (int i = 0; i < enumSize; i++) {
                if (hasMapping[i] && null == values[i]) {
                    return true;
                }
            }
        } else {
            for (int i = 0; i < enumSize; i++) {
                if (hasMapping[i] && value.equals(values[i])) {
                    return true;
                }
            }
        }
        return false;
    
public java.util.SetentrySet()
Returns a {@code Set} containing all of the mappings in this {@code EnumMap}. Each mapping is an instance of {@link Map.Entry}. As the {@code Set} is backed by this {@code EnumMap}, changes in one will be reflected in the other.

return
a {@code Set} of the mappings.
since
Android 1.0

        if (null == entrySet) {
            entrySet = new EnumMapEntrySet<K, V>(this);
        }
        return entrySet;
    
public booleanequals(java.lang.Object object)
Compares the argument to the receiver, and returns {@code true} if the specified {@code Object} is an {@code EnumMap} and both {@code EnumMap}s contain the same mappings.

param
object the {@code Object} to compare with this {@code EnumMap}.
return
boolean {@code true} if {@code object} is the same as this {@code EnumMap}, {@code false} otherwise.
see
#hashCode()
see
#entrySet()
since
Android 1.0

        if (this == object) {
            return true;
        }
        if (!(object instanceof EnumMap)) {
            return super.equals(object);
        }
        EnumMap<K, V> enumMap = (EnumMap<K, V>) object;
        if (keyType != enumMap.keyType || size() != enumMap.size()) {
            return false;
        }
        return Arrays.equals(hasMapping, enumMap.hasMapping)
                && Arrays.equals(values, enumMap.values);
    
public Vget(java.lang.Object key)
Returns the value of the mapping with the specified key.

param
key the key.
return
the value of the mapping with the specified key, or {@code null} if no mapping for the specified key is found.
since
Android 1.0

        if (!isValidKeyType(key)) {
            return null;
        }
        int keyOrdinal = ((Enum) key).ordinal();
        return (V) values[keyOrdinal];
    
private voidinitialization(java.util.EnumMap enumMap)

        keyType = enumMap.keyType;
        keys = enumMap.keys;
        enumSize = enumMap.enumSize;
        values = enumMap.values.clone();
        hasMapping = enumMap.hasMapping.clone();
        mappingsCount = enumMap.mappingsCount;
    
private voidinitialization(java.lang.Class type)

        keyType = type;
        keys = keyType.getEnumConstants();
        enumSize = keys.length;
        values = new Object[enumSize];
        hasMapping = new boolean[enumSize];
    
private booleanisValidKeyType(java.lang.Object key)

        if (null != key && keyType.isInstance(key)) {
            return true;
        }
        return false;
    
public java.util.SetkeySet()
Returns a set of the keys contained in this {@code EnumMap}. The {@code Set} is backed by this {@code EnumMap} so changes to one are reflected in the other. The {@code Set} does not support adding.

return
a {@code Set} of the keys.
since
Android 1.0

        if (null == keySet) {
            keySet = new EnumMapKeySet<K, V>(this);
        }
        return keySet;
    
public Vput(K key, V value)
Maps the specified key to the specified value.

param
key the key.
param
value the value.
return
the value of any previous mapping with the specified key or {@code null} if there was no mapping.
throws
UnsupportedOperationException if adding to this map is not supported.
throws
ClassCastException if the class of the key or value is inappropriate for this map.
throws
IllegalArgumentException if the key or value cannot be added to this map.
throws
NullPointerException if the key or value is {@code null} and this {@code EnumMap} does not support {@code null} keys or values.
since
Android 1.0

        return putImpl(key, value);
    
public voidputAll(java.util.Map map)
Copies every mapping in the specified {@code Map} to this {@code EnumMap}.

param
map the {@code Map} to copy mappings from.
throws
UnsupportedOperationException if adding to this {@code EnumMap} is not supported.
throws
ClassCastException if the class of a key or value is inappropriate for this {@code EnumMap}.
throws
IllegalArgumentException if a key or value cannot be added to this map.
throws
NullPointerException if a key or value is {@code null} and this {@code EnumMap} does not support {@code null} keys or values.
since
Android 1.0

        putAllImpl(map);
    
private voidputAllImpl(java.util.Map map)

        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            putImpl((K) entry.getKey(), (V) entry.getValue());
        }
    
private VputImpl(K key, V value)

        if (null == key) {
            throw new NullPointerException();
        }
        if (!isValidKeyType(key)) {
            throw new ClassCastException();
        }
        int keyOrdinal = key.ordinal();
        if (!hasMapping[keyOrdinal]) {
            hasMapping[keyOrdinal] = true;
            mappingsCount++;
        }
        V oldValue = (V) values[keyOrdinal];
        values[keyOrdinal] = value;
        return oldValue;
    
private voidreadObject(java.io.ObjectInputStream stream)

        stream.defaultReadObject();
        initialization(keyType);
        int elementCount = stream.readInt();
        Enum<K> enumKey;
        Object value;
        for (int i = elementCount; i > 0; i--) {
            enumKey = (Enum<K>) stream.readObject();
            value = stream.readObject();
            putImpl((K) enumKey, (V) value);
        }
    
public Vremove(java.lang.Object key)
Removes a mapping with the specified key from this {@code EnumMap}.

param
key the key of the mapping to remove.
return
the value of the removed mapping or {@code null} if no mapping for the specified key was found.
throws
UnsupportedOperationException if removing from this {@code EnumMap} is not supported.
since
Android 1.0

        if (!isValidKeyType(key)) {
            return null;
        }
        int keyOrdinal = ((Enum) key).ordinal();
        if (hasMapping[keyOrdinal]) {
            hasMapping[keyOrdinal] = false;
            mappingsCount--;
        }
        V oldValue = (V) values[keyOrdinal];
        values[keyOrdinal] = null;
        return oldValue;
    
public intsize()
Returns the number of elements in this {@code EnumMap}.

return
the number of elements in this {@code EnumMap}.
since
Android 1.0

        return mappingsCount;
    
public java.util.Collectionvalues()
Returns a {@code Collection} of the values contained in this {@code EnumMap}. The returned {@code Collection} complies with the general rule specified in {@link Map#values()}. The {@code Collection}'s {@code Iterator} will return the values in the their corresponding keys' natural order (the {@code Enum} constants are declared in this order).

return
a collection of the values contained in this {@code EnumMap}.
since
Android 1.0

        if (null == valuesCollection) {
            valuesCollection = new EnumMapValueCollection<K, V>(this);
        }
        return valuesCollection;
    
private voidwriteObject(java.io.ObjectOutputStream stream)

        stream.defaultWriteObject();
        stream.writeInt(mappingsCount);
        Iterator<Map.Entry<K, V>> iterator = entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<K, V> entry = iterator.next();
            stream.writeObject(entry.getKey());
            stream.writeObject(entry.getValue());
        }