Class 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 Detail

      • SimpleTimeoutMap

        public SimpleTimeoutMap()
    • Method Detail

      • put

        public V put​(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:
        put in interface TimeoutMap<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 null if none.
      • 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.
      • pollTimeoutNow

        public Map.Entry<K,​V> pollTimeoutNow()
        Description copied from interface: TimeoutMap
        Nonblocking poll for the next entry which is timed out, based on the time of the invocation of this method.
        Specified by:
        pollTimeoutNow in interface TimeoutMap<K,​V>
        Returns:
        null if there is no timed-out entry in this map. otherwise the oldest timed-out entry.
      • pollTimeoutBlocking

        public Map.Entry<K,​V> pollTimeoutBlocking​(long duration,
                                                        TimeUnit timeUnit)
                                                 throws InterruptedException
        Description copied from interface: TimeoutMap
        Blocking 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:
        pollTimeoutBlocking in interface TimeoutMap<K,​V>
        Returns:
        null if 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: 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.
      • _getTimeToNextTimeoutNs

        public long _getTimeToNextTimeoutNs​(long nowNs)