K
- the type of keys maintained by this map.V
- the type of mapped values.public abstract class AbstractPackedMap<K,V> extends Object
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.
Modifier and Type | Field and Description |
---|---|
protected Object[] |
keysValues
Keys and values of the map.
|
Constructor and Description |
---|
AbstractPackedMap()
Constructs an empty map.
|
AbstractPackedMap(AbstractPackedMap<K,V> map)
Constructs a map with the same mappings as the specified map.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all of the mappings from this map.
|
abstract Object |
clone()
Returns a shallow copy of this map instance: the keys and values themselves are not cloned.
|
boolean |
containsKey(Object key)
Returns
true if this map contains a mapping for the specified key. |
protected boolean |
containsUnwrappedKey(Object key)
Returns
true if this map contains a mapping for the specified key. |
boolean |
containsValue(Object value)
Returns
true if this map maps one or more keys to the specified value. |
boolean |
equals(Object o)
Compares the specified object with this map for equality.
|
V |
get(Object key)
Returns the value to which the specified key is mapped, or
null if this map contains no mapping for the
key. |
protected int |
getKeyHashCode(Object key)
Gets the hash code of a key.
|
protected int |
getWrappedKeyHashCode(Object wrappedKey)
Gets the hash code of a key stored in the packed map.
|
int |
hashCode()
Returns the hash code value for this map.
|
boolean |
isEmpty()
Returns
true if this map contains no key-value mappings. |
protected abstract boolean |
isSame(Object key,
Object candidateKey)
Checks whether two keys are equal or not.
|
Set<K> |
keySet()
Returns an unmodifiable
Set view of the keys contained in this map. |
V |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
V |
remove(Object key)
Removes the mapping for a key from this map if it is present.
|
int |
size()
Returns the number of key-value mappings in this map.
|
protected K |
unwrapKey(Object wrappedKey)
Unwraps a wrapped key.
|
Collection<V> |
values()
Returns an unmodifiable
Collection view of the values contained in this map. |
protected Object |
wrapKey(K key)
Wraps a key.
|
protected Object[] keysValues
public AbstractPackedMap()
public AbstractPackedMap(AbstractPackedMap<K,V> map)
map
- the map whose mappings are to be placed in this map.NullPointerException
- if the specified map is null
.public void clear()
UnsupportedOperationException
- if the clear
operation is not supported by this mappublic abstract Object clone()
public boolean containsKey(Object key)
true
if this map contains a mapping for the specified key. More formally, returns true
if
and only if this map contains a mapping for a key same than the given key (see isSame(Object, Object)
).
(There can be at most one such mapping.)key
- key whose presence in this map is to be tested.true
if this map contains a mapping for the specified key.NullPointerException
- if the specified key is null
.protected boolean containsUnwrappedKey(@Nullable Object key)
true
if this map contains a mapping for the specified key. More formally, returns true
if
and only if this map contains a mapping for a key same than the given key (see isSame(Object, Object)
).
(There can be at most one such mapping.)key
- key whose presence in this map is to be tested. Can be null
if and only if
unwrapKey(Object)
implementation can return null
.true
if this map contains a mapping for the specified key.NullPointerException
- if the specified key is null
.public boolean containsValue(@Nullable Object value)
true
if this map maps one or more keys to the specified value. More formally, returns
true
if and only if this map contains at least one mapping to a value v
such that
(value==null ? v==null : value.equals(v))
. This operation requires time linear in the map size.value
- value whose presence in this map is to be tested.true
if this map maps one or more keys to the specified value.public boolean equals(@Nullable Object o)
true
if the given object is also a map
and the two maps represent the same mappings.equals
in class Object
o
- object to be compared for equality with this maptrue
if the specified object is equal to this mapObject.hashCode()
,
HashMap
@Nullable public V get(Object key)
null
if this map contains no mapping for the
key.
More formally, if this map contains a mapping from a key k
to a value v
such that key is the same
as k (see isSame(Object, Object)
), then this method returns v
otherwise it returns null
.
(There can be at most one such mapping.)
A return value of null
does not necessarily indicate that the map contains no mapping for the key:
it is also possible that the map explicitly maps the key to null
. The containsKey(key)
operation may be used to distinguish these two cases.
key
- the key whose associated value is to be returned.null
if this map contains no mapping for the
key.NullPointerException
- if the specified key is null
.protected int getKeyHashCode(Object key)
By default, returns the hash code of the given object.
key
- the key.protected int getWrappedKeyHashCode(Object wrappedKey)
wrapKey(Object)
.
By default, the given parameter is unwrapped using unwrapKey(Object)
then the hash code is computed
using getKeyHashCode(Object)
.
wrappedKey
- the wrapped key.public int hashCode()
m1.equals(m2)
implies that
m1.hashCode()==m2.hashCode()
for any two maps m1
and m2
, as required by the general
contract of Object.hashCode()
.hashCode
in class Object
Object.equals(Object)
,
equals(Object)
public boolean isEmpty()
true
if this map contains no key-value mappings.true
if this map contains no key-value mappingsprotected abstract boolean isSame(Object key, @Nullable Object candidateKey)
null
.key
- the key searched in the map.candidateKey
- the key in the map to compare with. Can be null
if and only if unwrapKey(Object)
implementation can return null
.true
if the two keys are equal, false
otherwise.public Set<K> keySet()
Set
view 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 an UnsupportedOperationException
.@Nullable public V put(K key, V value)
m
is said to contain a mapping for a
key k
if and only if m.containsKey(k)
would return true
.)key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.key
, or null
if there was no mapping for key
.
(A null
return can also indicate that the map previously associated null
with
key
.)NullPointerException
- if the specified key is null
.@Nullable public V remove(Object key)
isSame(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 null
if the map contained no
mapping for the key.
A return value of null
does not necessarily indicate that the map contained no mapping for the key
it's also possible that the map explicitly mapped the key to null
.
The map will not contain a mapping for the specified key once the call returns.
key
- key whose mapping is to be removed from the map.key
, or null
if there was no mapping for key
.NullPointerException
- if the specified key is null
.public int size()
Integer.MAX_VALUE
elements, returns Integer.MAX_VALUE
.@Nullable protected K unwrapKey(Object wrappedKey)
wrapKey(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 throwing NullPointerException
, the subclass must at least check the null
parameters in isSame(Object, Object)
, containsKey(Object)
and getKeyHashCode(Object)
.
wrappedKey
- the object to unwrap.public Collection<V> values()
Collection
view 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 an
UnsupportedOperationException
.