Methods Summary |
---|
public void | clear()Removes all elements from this {@code EnumMap}, leaving it empty.
Arrays.fill(values, null);
Arrays.fill(hasMapping, false);
mappingsCount = 0;
|
public java.util.EnumMap | clone()Returns a shallow copy of this {@code EnumMap}.
try {
EnumMap<K, V> enumMap = (EnumMap<K, V>) super.clone();
enumMap.initialization(this);
return enumMap;
} catch (CloneNotSupportedException e) {
return null;
}
|
public boolean | containsKey(java.lang.Object key)Returns whether this {@code EnumMap} contains the specified key.
if (isValidKeyType(key)) {
int keyOrdinal = ((Enum) key).ordinal();
return hasMapping[keyOrdinal];
}
return false;
|
public boolean | containsValue(java.lang.Object value)Returns whether this {@code EnumMap} contains the specified value.
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.Set | entrySet()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.
if (null == entrySet) {
entrySet = new EnumMapEntrySet<K, V>(this);
}
return entrySet;
|
public boolean | equals(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.
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 V | get(java.lang.Object key)Returns the value of the mapping with the specified key.
if (!isValidKeyType(key)) {
return null;
}
int keyOrdinal = ((Enum) key).ordinal();
return (V) values[keyOrdinal];
|
private void | initialization(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 void | initialization(java.lang.Class type)
keyType = type;
keys = keyType.getEnumConstants();
enumSize = keys.length;
values = new Object[enumSize];
hasMapping = new boolean[enumSize];
|
private boolean | isValidKeyType(java.lang.Object key)
if (null != key && keyType.isInstance(key)) {
return true;
}
return false;
|
public java.util.Set | keySet()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.
if (null == keySet) {
keySet = new EnumMapKeySet<K, V>(this);
}
return keySet;
|
public V | put(K key, V value)Maps the specified key to the specified value.
return putImpl(key, value);
|
public void | putAll(java.util.Map map)Copies every mapping in the specified {@code Map} to this {@code EnumMap}.
putAllImpl(map);
|
private void | putAllImpl(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 V | putImpl(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 void | readObject(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 V | remove(java.lang.Object key)Removes a mapping with the specified key from this {@code EnumMap}.
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 int | size()Returns the number of elements in this {@code EnumMap}.
return mappingsCount;
|
public java.util.Collection | values()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).
if (null == valuesCollection) {
valuesCollection = new EnumMapValueCollection<K, V>(this);
}
return valuesCollection;
|
private void | writeObject(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());
}
|