Class ExpiringMap<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this map
    V - the type of mapped values
    All Implemented Interfaces:
    Map<K,​V>

    public class ExpiringMap<K,​V>
    extends Object
    implements Map<K,​V>
    A Map that expires entries based on oldest age (when maximum size has been exceeded), write time, or last access time.

    The expiration policy is only enforced on map access. There will be no automatic expiration handling running in a background thread or similar. For performance reasons the policy is not enforced on every single access, but only once every "expiration window" (Math.max(expireAfterWrite, expireAfterAccess)). Therefore, it may happen that entries are kept in the map up to the double expiration window length.

    This data structure is not thread-safe!

    • Constructor Detail

      • ExpiringMap

        public ExpiringMap​(long maximumSize,
                           long expireAfterWrite,
                           long expireAfterAccess)
        Parameters:
        maximumSize - maximum number of entries that the map should contain. On overflow, first elements based on expiration policy are removed. -1 deactivates a size limitation.
        expireAfterWrite - time in milliseconds after which elements are automatically removed from the map after being added. -1 deactivates this expiration policy.
        expireAfterAccess - time in milliseconds after which elements are automatically removed from the map after last access. -1 deactivates this expiration policy. Keep in mind, that only get(Object) is treated as an element access.
        Throws:
        IllegalArgumentException - if maximumSize is 0, or expireAfterWrite and expireAfterAccess are both -1.
    • Method Detail

      • size

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

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

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

        public V get​(Object key)
        Specified by:
        get in interface Map<K,​V>
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put 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>