Package org.drasyl.util
Class ExpiringMap<K,V>
- java.lang.Object
-
- org.drasyl.util.ExpiringMap<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this mapV
- the type of mapped values
- All Implemented Interfaces:
Map<K,V>
public class ExpiringMap<K,V> extends Object implements Map<K,V>
AMap
that expires entries based on oldest age (when maximum size has been exceeded), write time, or last access time.The expiration policy is only enforced on map access. There will be no automatic expiration handling running in a background thread or similar. For performance reasons the policy is not enforced on every single access, but only once every "expiration window" (
Math
.max(expireAfterWrite, expireAfterAccess)). Therefore, it may happen that entries are kept in the map up to the double expiration window length.This data structure is not thread-safe!
-
-
Constructor Summary
Constructors Constructor Description ExpiringMap(long maximumSize, long expireAfterWrite, long expireAfterAccess)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
boolean
containsKey(Object key)
boolean
containsValue(Object value)
Set<Map.Entry<K,V>>
entrySet()
V
get(Object key)
boolean
isEmpty()
Set<K>
keySet()
V
put(K key, V value)
void
putAll(Map<? extends K,? extends V> m)
V
remove(Object key)
int
size()
Collection<V>
values()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Constructor Detail
-
ExpiringMap
public ExpiringMap(long maximumSize, long expireAfterWrite, long expireAfterAccess)
- Parameters:
maximumSize
- maximum number of entries that the map should contain. On overflow, first elements based on expiration policy are removed.-1
deactivates a size limitation.expireAfterWrite
- time in milliseconds after which elements are automatically removed from the map after being added.-1
deactivates this expiration policy.expireAfterAccess
- time in milliseconds after which elements are automatically removed from the map after last access.-1
deactivates this expiration policy. Keep in mind, that onlyget(Object)
is treated as an element access.- Throws:
IllegalArgumentException
- ifmaximumSize
is0
, orexpireAfterWrite
andexpireAfterAccess
are both-1
.
-
-