Class LRUHashMap<K,​V>

  • All Implemented Interfaces:
    ExpiringMap<K,​V>, java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​V>

    public class LRUHashMap<K,​V>
    extends java.util.LinkedHashMap<K,​V>
    implements ExpiringMap<K,​V>
    Map implementation with size limit, that keeps its entries in LRU (least recently used) order, also known as access-order. When the size limit is reached, the least recently accessed mappings are removed. The number of mappings to be removed from the map, is controlled by the trim factor.

    Version:
    $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/LRUHashMap.java#1 $
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • 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
      private int maxSize  
      private float trimFactor  
    • Constructor Summary

      Constructors 
      Constructor Description
      LRUHashMap()
      Creates an LRUHashMap with default max size (1000 entries).
      LRUHashMap​(int pMaxSize)
      Creates an LRUHashMap with the given max size.
      LRUHashMap​(java.util.Map<? extends K,​? extends V> pContents)
      Creates an LRUHashMap with initial mappings from the given map, and default max size (1000 entries).
      LRUHashMap​(java.util.Map<? extends K,​? extends V> pContents, int pMaxSize)
      Creates an LRUHashMap with initial mappings from the given map, and the given max size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getMaxSize()
      Returns the maximum number of mappings in this map.
      float getTrimFactor()
      Returns the current trim factor.
      void processRemoved​(java.util.Map.Entry<K,​V> pRemoved)
      Default implementation does nothing.
      protected boolean removeEldestEntry​(java.util.Map.Entry<K,​V> pEldest)
      always returns false, and instead invokes removeLRU() if size >= maxSize.
      void removeLRU()
      Removes the least recently used mapping(s) from this map.
      void setMaxSize​(int pMaxSize)
      Sets the maximum number of elements in this map.
      void setTrimFactor​(float pTrimFactor)
      Sets the trim factor.
      • Methods inherited from class java.util.LinkedHashMap

        clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, values
      • Methods inherited from class java.util.HashMap

        clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
      • Methods inherited from class java.util.AbstractMap

        equals, hashCode, toString
      • Methods inherited from class java.lang.Object

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

        clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
    • Field Detail

      • maxSize

        private int maxSize
      • trimFactor

        private float trimFactor
    • Constructor Detail

      • LRUHashMap

        public LRUHashMap()
        Creates an LRUHashMap with default max size (1000 entries). This is constructor is here to comply with the reccomendations for "standard" constructors in the Map interface.
        See Also:
        LRUHashMap(int)
      • LRUHashMap

        public LRUHashMap​(int pMaxSize)
        Creates an LRUHashMap with the given max size.
        Parameters:
        pMaxSize - size limit
      • LRUHashMap

        public LRUHashMap​(java.util.Map<? extends K,​? extends V> pContents)
        Creates an LRUHashMap with initial mappings from the given map, and default max size (1000 entries). This is constructor is here to comply with the reccomendations for "standard" constructors in the Map interface.
        Parameters:
        pContents - the map whose mappings are to be placed in this map. May be null.
        See Also:
        LRUHashMap(java.util.Map, int)
      • LRUHashMap

        public LRUHashMap​(java.util.Map<? extends K,​? extends V> pContents,
                          int pMaxSize)
        Creates an LRUHashMap with initial mappings from the given map, and the given max size.
        Parameters:
        pContents - the map whose mappings are to be placed in this map. May be null.
        pMaxSize - size limit
    • Method Detail

      • getMaxSize

        public int getMaxSize()
        Returns the maximum number of mappings in this map.
        Returns:
        the size limit
      • setMaxSize

        public void setMaxSize​(int pMaxSize)
        Sets the maximum number of elements in this map. If the current size is greater than the new max size, the map will be trimmed to fit the new max size constraint.
        Parameters:
        pMaxSize - new size limit
        See Also:
        removeLRU()
      • getTrimFactor

        public float getTrimFactor()
        Returns the current trim factor.

        The trim factor controls how many percent of the maps current size is reclaimed, when performing an removeLRU operation. Defaults to 1% (0.01f).

        Returns:
        the current trim factor
      • setTrimFactor

        public void setTrimFactor​(float pTrimFactor)
        Sets the trim factor.

        The trim factor controls how many percent of the maps current size is reclaimed, when performing an removeLRU operation. Defaults to 1% (0.01f).

        Parameters:
        pTrimFactor - the new trim factor. Acceptable values are between 0 (inclusive) and 1 (exclusive).
        See Also:
        removeLRU()
      • removeEldestEntry

        protected boolean removeEldestEntry​(java.util.Map.Entry<K,​V> pEldest)
        always returns false, and instead invokes removeLRU() if size >= maxSize.
        Overrides:
        removeEldestEntry in class java.util.LinkedHashMap<K,​V>
      • processRemoved

        public void processRemoved​(java.util.Map.Entry<K,​V> pRemoved)
        Default implementation does nothing. May be used by clients as a call-back to notify when mappings expire from the map.
        Specified by:
        processRemoved in interface ExpiringMap<K,​V>
        Parameters:
        pRemoved - the removed mapping
      • removeLRU

        public void removeLRU()
        Removes the least recently used mapping(s) from this map.

        How many mappings are removed from the map, is controlled by the trim factor. In any case, at least one mapping will be removed.

        See Also:
        getTrimFactor()