Class TrackingMap<K,V>
- Type Parameters:
K
- the type of keys maintained by this mapV
- the type of mapped values
- All Implemented Interfaces:
Map<K,
V>
Map
that tracks which keys have been accessed via get
or containsKey
methods.
This is useful for scenarios where it's necessary to monitor usage patterns of keys in a map,
such as identifying unused entries or optimizing memory usage by expunging rarely accessed keys.
Usage Example:
Map<String, Integer> originalMap = new HashMap<>();
originalMap.put("apple", 1);
originalMap.put("banana", 2);
originalMap.put("cherry", 3);
TrackingMap<String, Integer> trackingMap = new TrackingMap<>(originalMap);
// Access some keys
trackingMap.get("apple");
trackingMap.containsKey("banana");
// Expunge unused keys
trackingMap.expungeUnused();
// Now, "cherry" has been removed as it was not accessed
LOG.info(trackingMap.keySet()); // Outputs: [apple, banana]
Thread Safety: This class is thread-safe when wrapping concurrent map implementations
(ConcurrentMap
, ConcurrentNavigableMap
). The thread safety is provided by using
a concurrent tracking set and delegating all operations to the underlying concurrent map.
When wrapping non-concurrent maps, external synchronization is required.
Concurrent Operations: When wrapping a ConcurrentMap
or ConcurrentNavigableMap
,
this class provides additional methods that leverage the concurrent semantics of the backing map:
putIfAbsent()
, replace()
, compute*()
, merge()
, and navigation methods.
These operations maintain both the concurrent guarantees and the access tracking functionality.
Note: The expungeUnused()
method removes all entries that have not been accessed via
get(Object)
or containsKey(Object)
since the map was created or since the last call to
expungeUnused()
.
- Author:
- Sean Kellner - original version
John DeRegnaucourt - correct ConcurrentMap and ConcurrentNavigableMap support when wrapped.
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Nested Class Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionceilingEntry
(K key) Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.ceilingKey
(K key) Returns the least key greater than or equal to the given key, or null if no such key.void
clear()
Removes all the mappings from this map.Comparator<? super K>
Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).computeIfAbsent
(K key, Function<? super K, ? extends V> mappingFunction) If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null.computeIfPresent
(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.boolean
containsKey
(Object key) Returnstrue
if this map contains a mapping for the specified key.boolean
containsValue
(Object value) Returnstrue
if this map maps one or more keys to the specified value.Returns a reverse order NavigableSet view of the keys contained in this map.entrySet()
boolean
Compares the specified object with this map for equality.void
Remove the entries from the Map that have not been accessed by .get() or .containsKey().Returns a key-value mapping associated with the least key in this map, or null if the map is empty.firstKey()
Returns the first (lowest) key currently in this map.floorEntry
(K key) Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.Returns the greatest key less than or equal to the given key, or null if no such key.void
forEach
(BiConsumer<? super K, ? super V> action) Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.Retrieves the value associated with the specified key and marks the key as accessed.getOrDefault
(Object key, V defaultValue) Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.Returns the underlyingMap
that thisTrackingMap
wraps.int
hashCode()
Returns a view of the portion of this map whose keys are strictly less than toKey.Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.higherEntry
(K key) Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.Returns the least key strictly greater than the given key, or null if no such key.void
informAdditionalUsage
(TrackingMap<K, V> additional) Add the used keys from the passed in TrackingMap to this TrackingMap's keysUsed.void
informAdditionalUsage
(Collection<K> additional) Adds the accessed keys from anotherTrackingMap
to this map's set of accessed keys.boolean
isEmpty()
keySet()
keysUsed()
Returns an unmodifiable view of the keys that have been accessed viaget()
orcontainsKey()
.Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.lastKey()
Returns the last (highest) key currently in this map.lowerEntry
(K key) Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.Returns the greatest key strictly less than the given key, or null if no such key.If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.Returns a NavigableSet view of the keys contained in this map.Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.Associates the specified value with the specified key in this map.void
Copies all the mappings from the specified map to this map.putIfAbsent
(K key, V value) If the specified key is not already associated with a value, associate it with the given value.Removes the mapping for a key from this map if it is present.boolean
Removes the entry for a key only if currently mapped to a given value.Replaces the entry for a key only if currently mapped to some value.boolean
Replaces the entry for a key only if currently mapped to a given value.void
replaceAll
(BiFunction<? super K, ? super V, ? extends V> function) Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.void
replaceContents
(Map<K, V> map) Replace all contents of the wrapped map with those from the provided map.void
setWrappedMap
(Map<K, V> map) Deprecated.int
size()
Returns a view of the portion of this map whose keys range from fromKey to toKey.Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.Returns a view of the portion of this map whose keys are greater than or equal to fromKey.Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.toString()
values()
-
Constructor Details
-
TrackingMap
Wraps the providedMap
with aTrackingMap
.- Parameters:
map
- theMap
to be wrapped and tracked- Throws:
IllegalArgumentException
- if the providedmap
isnull
-
-
Method Details
-
get
Retrieves the value associated with the specified key and marks the key as accessed. -
put
Associates the specified value with the specified key in this map. -
containsKey
Returnstrue
if this map contains a mapping for the specified key. Marks the key as accessed.- Specified by:
containsKey
in interfaceMap<K,
V> - Parameters:
key
- key whose presence in this map is to be tested- Returns:
true
if this map contains a mapping for the specified key
-
putAll
Copies all the mappings from the specified map to this map.- Specified by:
putAll
in interfaceMap<K,
V> - Parameters:
m
- mappings to be stored in this map- Throws:
NullPointerException
- if the specified map isnull
-
remove
Removes the mapping for a key from this map if it is present. Also removes the key from the set of accessed keys. -
size
public int size() -
isEmpty
public boolean isEmpty() -
equals
Compares the specified object with this map for equality. -
hashCode
public int hashCode() -
toString
-
clear
public void clear()Removes all the mappings from this map. The map will be empty after this call returns. Also clears the set of accessed keys. -
containsValue
Returnstrue
if this map maps one or more keys to the specified value.- Specified by:
containsValue
in interfaceMap<K,
V> - Parameters:
value
- value whose presence in this map is to be tested- Returns:
true
if this map maps one or more keys to the specified value
-
values
- Specified by:
values
in interfaceMap<K,
V> - Returns:
- a
Collection
view of the values contained in this map
-
keySet
-
entrySet
-
expungeUnused
public void expungeUnused()Remove the entries from the Map that have not been accessed by .get() or .containsKey(). -
informAdditionalUsage
Adds the accessed keys from anotherTrackingMap
to this map's set of accessed keys. This can be useful when merging usage information from multiple tracking maps.- Parameters:
additional
- anotherTrackingMap
whose accessed keys are to be added
-
informAdditionalUsage
Add the used keys from the passed in TrackingMap to this TrackingMap's keysUsed. This can cause the readKeys to include entries that are not in wrapped Maps keys.- Parameters:
additional
- TrackingMap whose used keys are to be added to this map's used keys.
-
keysUsed
Returns an unmodifiable view of the keys that have been accessed viaget()
orcontainsKey()
.The returned set may contain objects that are not of type
K
if callers queried the map using keys of a different type.- Returns:
- unmodifiable set of accessed keys
-
getWrappedMap
Returns the underlyingMap
that thisTrackingMap
wraps.- Returns:
- the wrapped
Map
-
replaceContents
Replace all contents of the wrapped map with those from the provided map. The underlying map instance remains the same.- Parameters:
map
- map providing new contents; must not benull
-
setWrappedMap
Deprecated.UsereplaceContents(Map)
instead. This method merely replaces the contents of the wrapped map and does not change the underlying instance. -
putIfAbsent
If the specified key is not already associated with a value, associate it with the given value.Does not mark the key as accessed since this is a write operation. Available when the backing map is a
ConcurrentMap
.- Specified by:
putIfAbsent
in interfaceMap<K,
V> - Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or
null
if there was no mapping for the key - Throws:
UnsupportedOperationException
- if the wrapped map doesn't support ConcurrentMap operations
-
remove
Removes the entry for a key only if currently mapped to a given value. Also removes the key from the set of accessed keys if removal succeeds. Available when the backing map is aConcurrentMap
.- Specified by:
remove
in interfaceMap<K,
V> - Parameters:
key
- key with which the specified value is associatedvalue
- value expected to be associated with the specified key- Returns:
true
if the value was removed- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support ConcurrentMap operations
-
replace
Replaces the entry for a key only if currently mapped to a given value. Available when the backing map is aConcurrentMap
.- Specified by:
replace
in interfaceMap<K,
V> - Parameters:
key
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified key- Returns:
true
if the value was replaced- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support ConcurrentMap operations
-
replace
Replaces the entry for a key only if currently mapped to some value. Available when the backing map is aConcurrentMap
.- Specified by:
replace
in interfaceMap<K,
V> - Parameters:
key
- key with which the specified value is associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or
null
if there was no mapping for the key - Throws:
UnsupportedOperationException
- if the wrapped map doesn't support ConcurrentMap operations
-
computeIfAbsent
If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null. Marks the key as accessed since this involves reading the current value.- Specified by:
computeIfAbsent
in interfaceMap<K,
V> - Parameters:
key
- key with which the specified value is to be associatedmappingFunction
- the function to compute a value- Returns:
- the current (existing or computed) value associated with the specified key, or null if the computed value is null
-
computeIfPresent
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value. Marks the key as accessed since this involves reading the current value.- Specified by:
computeIfPresent
in interfaceMap<K,
V> - Parameters:
key
- key with which the specified value is to be associatedremappingFunction
- the function to compute a value- Returns:
- the new value associated with the specified key, or null if none
-
compute
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). Marks the key as accessed since this involves reading the current value. -
merge
If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null. Marks the key as accessed since this involves reading the current value.- Specified by:
merge
in interfaceMap<K,
V> - Parameters:
key
- key with which the resulting value is to be associatedvalue
- the non-null value to be merged with the existing valueremappingFunction
- the function to recompute a value if present- Returns:
- the new value associated with the specified key, or null if no value is associated with the key
-
getOrDefault
Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key. Marks the key as accessed.- Specified by:
getOrDefault
in interfaceMap<K,
V> - Parameters:
key
- the key whose associated value is to be returneddefaultValue
- the default mapping of the key- Returns:
- the value to which the specified key is mapped, or
defaultValue
if this map contains no mapping for the key
-
forEach
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. -
replaceAll
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.- Specified by:
replaceAll
in interfaceMap<K,
V> - Parameters:
function
- the function to apply to each entry
-
lowerEntry
Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Parameters:
key
- the key- Returns:
- an entry with the greatest key less than key, or null if no such key
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
lowerKey
Returns the greatest key strictly less than the given key, or null if no such key. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Parameters:
key
- the key- Returns:
- the greatest key less than key, or null if no such key
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
floorEntry
Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Parameters:
key
- the key- Returns:
- an entry with the greatest key less than or equal to key, or null if no such key
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
floorKey
Returns the greatest key less than or equal to the given key, or null if no such key. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Parameters:
key
- the key- Returns:
- the greatest key less than or equal to key, or null if no such key
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
ceilingEntry
Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Parameters:
key
- the key- Returns:
- an entry with the least key greater than or equal to key, or null if no such key
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
ceilingKey
Returns the least key greater than or equal to the given key, or null if no such key. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Parameters:
key
- the key- Returns:
- the least key greater than or equal to key, or null if no such key
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
higherEntry
Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Parameters:
key
- the key- Returns:
- an entry with the least key greater than key, or null if no such key
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
higherKey
Returns the least key strictly greater than the given key, or null if no such key. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Parameters:
key
- the key- Returns:
- the least key greater than key, or null if no such key
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
firstEntry
Returns a key-value mapping associated with the least key in this map, or null if the map is empty. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Returns:
- an entry with the least key, or null if this map is empty
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
lastEntry
Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty. Marks the returned key as accessed if present. Available when the backing map is aNavigableMap
.- Returns:
- an entry with the greatest key, or null if this map is empty
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
pollFirstEntry
Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty. Removes the key from tracked keys if present. Available when the backing map is aNavigableMap
.- Returns:
- the removed first entry of this map, or null if this map is empty
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
pollLastEntry
Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty. Removes the key from tracked keys if present. Available when the backing map is aNavigableMap
.- Returns:
- the removed last entry of this map, or null if this map is empty
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
descendingKeySet
Returns a reverse order NavigableSet view of the keys contained in this map. Available when the backing map is aNavigableMap
.- Returns:
- a reverse order navigable set view of the keys in this map
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
subMap
Returns a view of the portion of this map whose keys range from fromKey to toKey. Available when the backing map is aConcurrentNavigableMap
.- Parameters:
fromKey
- low endpoint of the keys in the returned mapfromInclusive
- true if the low endpoint is to be included in the returned viewtoKey
- high endpoint of the keys in the returned maptoInclusive
- true if the high endpoint is to be included in the returned view- Returns:
- a view of the portion of this map whose keys range from fromKey to toKey
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support ConcurrentNavigableMap operations
-
headMap
Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey. Available when the backing map is aNavigableMap
.- Parameters:
toKey
- high endpoint of the keys in the returned mapinclusive
- true if the high endpoint is to be included in the returned view- Returns:
- a view of the portion of this map whose keys are less than toKey
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
tailMap
Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey. Available when the backing map is aNavigableMap
.- Parameters:
fromKey
- low endpoint of the keys in the returned mapinclusive
- true if the low endpoint is to be included in the returned view- Returns:
- a view of the portion of this map whose keys are greater than fromKey
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
comparator
Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys. Available when the backing map is aSortedMap
.- Returns:
- the comparator used to order the keys in this map, or null
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support SortedMap operations
-
subMap
Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. Available when the backing map is aNavigableMap
.- Parameters:
fromKey
- low endpoint (inclusive) of the keys in the returned maptoKey
- high endpoint (exclusive) of the keys in the returned map- Returns:
- a view of the portion of this map whose keys range from fromKey to toKey
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
headMap
Returns a view of the portion of this map whose keys are strictly less than toKey. Available when the backing map is aNavigableMap
.- Parameters:
toKey
- high endpoint (exclusive) of the keys in the returned map- Returns:
- a view of the portion of this map whose keys are less than toKey
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
tailMap
Returns a view of the portion of this map whose keys are greater than or equal to fromKey. Available when the backing map is aNavigableMap
.- Parameters:
fromKey
- low endpoint (inclusive) of the keys in the returned map- Returns:
- a view of the portion of this map whose keys are greater than or equal to fromKey
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support NavigableMap operations
-
firstKey
Returns the first (lowest) key currently in this map. Marks the returned key as accessed. Available when the backing map is aSortedMap
.- Returns:
- the first (lowest) key currently in this map
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support SortedMap operations
-
lastKey
Returns the last (highest) key currently in this map. Marks the returned key as accessed. Available when the backing map is aSortedMap
.- Returns:
- the last (highest) key currently in this map
- Throws:
UnsupportedOperationException
- if the wrapped map doesn't support SortedMap operations
-
replaceContents(Map)
instead.