Class ExpiringHashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.concurrent.ConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
de.bsommerfeld.pathetic.engine.util.ExpiringHashMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
Serializable,ConcurrentMap<K,,ExpiringHashMap.Entry<V>> Map<K,ExpiringHashMap.Entry<V>>
A
ConcurrentHashMap that removes entries on access if they are expired.
The default expiration time is 5 minutes. The cleanup itself is only being triggered every 5 minutes on access by default, but both values can be configured.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA wrapper for values stored in the ExpiringHashMap that includes an expiration time.Nested classes/interfaces inherited from class java.util.concurrent.ConcurrentHashMap
ConcurrentHashMap.KeySetView<K extends Object,V extends Object> Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new ExpiringHashMap with default expiration time (5 minutes) and cleanup interval (5 minutes).ExpiringHashMap(long expirationTime, long cleanupInterval, TimeUnit timeUnit) Creates a new ExpiringHashMap with the specified expiration time and cleanup interval.ExpiringHashMap(long expirationTime, TimeUnit timeUnit) Creates a new ExpiringHashMap with the specified expiration time and default cleanup interval (5 minutes). -
Method Summary
Modifier and TypeMethodDescriptioncompute(K key, BiFunction<? super K, ? super ExpiringHashMap.Entry<V>, ? extends ExpiringHashMap.Entry<V>> remappingFunction) computeIfAbsent(K key, Function<? super K, ? extends ExpiringHashMap.Entry<V>> mappingFunction) computeIfPresent(K key, BiFunction<? super K, ? super ExpiringHashMap.Entry<V>, ? extends ExpiringHashMap.Entry<V>> remappingFunction) booleancontainsKey(Object key) Returns true if the map contains a mapping for the specified key and the value has not expired.booleancontainsRawValue(V value) Returns true if the map contains a mapping with the specified raw value and the entry has not expired.booleancontainsValue(Object value) Removes expired entries and then returns the entry set.longReturns the cleanup interval in milliseconds.longReturns the expiration time for entries in milliseconds.Returns the value associated with the key, or null if the key is not in the map or the value has expired.Removes expired entries and then returns the key set.merge(K key, ExpiringHashMap.Entry<V> value, BiFunction<? super ExpiringHashMap.Entry<V>, ? super ExpiringHashMap.Entry<V>, ? extends ExpiringHashMap.Entry<V>> remappingFunction) put(K key, ExpiringHashMap.Entry<V> value) voidputAll(Map<? extends K, ? extends ExpiringHashMap.Entry<V>> m) putIfAbsent(K key, ExpiringHashMap.Entry<V> value) Puts a value in the map with the default expiration time.putValueIfAbsent(K key, V value) Puts a value in the map if the key is not already associated with a value or if the current value has expired.putWithExpiration(K key, V value, long expirationTime, TimeUnit timeUnit) Puts a value in the map with a custom expiration time.booleanremoveValue(Object key) Removes the mapping for the specified key and returns the value that was associated with the key.replace(K key, ExpiringHashMap.Entry<V> value) booleanreplace(K key, ExpiringHashMap.Entry<V> oldValue, ExpiringHashMap.Entry<V> newValue) voidreplaceAll(BiFunction<? super K, ? super ExpiringHashMap.Entry<V>, ? extends ExpiringHashMap.Entry<V>> function) Removes expired entries and then returns the values.Methods inherited from class java.util.concurrent.ConcurrentHashMap
clear, contains, elements, entrySet, equals, forEach, forEach, forEach, forEachEntry, forEachEntry, forEachKey, forEachKey, forEachValue, forEachValue, getOrDefault, hashCode, isEmpty, keys, keySet, keySet, mappingCount, newKeySet, newKeySet, reduce, reduceEntries, reduceEntries, reduceEntriesToDouble, reduceEntriesToInt, reduceEntriesToLong, reduceKeys, reduceKeys, reduceKeysToDouble, reduceKeysToInt, reduceKeysToLong, reduceToDouble, reduceToInt, reduceToLong, reduceValues, reduceValues, reduceValuesToDouble, reduceValuesToInt, reduceValuesToLong, search, searchEntries, searchKeys, searchValues, size, toString, valuesMethods inherited from class java.util.AbstractMap
clone
-
Constructor Details
-
ExpiringHashMap
public ExpiringHashMap()Creates a new ExpiringHashMap with default expiration time (5 minutes) and cleanup interval (5 minutes). -
ExpiringHashMap
Creates a new ExpiringHashMap with the specified expiration time and default cleanup interval (5 minutes).- Parameters:
expirationTime- the expiration time for entriestimeUnit- the time unit of the expiration time
-
ExpiringHashMap
Creates a new ExpiringHashMap with the specified expiration time and cleanup interval.- Parameters:
expirationTime- the expiration time for entriescleanupInterval- the interval between cleanup operationstimeUnit- the time unit of both the expiration time and cleanup interval
-
-
Method Details
-
getExpirationTimeMs
public long getExpirationTimeMs()Returns the expiration time for entries in milliseconds.- Returns:
- the expiration time in milliseconds
-
getCleanupIntervalMs
public long getCleanupIntervalMs()Returns the cleanup interval in milliseconds.- Returns:
- the cleanup interval in milliseconds
-
putValue
Puts a value in the map with the default expiration time.- Parameters:
key- the keyvalue- the value- Returns:
- the previous value associated with the key, or null if there was no mapping
-
putWithExpiration
Puts a value in the map with a custom expiration time.- Parameters:
key- the keyvalue- the valueexpirationTime- the expiration timetimeUnit- the time unit of the expiration time- Returns:
- the previous value associated with the key, or null if there was no mapping
-
getValue
Returns the value associated with the key, or null if the key is not in the map or the value has expired.- Parameters:
key- the key- Returns:
- the value associated with the key, or null if the key is not in the map or the value has expired
-
putValueIfAbsent
Puts a value in the map if the key is not already associated with a value or if the current value has expired.- Parameters:
key- the keyvalue- the value- Returns:
- the previous value associated with the key, or null if there was no mapping or the value had expired
-
removeValue
Removes the mapping for the specified key and returns the value that was associated with the key.- Parameters:
key- the key- Returns:
- the value associated with the key, or null if the key was not in the map
-
containsKey
Returns true if the map contains a mapping for the specified key and the value has not expired.- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classConcurrentHashMap<K,ExpiringHashMap.Entry<V>> - Parameters:
key- the key- Returns:
- true if the map contains a mapping for the specified key and the value has not expired
-
containsRawValue
Returns true if the map contains a mapping with the specified raw value and the entry has not expired.- Parameters:
value- the raw value (not wrapped in an Entry)- Returns:
- true if the map contains a mapping with the specified value and the entry has not expired
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
put
- Specified by:
putin interfaceMap<K,V> - Overrides:
putin classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
putAll
- Specified by:
putAllin interfaceMap<K,V> - Overrides:
putAllin classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
putIfAbsent
- Specified by:
putIfAbsentin interfaceConcurrentMap<K,V> - Specified by:
putIfAbsentin interfaceMap<K,V> - Overrides:
putIfAbsentin classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
get
- Specified by:
getin interfaceMap<K,V> - Overrides:
getin classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
remove
- Specified by:
removein interfaceMap<K,V> - Overrides:
removein classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
remove
- Specified by:
removein interfaceConcurrentMap<K,V> - Specified by:
removein interfaceMap<K,V> - Overrides:
removein classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
replace
- Specified by:
replacein interfaceConcurrentMap<K,V> - Specified by:
replacein interfaceMap<K,V> - Overrides:
replacein classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
replace
- Specified by:
replacein interfaceConcurrentMap<K,V> - Specified by:
replacein interfaceMap<K,V> - Overrides:
replacein classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
replaceAll
public void replaceAll(BiFunction<? super K, ? super ExpiringHashMap.Entry<V>, ? extends ExpiringHashMap.Entry<V>> function) - Specified by:
replaceAllin interfaceConcurrentMap<K,V> - Specified by:
replaceAllin interfaceMap<K,V> - Overrides:
replaceAllin classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
computeIfAbsent
public ExpiringHashMap.Entry<V> computeIfAbsent(K key, Function<? super K, ? extends ExpiringHashMap.Entry<V>> mappingFunction) - Specified by:
computeIfAbsentin interfaceConcurrentMap<K,V> - Specified by:
computeIfAbsentin interfaceMap<K,V> - Overrides:
computeIfAbsentin classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
computeIfPresent
public ExpiringHashMap.Entry<V> computeIfPresent(K key, BiFunction<? super K, ? super ExpiringHashMap.Entry<V>, ? extends ExpiringHashMap.Entry<V>> remappingFunction) - Specified by:
computeIfPresentin interfaceConcurrentMap<K,V> - Specified by:
computeIfPresentin interfaceMap<K,V> - Overrides:
computeIfPresentin classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
compute
public ExpiringHashMap.Entry<V> compute(K key, BiFunction<? super K, ? super ExpiringHashMap.Entry<V>, ? extends ExpiringHashMap.Entry<V>> remappingFunction) - Specified by:
computein interfaceConcurrentMap<K,V> - Specified by:
computein interfaceMap<K,V> - Overrides:
computein classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
merge
public ExpiringHashMap.Entry<V> merge(K key, ExpiringHashMap.Entry<V> value, BiFunction<? super ExpiringHashMap.Entry<V>, ? super ExpiringHashMap.Entry<V>, ? extends ExpiringHashMap.Entry<V>> remappingFunction) - Specified by:
mergein interfaceConcurrentMap<K,V> - Specified by:
mergein interfaceMap<K,V> - Overrides:
mergein classConcurrentHashMap<K,ExpiringHashMap.Entry<V>>
-
keySetWithCleanup
Removes expired entries and then returns the key set.- Returns:
- the key set
-
valuesWithCleanup
Removes expired entries and then returns the values.- Returns:
- the values
-
entrySetWithCleanup
Removes expired entries and then returns the entry set.- Returns:
- the entry set
-