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>
A
Map
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!
-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionExpiringMap
(long maximumSize, long expireAfterWrite, long expireAfterAccess) ExpiringMap
(long maximumSize, long expireAfterWrite, long expireAfterAccess, BiConsumer<K, V> removalListener) -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) entrySet()
boolean
isEmpty()
keySet()
void
int
size()
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 Details
-
ExpiringMap
public ExpiringMap(long maximumSize, long expireAfterWrite, long expireAfterAccess, BiConsumer<K, V> removalListener) - 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.removalListener
- called every time an entry is removed from the map. First passed argument is the entry key, second argument the value.- Throws:
IllegalArgumentException
- ifmaximumSize
is0
, orexpireAfterWrite
andexpireAfterAccess
are both-1
.
-
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
.
-
-
Method Details
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V>
-
get
-
put
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-