Class AbstractPackedMap<K,V>
- java.lang.Object
-
- ej.basictool.map.AbstractPackedMap<K,V>
-
- Type Parameters:
K- the type of keys maintained by this map.V- the type of mapped values.
- Direct Known Subclasses:
AbstractWeakPackedMap,IdentityPackedMap,PackedMap
public abstract class AbstractPackedMap<K,V> extends Object
An object that maps keys to values. A packed map cannot contain duplicate keys; each key can map to at most one value.The main goal of this implementation, compared to a standard
Map, is to have a small heap footprint. For instance, it doesn't provide direct access to(key,value)entries. This allows to simplify the implementation.It uses a single array containing both keys and values that is fitted to the content (enlarged when an entry is added, shrunk when an entry is removed). The array contains first all the keys then all the values. The keys are sorted by their hashcode value. A key is retrieved first using a dichotomic search on its hashcode, then using a linear search within keys having the same hashcode.
-
-
Field Summary
Fields Modifier and Type Field Description protected Object[]keysValuesKeys and values of the map.
-
Constructor Summary
Constructors Constructor Description AbstractPackedMap()Constructs an empty map.AbstractPackedMap(AbstractPackedMap<K,V> map)Constructs a map with the same mappings as the specified map.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all of the mappings from this map.abstract Objectclone()Returns a shallow copy of this map instance: the keys and values themselves are not cloned.booleancontainsKey(Object key)Returnstrueif this map contains a mapping for the specified key.protected booleancontainsUnwrappedKey(Object key)Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(Object value)Returnstrueif this map maps one or more keys to the specified value.booleanequals(Object o)Compares the specified object with this map for equality.Vget(Object key)Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.protected intgetKeyHashCode(Object key)Gets the hash code of a key.protected intgetWrappedKeyHashCode(Object wrappedKey)Gets the hash code of a key stored in the packed map.inthashCode()Returns the hash code value for this map.booleanisEmpty()Returnstrueif this map contains no key-value mappings.protected abstract booleanisSame(Object key, Object candidateKey)Checks whether two keys are equal or not.Set<K>keySet()Returns an unmodifiableSetview of the keys contained in this map.Vput(K key, V value)Associates the specified value with the specified key in this map.Vremove(Object key)Removes the mapping for a key from this map if it is present.intsize()Returns the number of key-value mappings in this map.protected KunwrapKey(Object wrappedKey)Unwraps a wrapped key.Collection<V>values()Returns an unmodifiableCollectionview of the values contained in this map.protected ObjectwrapKey(K key)Wraps a key.
-
-
-
Field Detail
-
keysValues
protected Object[] keysValues
Keys and values of the map.
-
-
Constructor Detail
-
AbstractPackedMap
public AbstractPackedMap()
Constructs an empty map.
-
AbstractPackedMap
public AbstractPackedMap(AbstractPackedMap<K,V> map)
Constructs a map with the same mappings as the specified map.- Parameters:
map- the map whose mappings are to be placed in this map.- Throws:
NullPointerException- if the specified map isnull.
-
-
Method Detail
-
clear
public void clear()
Removes all of the mappings from this map. The map will be empty after this call returns.- Throws:
UnsupportedOperationException- if theclearoperation is not supported by this map
-
clone
public abstract Object clone()
Returns a shallow copy of this map instance: the keys and values themselves are not cloned.
-
containsKey
public boolean containsKey(Object key)
Returnstrueif this map contains a mapping for the specified key. More formally, returnstrueif and only if this map contains a mapping for a key same than the given key (seeisSame(Object, Object)). (There can be at most one such mapping.)- Parameters:
key- key whose presence in this map is to be tested.- Returns:
trueif this map contains a mapping for the specified key.- Throws:
NullPointerException- if the specified key isnull.
-
containsValue
public boolean containsValue(@Nullable Object value)
Returnstrueif this map maps one or more keys to the specified value. More formally, returnstrueif and only if this map contains at least one mapping to a valuevsuch that(value==null ? v==null : value.equals(v)). This operation requires time linear in the map size.- Parameters:
value- value whose presence in this map is to be tested.- Returns:
trueif this map maps one or more keys to the specified value.
-
equals
public boolean equals(@Nullable Object o)
Compares the specified object with this map for equality. Returnstrueif the given object is also a map and the two maps represent the same mappings.- Overrides:
equalsin classObject- Parameters:
o- object to be compared for equality with this map- Returns:
trueif the specified object is equal to this map- See Also:
Object.hashCode(),HashMap
-
get
@Nullable public V get(Object key)
Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.More formally, if this map contains a mapping from a key
kto a valuevsuch that key is the same as k (seeisSame(Object, Object)), then this method returnsvotherwise it returnsnull. (There can be at most one such mapping.)A return value of
nulldoes not necessarily indicate that the map contains no mapping for the key: it is also possible that the map explicitly maps the key tonull. ThecontainsKey(key)operation may be used to distinguish these two cases.- Parameters:
key- the key whose associated value is to be returned.- Returns:
- the value to which the specified key is mapped, or
nullif this map contains no mapping for the key. - Throws:
NullPointerException- if the specified key isnull.
-
hashCode
public int hashCode()
Returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry (key-value mapping) in the map. This ensures thatm1.equals(m2)implies thatm1.hashCode()==m2.hashCode()for any two mapsm1andm2, as required by the general contract ofObject.hashCode().- Overrides:
hashCodein classObject- Returns:
- the hash code value for this map.
- See Also:
Object.equals(Object),equals(Object)
-
isEmpty
public boolean isEmpty()
Returnstrueif this map contains no key-value mappings.- Returns:
trueif this map contains no key-value mappings
-
keySet
public Set<K> keySet()
Returns an unmodifiableSetview of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set cannot be modified and attempts to modify the returned collection, whether direct or via its iterator, result in anUnsupportedOperationException.- Returns:
- a set view of the keys contained in this map.
-
put
@Nullable 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 the key, the old value is replaced by the specified value. (A mapmis said to contain a mapping for a keykif and only ifm.containsKey(k)would returntrue.)- Parameters:
key- key with which the specified value is to be associated.value- value to be associated with the specified key.- Returns:
- the previous value associated with
key, ornullif there was no mapping forkey. (Anullreturn can also indicate that the map previously associatednullwithkey.) - Throws:
NullPointerException- if the specified key isnull.
-
getKeyHashCode
protected int getKeyHashCode(Object key)
Gets the hash code of a key.By default, returns the hash code of the given object.
- Parameters:
key- the key.- Returns:
- the hash code.
-
getWrappedKeyHashCode
protected int getWrappedKeyHashCode(Object wrappedKey)
Gets the hash code of a key stored in the packed map. The given object may not be the actual key because it has been wrapped bywrapKey(Object).By default, the given parameter is unwrapped using
unwrapKey(Object)then the hash code is computed usinggetKeyHashCode(Object).- Parameters:
wrappedKey- the wrapped key.- Returns:
- the hash code.
-
wrapKey
protected Object wrapKey(K key)
Wraps a key. The result of this method will be stored in the packed map.By default, the key is returned.
- Parameters:
key- the key to wrap.- Returns:
- the wrapped key.
-
unwrapKey
@Nullable protected K unwrapKey(Object wrappedKey)
Unwraps a wrapped key. The given wrapper is an object stored in the packed map (seewrapKey(Object)). The result is the key contained by this wrapper.By default, the given object is returned (casted in the right type).
Beware that the returned key may be
null, for instance if the key is wrapped in a weak reference. In this case, to avoid throwingNullPointerException, the subclass must at least check thenullparameters inisSame(Object, Object),containsKey(Object)andgetKeyHashCode(Object).- Parameters:
wrappedKey- the object to unwrap.- Returns:
- the key.
-
remove
@Nullable public V remove(Object key)
Removes the mapping for a key from this map if it is present. More formally, if this map contains a mapping for a key that is the same as the given key (seeisSame(Object, Object)), that mapping is removed. (The map can contain at most one such mapping.)Returns the value to which this map previously associated the key, or
nullif the map contained no mapping for the key.A return value of
nulldoes not necessarily indicate that the map contained no mapping for the key it's also possible that the map explicitly mapped the key tonull.The map will not contain a mapping for the specified key once the call returns.
- Parameters:
key- key whose mapping is to be removed from the map.- Returns:
- the previous value associated with
key, ornullif there was no mapping forkey. - Throws:
NullPointerException- if the specified key isnull.
-
size
public int size()
Returns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.- Returns:
- the number of key-value mappings in this map
-
values
public Collection<V> values()
Returns an unmodifiableCollectionview of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined. The collection cannot be modified and attempts to modify the returned collection, whether direct or via its iterator, result in anUnsupportedOperationException.- Returns:
- a collection view of the values contained in this map
-
isSame
protected abstract boolean isSame(Object key, @Nullable Object candidateKey)
Checks whether two keys are equal or not. The key must not benull.- Parameters:
key- the key searched in the map.candidateKey- the key in the map to compare with. Can benullif and only ifunwrapKey(Object)implementation can returnnull.- Returns:
trueif the two keys are equal,falseotherwise.
-
containsUnwrappedKey
protected boolean containsUnwrappedKey(@Nullable Object key)
Returnstrueif this map contains a mapping for the specified key. More formally, returnstrueif and only if this map contains a mapping for a key same than the given key (seeisSame(Object, Object)). (There can be at most one such mapping.)- Parameters:
key- key whose presence in this map is to be tested. Can benullif and only ifunwrapKey(Object)implementation can returnnull.- Returns:
trueif this map contains a mapping for the specified key.- Throws:
NullPointerException- if the specified key isnull.
-
-