Package overit.geocall.util
Class ExpiringMap<K,V>
java.lang.Object
overit.geocall.util.ExpiringMap<K,V>
Implementation of a
ConcurrentHashMap with an expiration logic.
Expired items are deleted periodically through a ScheduledExecutorService.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidForce removal of the expired elements from the ExpiringMap.voidclear()Removes all elements from the ExpiringMap.computeIfAbsent(K key, long expiration, Function<K, V> mappingFunction) If the specified key is not already associated with a value associates it with the given value and returns null, else returns the current value.Get an element from the ExpiringMap.Associates the specified value with the specified key in this map.Removes the mapping for a key from this map if it is present and returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.intsize()Returns the number of key-value mappings in this map
-
Constructor Details
-
ExpiringMap
public ExpiringMap()
-
-
Method Details
-
cleanExpired
public void cleanExpired()Force removal of the expired elements from the ExpiringMap. -
put
Associates the specified value with the specified key in this map. This entry will be kept until the expiration is passed. If the map previously contained a mapping for the key, the old value is replaced by the specified value.- Parameters:
key- key with which the specified value is to be associated valuevalue- value to be associated with the specified keyexpiration- retention time expressed in millis- Returns:
- the previous value associated with key, or null if there was no mapping for key
-
computeIfAbsent
If the specified key is not already associated with a value associates it with the given value and returns null, else returns the current value. This entry will be kept until the expiration is passed.- Parameters:
key- key with which the specified value is to be associated valueexpiration- retention time expressed in millismappingFunction- the function to compute a value- Returns:
- the value associated with the specified key, or null if there was no mapping for the key
-
remove
Removes the mapping for a key from this map if it is present and returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.- Parameters:
key- key whose mapping is to be removed from the map- Returns:
- the previous value associated with key, or null if there was no mapping for key.
-
get
Get an element from the ExpiringMap. If the element is not present, is null or is expired null is returned.- Parameters:
key- the key for the element- Returns:
- the valid value found, null otherwise
-
clear
public void clear()Removes all elements from the ExpiringMap. -
size
public int size()Returns the number of key-value mappings in this map- Returns:
- the number of key-value mappings in this map
-