- java.lang.Object
-
- ch.bind.philib.util.SimpleTimeoutMap<K,V>
-
- Type Parameters:
K- type parameter for the map's keys.V- type parameter for the map's values.
- All Implemented Interfaces:
TimeoutMap<K,V>
public final class SimpleTimeoutMap<K,V> extends Object implements TimeoutMap<K,V>
An implementation ofTimeoutMapwhich uses theTreeMapandHashMapfrom java.util for internal data management. This implementation is threadsafe.- Author:
- Philipp Meinen
- See Also:
TimeoutMap
-
-
Constructor Summary
Constructors Constructor Description SimpleTimeoutMap()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long_getTimeToNextTimeoutNs(long nowNs)voidclear()removes all key-value pairs from this map.booleancontainsKey(K key)Check for the existence of a key.Vget(K key)Searches a value by its keylonggetTimeToNextTimeout()booleanisEmpty()List<Map.Entry<K,V>>pollAllTimeoutNow()Nonblocking poll for all timed out entries, based on the time of the invocation of this method.Map.Entry<K,V>pollTimeoutBlocking()Blocking poll for the next entry which is timed out.Map.Entry<K,V>pollTimeoutBlocking(long duration, TimeUnit timeUnit)Blocking poll for the next entry which is timed out.Map.Entry<K,V>pollTimeoutNow()Nonblocking poll for the next entry which is timed out, based on the time of the invocation of this method.Vput(long timeout, K key, V value)Add a key-value pair with an associated timeout to the map.Vremove(K key)removes an entry by it's keyintsize()query the number of entries in this map.
-
-
-
Method Detail
-
put
public V put(long timeout, K key, V value)
Description copied from interface:TimeoutMapAdd a key-value pair with an associated timeout to the map. The resulting timeout-timestamp is the current time plus the supplied timeout.- Specified by:
putin interfaceTimeoutMap<K,V>- Parameters:
timeout- the timeout for this key-value pair (in milliseconds)key- -value- -- Returns:
- the value which was previously associated with the given key or
nullif none.
-
get
public V get(K key)
Description copied from interface:TimeoutMapSearches a value by its key- Specified by:
getin interfaceTimeoutMap<K,V>- Returns:
nullif the key does not exist.
-
remove
public V remove(K key)
Description copied from interface:TimeoutMapremoves an entry by it's key- Specified by:
removein interfaceTimeoutMap<K,V>- Parameters:
key- the key for which an entry must be removed.- Returns:
nullif there was no entry for this key in that map, otherwise the value of the removed entry.
-
pollTimeoutNow
public Map.Entry<K,V> pollTimeoutNow()
Description copied from interface:TimeoutMapNonblocking poll for the next entry which is timed out, based on the time of the invocation of this method.- Specified by:
pollTimeoutNowin interfaceTimeoutMap<K,V>- Returns:
nullif there is no timed-out entry in this map. otherwise the oldest timed-out entry.
-
pollAllTimeoutNow
public List<Map.Entry<K,V>> pollAllTimeoutNow()
Description copied from interface:TimeoutMapNonblocking poll for all timed out entries, based on the time of the invocation of this method.- Specified by:
pollAllTimeoutNowin interfaceTimeoutMap<K,V>- Returns:
Collections.EMPTY_LISTif there is are no timed out entries in this map.
-
pollTimeoutBlocking
public Map.Entry<K,V> pollTimeoutBlocking() throws InterruptedException
Description copied from interface:TimeoutMapBlocking poll for the next entry which is timed out.- Specified by:
pollTimeoutBlockingin interfaceTimeoutMap<K,V>- Returns:
nullif there is no timed-out entry in this map within the given duration. otherwise the oldest timed-out entry.- Throws:
InterruptedException
-
pollTimeoutBlocking
public Map.Entry<K,V> pollTimeoutBlocking(long duration, TimeUnit timeUnit) throws InterruptedException
Description copied from interface:TimeoutMapBlocking poll for the next entry which is timed out. The parameters duration and timeUnit define for how long the method must wait for a timeout to occur.- Specified by:
pollTimeoutBlockingin interfaceTimeoutMap<K,V>- Returns:
nullif there is no timed-out entry in this map within the given duration. otherwise the oldest timed-out entry.- Throws:
InterruptedException
-
clear
public void clear()
Description copied from interface:TimeoutMapremoves all key-value pairs from this map.- Specified by:
clearin interfaceTimeoutMap<K,V>
-
size
public int size()
Description copied from interface:TimeoutMapquery the number of entries in this map.- Specified by:
sizein interfaceTimeoutMap<K,V>- Returns:
- the number of entries in this map.
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceTimeoutMap<K,V>- Returns:
trueif this map is empty,falseotherwise.
-
containsKey
public boolean containsKey(K key)
Description copied from interface:TimeoutMapCheck for the existence of a key.- Specified by:
containsKeyin interfaceTimeoutMap<K,V>- Parameters:
key- -- Returns:
trueif there is an entry identified by that key in this map,false otherwise.
-
getTimeToNextTimeout
public long getTimeToNextTimeout()
- Specified by:
getTimeToNextTimeoutin interfaceTimeoutMap<K,V>- Returns:
Long.MAX_VALUEif the TimeoutMap is empty. Otherwise the time until the next entry times out (in milliseconds since the epoch). A return value of zero indicates that there is at least one entry which has already timed out.
-
_getTimeToNextTimeoutNs
public long _getTimeToNextTimeoutNs(long nowNs)
-
-