Class LeaseTimeMap<K,​V>

  • All Implemented Interfaces:
    java.util.Map<K,​V>

    public class LeaseTimeMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    Lease time map with supervised write/update access. For every entry a (potentially different) leasetime is used. Note: When not set to passive, the map internally uses a timer to prune expired entries. It should thus be used as synchronized map (default for non-passive) besides it is used with a specific timer that runs on same thread as normal access. The passive variant should only be called from same thread or externally synchronized or explicitly setting sync to true.
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

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

      Fields 
      Modifier and Type Field Description
      protected java.util.Map<K,​V> map
      The map.
      protected ILeaseTimeSet<K> times
      Lease time map with keys.
      protected boolean touchonread
      Flag if touch on read.
      protected boolean touchonwrite
      Flag if touch on read.
    • Constructor Summary

      Constructors 
      Constructor Description
      LeaseTimeMap​(long leasetime)
      Create a new lease time map.
      LeaseTimeMap​(long leasetime, ICommand<Tuple2<java.util.Map.Entry<K,​V>,​java.lang.Long>> removecmd, boolean touchonread, boolean touchonwrite, boolean passive)
      Create a new lease time map.
      LeaseTimeMap​(long leasetime, ICommand<Tuple2<java.util.Map.Entry<K,​V>,​java.lang.Long>> removecmd, boolean touchonread, boolean touchonwrite, boolean passive, IDelayRunner timer, boolean sync)
      Create a new lease time map.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void checkStale()
      Check for stale entries and remove them.
      void clear()
      Removes all mappings from this map.
      boolean containsKey​(java.lang.Object key)
      Returns
      boolean containsValue​(java.lang.Object value)
      Returns
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Returns a set view of the mappings contained in this map.
      boolean equals​(java.lang.Object o)
      Compares the specified object with this map for equality.
      V get​(java.lang.Object key)
      Returns the collection to which this map maps the specified key.
      int hashCode()
      Returns the hash code value for this map.
      boolean isEmpty()
      Returns
      java.util.Set<K> keySet()
      Returns a set view of the keys contained in this map.
      static void main​(java.lang.String[] args)
      Main for testing.
      V put​(K key, V value)
      Associates the specified value with the specified key in this map (optional operation).
      V put​(K key, V value, long leasetime)  
      void putAll​(java.util.Map<? extends K,​? extends V> t)
      Copies all of the mappings from the specified map to this map (optional operation).
      void putAll​(java.util.Map<? extends K,​? extends V> t, long leasetime)  
      V remove​(java.lang.Object key)
      Removes the mapping for this key from this map if it is present.
      int size()
      Returns the number of elements added to this map.
      java.lang.String toString()
      Create a string representation of this map.
      void touch​(K key)
      Update the timestamp of e.
      java.util.Collection<V> values()
      Unsupported Operation.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • map

        protected java.util.Map<K,​V> map
        The map.
      • touchonread

        protected boolean touchonread
        Flag if touch on read.
      • touchonwrite

        protected boolean touchonwrite
        Flag if touch on read.
    • Constructor Detail

      • LeaseTimeMap

        public LeaseTimeMap​(long leasetime)
        Create a new lease time map.
      • LeaseTimeMap

        public LeaseTimeMap​(long leasetime,
                            ICommand<Tuple2<java.util.Map.Entry<K,​V>,​java.lang.Long>> removecmd,
                            boolean touchonread,
                            boolean touchonwrite,
                            boolean passive)
        Create a new lease time map.
      • LeaseTimeMap

        public LeaseTimeMap​(long leasetime,
                            ICommand<Tuple2<java.util.Map.Entry<K,​V>,​java.lang.Long>> removecmd,
                            boolean touchonread,
                            boolean touchonwrite,
                            boolean passive,
                            IDelayRunner timer,
                            boolean sync)
        Create a new lease time map.
    • Method Detail

      • size

        public int size()
        Returns the number of elements added to this map.
        Specified by:
        size in interface java.util.Map<K,​V>
      • isEmpty

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

        public boolean containsKey​(java.lang.Object key)
        Returns
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Parameters:
        key - key whose presence in this map is to be tested.
        Returns:
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - if the key is
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Returns
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Parameters:
        value - value whose presence in this map is to be tested.
        Returns:
        Throws:
        java.lang.ClassCastException - if the value is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - if the value is
      • get

        public V get​(java.lang.Object key)
        Returns the collection to which this map maps the specified key. Returns
        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key - key whose associated collection is to be returned.
        Returns:
        the collection to which this map maps the specified key, or
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - key is
        See Also:
        containsKey(Object)
      • put

        public V put​(K key,
                     V value)
        Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced by the specified value. (A map m.containsKey(k) would return
        Specified by:
        put in interface java.util.Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated.
        value - value to be associated with the specified key.
        Returns:
        The collection associated to the key.
        Throws:
        java.lang.UnsupportedOperationException - if the
        java.lang.ClassCastException - if the class of the specified key or value prevents it from being stored in this map.
        java.lang.IllegalArgumentException - if some aspect of this key or value prevents it from being stored in this map.
        java.lang.NullPointerException - this map does not permit
      • put

        public V put​(K key,
                     V value,
                     long leasetime)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> t)
        Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Parameters:
        t - Mappings to be stored in this map.
        Throws:
        java.lang.UnsupportedOperationException - if the
        java.lang.ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map.
        java.lang.IllegalArgumentException - some aspect of a key or value in the specified map prevents it from being stored in this map.
        java.lang.NullPointerException - the specified map is
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> t,
                           long leasetime)
      • clear

        public void clear()
        Removes all mappings from this map.
        Specified by:
        clear in interface java.util.Map<K,​V>
      • keySet

        public java.util.Set<K> keySet()
        Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Returns:
        a set view of the keys contained in this map.
      • values

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

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Returns a set view of the mappings contained in this map. Each element in the returned set is a map entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        a set view of the mappings contained in this map.
      • equals

        public boolean equals​(java.lang.Object o)
        Compares the specified object with this map for equality. Returns
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - object to be compared for equality with this map.
        Returns:
      • hashCode

        public int hashCode()
        Returns the hash code value for this map.
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code value for this map.
        See Also:
        Object.hashCode(), Object.equals(Object), equals(Object)
      • toString

        public java.lang.String toString()
        Create a string representation of this map.
        Overrides:
        toString in class java.lang.Object
      • remove

        public V remove​(java.lang.Object key)
        Removes the mapping for this key from this map if it is present. More formally, if this map contains a mapping from key (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the map.
        Returns:
        collection associated with specified key, or
        Throws:
        java.lang.ClassCastException - if the key is of an inappropriate type for this map (optional).
        java.lang.NullPointerException - if the key is
      • touch

        public void touch​(K key)
        Update the timestamp of e.
        Parameters:
        entry - The entry.
      • checkStale

        public void checkStale()
        Check for stale entries and remove them. This operation is only permissible in passive mode.
      • main

        public static void main​(java.lang.String[] args)
        Main for testing.