Class ExpireHashMap<K,​V>

java.lang.Object
com.comphenix.protocol.collections.ExpireHashMap<K,​V>
Type Parameters:
K - - type of the keys.
V - - type of the values.

public class ExpireHashMap<K,​V> extends Object
Represents a hash map where each association may expire after a given time has elapsed.

Note that replaced key-value associations are only collected once the original expiration time has elapsed.

Author:
Kristian Stangeland
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a new hash map where each entry may expire at a given time.
    ExpireHashMap​(com.google.common.base.Ticker ticker)
    Construct a new hash map where each entry may expire at a given time.
  • Method Summary

    Modifier and Type
    Method
    Description
    Map<K,​V>
    Retrieve a view of this expire map as an ordinary map that does not support insertion.
    void
    Clear all the entries in the current map.
    void
    Clear all references to key-value pairs that have been removed or replaced before they were naturally evicted.
    boolean
    containsKey​(K key)
    Determine if the given key is referring to an unexpired association in the map.
    boolean
    containsValue​(V value)
    Determine if the given value is referring to an unexpired association in the map.
    Retrieve a view of all the entries in the set.
    protected void
    Evict any expired entries in the map.
    get​(K key)
    Retrieve the value associated with the given key, if it has not expired.
    Retrieve a view of the keys in the current map.
    put​(K key, V value, long expireDelay, TimeUnit expireUnit)
    Associate the given key with the given value, until the expire delay have elapsed.
    removeKey​(K key)
    Remove a key and its associated value from the map.
    int
    Retrieve the number of entries in the map.
     
    Retrieve a view of all the values in the current map.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ExpireHashMap

      public ExpireHashMap()
      Construct a new hash map where each entry may expire at a given time.
    • ExpireHashMap

      public ExpireHashMap(com.google.common.base.Ticker ticker)
      Construct a new hash map where each entry may expire at a given time.
      Parameters:
      ticker - - supplier of the current time.
  • Method Details

    • get

      public V get(K key)
      Retrieve the value associated with the given key, if it has not expired.
      Parameters:
      key - - the key.
      Returns:
      The value, or NULL if not found or it has expired.
    • put

      public V put(K key, V value, long expireDelay, TimeUnit expireUnit)
      Associate the given key with the given value, until the expire delay have elapsed.
      Parameters:
      key - - the key.
      value - - the value.
      expireDelay - - the amount of time until this association expires. Must be greater than zero.
      expireUnit - - the unit of the expiration.
      Returns:
      Any previously unexpired association with this key, or NULL.
    • containsKey

      public boolean containsKey(K key)
      Determine if the given key is referring to an unexpired association in the map.
      Parameters:
      key - - the key.
      Returns:
      TRUE if it is, FALSE otherwise.
    • containsValue

      public boolean containsValue(V value)
      Determine if the given value is referring to an unexpired association in the map.
      Parameters:
      value - - the value.
      Returns:
      TRUE if it is, FALSE otherwise.
    • removeKey

      public V removeKey(K key)
      Remove a key and its associated value from the map.
      Parameters:
      key - - the key to remove.
      Returns:
      Value of the removed association, NULL otherwise.
    • size

      public int size()
      Retrieve the number of entries in the map.
      Returns:
      The number of entries.
    • keySet

      public Set<K> keySet()
      Retrieve a view of the keys in the current map.
      Returns:
      View of the keys.
    • values

      public Collection<V> values()
      Retrieve a view of all the values in the current map.
      Returns:
      All the values.
    • entrySet

      public Set<Map.Entry<K,​V>> entrySet()
      Retrieve a view of all the entries in the set.
      Returns:
      All the entries.
    • asMap

      public Map<K,​V> asMap()
      Retrieve a view of this expire map as an ordinary map that does not support insertion.
      Returns:
      The map.
    • collect

      public void collect()
      Clear all references to key-value pairs that have been removed or replaced before they were naturally evicted.

      This operation requires a linear scan of the current entries in the map.

    • clear

      public void clear()
      Clear all the entries in the current map.
    • evictExpired

      protected void evictExpired()
      Evict any expired entries in the map.

      This is called automatically by any of the read or write operations.

    • toString

      public String toString()
      Overrides:
      toString in class Object