Class ExpiringMap<K,V>

java.lang.Object
overit.geocall.util.ExpiringMap<K,V>

public class ExpiringMap<K,V> extends Object
Implementation of a ConcurrentHashMap with an expiration logic. Expired items are deleted periodically through a ScheduledExecutorService.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Force removal of the expired elements from the ExpiringMap.
    void
    Removes all elements from the ExpiringMap.
    computeIfAbsent(K key, long expiration, Function<K,V> mappingFunction)
    If the specified key is not already associated with a value associates it with the given value and returns null, else returns the current value.
    get(K key)
    Get an element from the ExpiringMap.
    put(K key, V value, long expiration)
    Associates the specified value with the specified key in this map.
    remove(K key)
    Removes the mapping for a key from this map if it is present and returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.
    int
    Returns the number of key-value mappings in this map

    Methods inherited from class java.lang.Object

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

    • ExpiringMap

      public ExpiringMap()
  • Method Details

    • cleanExpired

      public void cleanExpired()
      Force removal of the expired elements from the ExpiringMap.
    • put

      public V put(K key, V value, long expiration)
      Associates the specified value with the specified key in this map. This entry will be kept until the expiration is passed. If the map previously contained a mapping for the key, the old value is replaced by the specified value.
      Parameters:
      key - key with which the specified value is to be associated value
      value - value to be associated with the specified key
      expiration - retention time expressed in millis
      Returns:
      the previous value associated with key, or null if there was no mapping for key
    • computeIfAbsent

      public V computeIfAbsent(K key, long expiration, Function<K,V> mappingFunction)
      If the specified key is not already associated with a value associates it with the given value and returns null, else returns the current value. This entry will be kept until the expiration is passed.
      Parameters:
      key - key with which the specified value is to be associated value
      expiration - retention time expressed in millis
      mappingFunction - the function to compute a value
      Returns:
      the value associated with the specified key, or null if there was no mapping for the key
    • remove

      public V remove(K key)
      Removes the mapping for a key from this map if it is present and returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.
      Parameters:
      key - key whose mapping is to be removed from the map
      Returns:
      the previous value associated with key, or null if there was no mapping for key.
    • get

      public V get(K key)
      Get an element from the ExpiringMap. If the element is not present, is null or is expired null is returned.
      Parameters:
      key - the key for the element
      Returns:
      the valid value found, null otherwise
    • clear

      public void clear()
      Removes all elements from the ExpiringMap.
    • size

      public int size()
      Returns the number of key-value mappings in this map
      Returns:
      the number of key-value mappings in this map