Class AbstractWeakPackedMap<K,V>
- java.lang.Object
-
- ej.basictool.map.AbstractPackedMap<K,V>
-
- ej.basictool.map.AbstractWeakPackedMap<K,V>
-
- Type Parameters:
K- the type of keys maintained by this map.V- the type of mapped values.
- Direct Known Subclasses:
WeakIdentityPackedMap,WeakPackedMap
public abstract class AbstractWeakPackedMap<K,V> extends AbstractPackedMap<K,V>
-
-
Field Summary
-
Fields inherited from class ej.basictool.map.AbstractPackedMap
keysValues
-
-
Constructor Summary
Constructors Constructor Description AbstractWeakPackedMap()Constructs an empty map.AbstractWeakPackedMap(AbstractWeakPackedMap<K,V> map)Constructs a map with the same mappings as the specified map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.Vget(Object key)Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.protected intgetWrappedKeyHashCode(Object wrappedKey)Gets the hash code of a key stored in the packed map.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.protected ObjectwrapKey(K key)Wraps a key.-
Methods inherited from class ej.basictool.map.AbstractPackedMap
clear, clone, equals, getKeyHashCode, hashCode, isEmpty, isSame, values
-
-
-
-
Constructor Detail
-
AbstractWeakPackedMap
public AbstractWeakPackedMap()
Constructs an empty map.
-
AbstractWeakPackedMap
public AbstractWeakPackedMap(AbstractWeakPackedMap<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
-
containsKey
public boolean containsKey(Object key)
Description copied from class:AbstractPackedMapReturnstrueif 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 (seeAbstractPackedMap.isSame(Object, Object)). (There can be at most one such mapping.)- Overrides:
containsKeyin classAbstractPackedMap<K,V>- Parameters:
key- key whose presence in this map is to be tested.- Returns:
trueif this map contains a mapping for the specified key.
-
containsUnwrappedKey
protected boolean containsUnwrappedKey(@Nullable Object key)
Description copied from class:AbstractPackedMapReturnstrueif 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 (seeAbstractPackedMap.isSame(Object, Object)). (There can be at most one such mapping.)- Overrides:
containsUnwrappedKeyin classAbstractPackedMap<K,V>- Parameters:
key- key whose presence in this map is to be tested. Can benullif and only ifAbstractPackedMap.unwrapKey(Object)implementation can returnnull.- Returns:
trueif this map contains a mapping for the specified key.
-
containsValue
public boolean containsValue(@Nullable Object value)
Description copied from class:AbstractPackedMapReturnstrueif 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.- Overrides:
containsValuein classAbstractPackedMap<K,V>- 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.
-
get
@Nullable public V get(Object key)
Description copied from class:AbstractPackedMapReturns 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 (seeAbstractPackedMap.isSame(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.- Overrides:
getin classAbstractPackedMap<K,V>- 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.
-
keySet
public Set<K> keySet()
Description copied from class:AbstractPackedMapReturns 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.- Overrides:
keySetin classAbstractPackedMap<K,V>- Returns:
- a set view of the keys contained in this map.
-
put
@Nullable public V put(K key, V value)
Description copied from class:AbstractPackedMapAssociates 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.)- Overrides:
putin classAbstractPackedMap<K,V>- 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.)
-
remove
@Nullable public V remove(Object key)
Description copied from class:AbstractPackedMapRemoves 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 (seeAbstractPackedMap.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
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.
- Overrides:
removein classAbstractPackedMap<K,V>- 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.
-
size
public int size()
Description copied from class:AbstractPackedMapReturns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.- Overrides:
sizein classAbstractPackedMap<K,V>- Returns:
- the number of key-value mappings in this map
-
wrapKey
protected Object wrapKey(K key)
Description copied from class:AbstractPackedMapWraps a key. The result of this method will be stored in the packed map.By default, the key is returned.
- Overrides:
wrapKeyin classAbstractPackedMap<K,V>- Parameters:
key- the key to wrap.- Returns:
- the wrapped key.
-
getWrappedKeyHashCode
protected int getWrappedKeyHashCode(Object wrappedKey)
Description copied from class:AbstractPackedMapGets 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 byAbstractPackedMap.wrapKey(Object).By default, the given parameter is unwrapped using
AbstractPackedMap.unwrapKey(Object)then the hash code is computed usingAbstractPackedMap.getKeyHashCode(Object).- Overrides:
getWrappedKeyHashCodein classAbstractPackedMap<K,V>- Parameters:
wrappedKey- the wrapped key.- Returns:
- the hash code.
-
unwrapKey
@Nullable protected K unwrapKey(Object wrappedKey)
Description copied from class:AbstractPackedMapUnwraps a wrapped key. The given wrapper is an object stored in the packed map (seeAbstractPackedMap.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 throwingNullPointerException, the subclass must at least check thenullparameters inAbstractPackedMap.isSame(Object, Object),AbstractPackedMap.containsKey(Object)andAbstractPackedMap.getKeyHashCode(Object).- Overrides:
unwrapKeyin classAbstractPackedMap<K,V>- Parameters:
wrappedKey- the object to unwrap.- Returns:
- the key.
-
-