Class ExpiringMap<K,​V>

  • Type Parameters:
    K - The key type
    V - The value type
    All Implemented Interfaces:
    java.util.Map<K,​V>

    public class ExpiringMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    A map with expiration. This class contains a worker thread that will periodically check this class in order to determine if any objects should be removed based on the provided time-to-live value.
    Author:
    Apache MINA Project
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  ExpiringMap.Expirer
      A Thread that monitors an ExpiringMap and will remove elements that have passed the threshold.
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      ExpiringMap()
      Creates a new instance of ExpiringMap using the default values DEFAULT_TIME_TO_LIVE and DEFAULT_EXPIRATION_INTERVAL
      ExpiringMap​(int timeToLive)
      Creates a new instance of ExpiringMap using the supplied time-to-live value and the default value for DEFAULT_EXPIRATION_INTERVAL
      ExpiringMap​(int timeToLive, int expirationInterval)
      Creates a new instance of ExpiringMap using the supplied values and a ConcurrentHashMap for the internal data structure.
    • Field Detail

      • DEFAULT_TIME_TO_LIVE

        public static final int DEFAULT_TIME_TO_LIVE
        The default value, 60 seconds
        See Also:
        Constant Field Values
      • DEFAULT_EXPIRATION_INTERVAL

        public static final int DEFAULT_EXPIRATION_INTERVAL
        The default value, 1 second
        See Also:
        Constant Field Values
    • Constructor Detail

      • ExpiringMap

        public ExpiringMap()
        Creates a new instance of ExpiringMap using the default values DEFAULT_TIME_TO_LIVE and DEFAULT_EXPIRATION_INTERVAL
      • ExpiringMap

        public ExpiringMap​(int timeToLive)
        Creates a new instance of ExpiringMap using the supplied time-to-live value and the default value for DEFAULT_EXPIRATION_INTERVAL
        Parameters:
        timeToLive - The time-to-live value (seconds)
      • ExpiringMap

        public ExpiringMap​(int timeToLive,
                           int expirationInterval)
        Creates a new instance of ExpiringMap using the supplied values and a ConcurrentHashMap for the internal data structure.
        Parameters:
        timeToLive - The time-to-live value (seconds)
        expirationInterval - The time between checks to see if a value should be removed (seconds)
    • Method Detail

      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • size

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

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> inMap)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • values

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

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
      • addExpirationListener

        public void addExpirationListener​(ExpirationListener<V> listener)
        Adds a listener in the expiration listeners
        Parameters:
        listener - The listener to add
      • removeExpirationListener

        public void removeExpirationListener​(ExpirationListener<V> listener)
        Removes a listener from the expiration listeners
        Parameters:
        listener - The listener to remove
      • getExpirationInterval

        public int getExpirationInterval()
        Get the interval in which an object will live in the map before it is removed.
        Returns:
        The expiration time in second
      • getTimeToLive

        public int getTimeToLive()
        Returns:
        the Time-to-live value in seconds.
      • setExpirationInterval

        public void setExpirationInterval​(int expirationInterval)
        Set the interval in which an object will live in the map before it is removed.
        Parameters:
        expirationInterval - The expiration time in seconds
      • setTimeToLive

        public void setTimeToLive​(int timeToLive)
        Update the value for the time-to-live
        Parameters:
        timeToLive - The time-to-live (seconds)