Class TimedHashMap<K,V>

java.lang.Object
net.sourceforge.jnlp.util.TimedHashMap<K,V>
Type Parameters:
K - The key type
V - The Object type
All Implemented Interfaces:
Map<K,V>

public class TimedHashMap<K,V> extends Object implements Map<K,V>
Simple utility class that extends HashMap by adding an expiry to the entries. This map stores entries, and returns them only if the entries were last accessed within a specified timeout period. Otherwise, null is returned. This map does not allow null keys but does allow null values.
  • Constructor Details

    • TimedHashMap

      public TimedHashMap()
    • TimedHashMap

      public TimedHashMap(long timeout, TimeUnit unit)
      Create a new map with a non-default entry timeout period
      Parameters:
      unit - the units of the timeout
      timeout - the length of the timeout
  • Method Details

    • setTimeout

      public void setTimeout(long timeout, TimeUnit unit)
      Specify how long (in nanoseconds) entries are valid for
      Parameters:
      unit - the units of the timeout
      timeout - the length of the timeout
    • put

      public V put(K key, V value)
      Store the item in the map and associate a timestamp with it. null is not accepted as a key.
      Specified by:
      put in interface Map<K,V>
      Parameters:
      key - The key
      value - The value to store
    • get

      public V get(Object key)
      Return cached item if it has not already expired. Before returning, this method also resets the "last accessed" time for this entry, so it is good for another 10 seconds
      Specified by:
      get in interface Map<K,V>
      Parameters:
      key - The key
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>