maps where it is desirable to
remove entries automatically when the key is garbage collected. This is
accomplished by using a backing map where the keys are either a
WeakReference or a SoftReference.
To create a ReferencedKeyMap the user must provide a Supplier
of the backing map and whether WeakReference or
SoftReference is to be used.
// Use HashMap and WeakReference
Map<Long, String> map = ReferencedKeyMap.create(false, HashMap::new);
map.put(10_000_000L, "a");
map.put(10_000_001L, "b");
map.put(10_000_002L, "c");
map.put(10_000_003L, "d");
map.put(10_000_004L, "e");
// Use ConcurrentHashMap and SoftReference
map = ReferencedKeyMap.create(true, ConcurrentHashMap::new);
map.put(20_000_000L, "v");
map.put(20_000_001L, "w");
map.put(20_000_002L, "x");
map.put(20_000_003L, "y");
map.put(20_000_004L, "z");
Implementation Note
Care must be given that the backing map does replacement by
replacing the value in the map entry instead of deleting the old entry and
adding a new entry, otherwise replaced entries may end up with a strongly
referenced key. HashMap and ConcurrentHashMap are known
to be safe.
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | isSoft
true if |
| private final Map | |
| private final ReferenceQueue | stale
|
| Access | Constructor and Description |
|---|---|
| private | ReferencedKeyMap(boolean
true if isSoft, Map<ReferenceKey<K>, V> SoftReference keys are to
be used, WeakReference otherwise.backing map map, ReferenceQueue<K> ReferenceQueue for cleaning up entriesPrivate constructor. |
| Modifier and Type | Method and Description |
|---|---|
| public void | clear()
Implements java. Removes all of the mappings from this map (optional operation). |
| public static < the type of keys maintained by the new map K, the type of mapped values V> Supplier | concurrentHashMapSupplier()
Returns: a supplier to create a |
| public boolean | containsKey(Object key)
Implements java. Returns |
| public boolean | containsValue(Object value)
Implements java. Returns |
| public static < the type of keys maintained by the new map K, the type of mapped values V> ReferencedKeyMap | Returns: a new map withReference keystrue if isSoft, Supplier<Map<ReferenceKey<K>, V>> SoftReference keys are to
be used, WeakReference otherwise.Supplier of the backing mapCreate a new |
| private ReferenceKey | |
| public Set | entrySet()
Implements java. Returns a |
| private static < type of key T> T | Returns: key if found otherwise nullReferencedKeyMap where interning takes placekey to test key)Check if the key already exists in the map. |
| private Stream | Returns: Stream of valid keys (unwrapped)Common routine for collecting the current set of keys. |
| public V | get(Object key)
Implements java. Returns the value to which the specified key is mapped,
or |
| private V | |
| pack-priv static < type of key T> T | Returns: the old key instance if found otherwise the new key instanceReferencedKeyMap where interning takes placekey to add key)Puts an entry where the key and the value are the same. |
| pack-priv static < type of key T> T | Returns: the old key instance if found otherwise the new key instanceReferencedKeyMap where interning takes placekey to add key, UnaryOperator<T> operation to apply to key before adding to map interner)Puts an entry where the key and the value are the same. |
| pack-priv static < type of key T> boolean | Returns: true if the key was addedReferencedKeyMap where interning takes placekey to add key)Attempt to add key to map if absent. |
| private static < type of key T> T | Returns: the old key instance if found otherwise the new key instanceReferencedKeyMap where interning takes placekey to add key)Attempt to add key to map. |
| public boolean | |
| public Set | |
| private ReferenceKey | |
| public void | |
| public V | put(K key, V newValue)
Implements java. Associates the specified value with the specified key in this map (optional operation). |
| public void | putAll(Map<? extends K, ? extends V> m)
Implements java. Copies all of the mappings from the specified map to this map (optional operation). |
| public V | putIfAbsent(K key, V newValue)
Overrides default java. If the specified key is not already associated with a value (or is mapped
to |
| public V | remove(Object key)
Implements java. Removes the mapping for a key from this map if it is present (optional operation). |
| public boolean | remove(Object key, Object value)
Overrides default java. Removes the entry for the specified key only if it is currently mapped to the specified value (optional operation). |
| public void | |
| public boolean | replace(K key, V oldValue, V newValue)
Overrides default java. Replaces the entry for the specified key only if currently mapped to the specified value (optional operation). |
| public V | replace(K key, V value)
Overrides default java. Replaces the entry for the specified key only if it is currently mapped to some value (optional operation). |
| public int | |
| public String | |
| public Collection | values()
Implements java. Returns a |
| isSoft | back to summary |
|---|---|
| private final boolean isSoft true if | |
| map | back to summary |
|---|---|
| private final Map<ReferenceKey<K>, V> map Backing | |
| stale | back to summary |
|---|---|
| private final ReferenceQueue<K> stale
| |
| ReferencedKeyMap | back to summary |
|---|---|
| private ReferencedKeyMap(boolean isSoft, Map<ReferenceKey<K>, V> map, ReferenceQueue<K> stale) Private constructor.
| |
| clear | back to summary |
|---|---|
| public void clear() Implements java. Doc from java. Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.
| |
| concurrentHashMapSupplier | back to summary |
|---|---|
public static <K, V> Supplier
| |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(Object key) Implements java. Doc from java. Returns
| |
| containsValue | back to summary |
|---|---|
| public boolean containsValue(Object value) Implements java. Doc from java. Returns
| |
| create | back to summary |
|---|---|
| public static <K, V> ReferencedKeyMap Create a new
| |
| entryKey | back to summary |
|---|---|
| private ReferenceKey Returns a key suitable for a map entry.
| |
| entrySet | back to summary |
|---|---|
| public Set Implements java. Doc from java. Returns a
| |
| existingKey | back to summary |
|---|---|
| private static <T> T existingKey(ReferencedKeyMap<T, ReferenceKey<T>> setMap, T key) Check if the key already exists in the map.
| |
| filterKeySet | back to summary |
|---|---|
| private Stream Common routine for collecting the current set of keys. | |
| get | back to summary |
|---|---|
| public V get(Object key) Implements java. Doc from java. Returns the value to which the specified key is mapped,
or More formally, if this map contains a mapping from a key
If this map permits null values, then a return value of
| |
| getNoCheckStale | back to summary |
|---|---|
| private V getNoCheckStale(Object key) | |
| intern | back to summary |
|---|---|
| pack-priv static <T> T intern(ReferencedKeyMap<T, ReferenceKey<T>> setMap, T key) Puts an entry where the key and the value are the same. Used for interning values in a set. Implementation Note Requires a
| |
| intern | back to summary |
|---|---|
| pack-priv static <T> T intern(ReferencedKeyMap<T, ReferenceKey<T>> setMap, T key, UnaryOperator<T> interner) Puts an entry where the key and the value are the same. Used for interning values in a set. Implementation Note Requires a Implementation Note This version of intern should not be called during phase1 using a lambda. Use an UnaryOperator instance instead.
| |
| internAddKey | back to summary |
|---|---|
| pack-priv static <T> boolean internAddKey(ReferencedKeyMap<T, ReferenceKey<T>> setMap, T key) Attempt to add key to map if absent.
| |
| internKey | back to summary |
|---|---|
| private static <T> T internKey(ReferencedKeyMap<T, ReferenceKey<T>> setMap, T key) Attempt to add key to map.
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Implements java. Doc from java. Returns
| |
| keySet | back to summary |
|---|---|
| public Set Implements java. Doc from java. Returns a
| |
| lookupKey | back to summary |
|---|---|
| private ReferenceKey Returns a key suitable for lookup.
| |
| prepareForAOTCache | back to summary |
|---|---|
public void prepareForAOTCache()
| |
| put | back to summary |
|---|---|
| public V put(K key, V newValue) Implements java. Doc from java. Associates the specified value with the specified key in this map
(optional operation). If the map previously contained a mapping for
the key, the old value is replaced by the specified value. (A map
| |
| putAll | back to summary |
|---|---|
| public void putAll(Map<? extends K, ? extends V> m) Implements java. Doc from java. Copies all of the mappings from the specified map to this map
(optional operation). The effect of this call is equivalent to that
of calling
| |
| putIfAbsent | back to summary |
|---|---|
| public V putIfAbsent(K key, V newValue) Overrides default java. Doc from java. If the specified key is not already associated with a value (or is mapped
to
| |
| remove | back to summary |
|---|---|
| public V remove(Object key) Implements java. Doc from java. Removes the mapping for a key from this map if it is present
(optional operation). More formally, if this map contains a mapping
from key Returns the value to which this map previously associated the key,
or If this map permits null values, then a return value of
The map will not contain a mapping for the specified key once the call returns.
| |
| remove | back to summary |
|---|---|
| public boolean remove(Object key, Object value) Overrides default java. Doc from java. Removes the entry for the specified key only if it is currently mapped to the specified value (optional operation).
| |
| removeStaleReferences | back to summary |
|---|---|
| public void removeStaleReferences() Removes enqueued weak references from map. | |
| replace | back to summary |
|---|---|
| public boolean replace(K key, V oldValue, V newValue) Overrides default java. Doc from java. Replaces the entry for the specified key only if currently mapped to the specified value (optional operation).
| |
| replace | back to summary |
|---|---|
| public V replace(K key, V value) Overrides default java. Doc from java. Replaces the entry for the specified key only if it is currently mapped to some value (optional operation).
| |
| size | back to summary |
|---|---|
| public int size() Implements java. Doc from java. Returns the number of key-value mappings in this map. If the
map contains more than
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| values | back to summary |
|---|---|
| public Collection Implements java. Doc from java. Returns a
| |