ch.bind.philib.util
Class SimpleTimeoutMap<K,V>

java.lang.Object
  extended by 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 of TimeoutMap which uses the TreeMap and HashMap from java.util for internal data management. This implementation is threadsafe.

Author:
Philipp Meinen
See Also:
TimeoutMap

Constructor Summary
SimpleTimeoutMap()
           
 
Method Summary
 void add(long timeout, K key, V value)
          Add a key-value pair with an associated timeout to the map.
 void addWithTimestamp(long timestamp, K key, V value)
          Add a key-value pair with a specific timeout timestamp.
 void clear()
          removes all key-value pairs from this map.
 boolean containsKey(K key)
          Check for the existence of a key.
 V get(K key)
          Searches a value by its key
 long getTimeToNextTimeout()
           
 boolean isEmpty()
           
 Map.Entry<K,V> pollTimeout()
          finds the next entry which is timed out, based on the time of the invocation of this method.
 V remove(K key)
          removes an entry by it's key
 int size()
          query the number of entries in this map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleTimeoutMap

public SimpleTimeoutMap()
Method Detail

add

public void add(long timeout,
                K key,
                V value)
Description copied from interface: TimeoutMap
Add 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:
add in interface TimeoutMap<K,V>
key - -
value - -

addWithTimestamp

public void addWithTimestamp(long timestamp,
                             K key,
                             V value)
Description copied from interface: TimeoutMap
Add a key-value pair with a specific timeout timestamp.

Specified by:
addWithTimestamp in interface TimeoutMap<K,V>
Parameters:
timestamp - the timestamp when this key-value pair will expire (in milliseconds since the epoch).
key - -
value - -

get

public V get(K key)
Description copied from interface: TimeoutMap
Searches a value by its key

Specified by:
get in interface TimeoutMap<K,V>
Returns:
null if the key does not exist.

remove

public V remove(K key)
Description copied from interface: TimeoutMap
removes an entry by it's key

Specified by:
remove in interface TimeoutMap<K,V>
Parameters:
key - the key for which an entry must be removed.
Returns:
null if there was no entry for this key in that map, otherwise the value of the removed entry.

pollTimeout

public Map.Entry<K,V> pollTimeout()
Description copied from interface: TimeoutMap
finds the next entry which is timed out, based on the time of the invocation of this method.

Specified by:
pollTimeout in interface TimeoutMap<K,V>
Returns:
null if there is no timed-out entry in this map. otherwise the oldest timed-out entry.

clear

public void clear()
Description copied from interface: TimeoutMap
removes all key-value pairs from this map.

Specified by:
clear in interface TimeoutMap<K,V>

size

public int size()
Description copied from interface: TimeoutMap
query the number of entries in this map.

Specified by:
size in interface TimeoutMap<K,V>
Returns:
the number of entries in this map.

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface TimeoutMap<K,V>
Returns:
true if this map is empty, false otherwise.

containsKey

public boolean containsKey(K key)
Description copied from interface: TimeoutMap
Check for the existence of a key.

Specified by:
containsKey in interface TimeoutMap<K,V>
Parameters:
key - -
Returns:
true if there is an entry identified by that key in this map, false otherwise.

getTimeToNextTimeout

public long getTimeToNextTimeout()
Specified by:
getTimeToNextTimeout in interface TimeoutMap<K,V>
Returns:
Long.MAX_VALUE if 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.


Copyright © 2013. All Rights Reserved.