Package com.cedarsoftware.util
Class TrackingMap<K,V>
java.lang.Object
com.cedarsoftware.util.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>
A wrapper around a
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
System.out.println(trackingMap.keySet()); // Outputs: [apple, banana]
Thread Safety: This class is not thread-safe. If multiple threads access a TrackingMap
concurrently and at least one of the threads modifies the map structurally, it must be synchronized externally.
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
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 TypeMethodDescriptionvoid
clear()
Removes all the mappings from this map.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.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().Retrieves the value associated with the specified key and marks the key as accessed.Returns the underlyingMap
that thisTrackingMap
wraps.int
hashCode()
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()
Associates the specified value with the specified key in this map.void
Copies all the mappings from the specified map to this map.Removes the mapping for a key from this map if it is present.int
size()
toString()
values()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
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:
- a
Set
of accessed keys
-
getWrappedMap
Returns the underlyingMap
that thisTrackingMap
wraps.- Returns:
- the wrapped
Map
-