Class CapacityLimitedHashtable<K,​V>

  • Type Parameters:
    K - the map key type
    V - the map value type
    All Implemented Interfaces:
    java.io.Serializable, java.util.concurrent.ConcurrentMap<K,​V>, java.util.Map<K,​V>
    Direct Known Subclasses:
    LRUByteLimitedHashtable, LRUHashtable

    public abstract class CapacityLimitedHashtable<K,​V>
    extends java.util.concurrent.ConcurrentHashMap<K,​V>

    Map that will eject its least-recently-accessed values to remain within a specified capacity.

    The type of capacity is determined by implementations of this abstract class's getObjectSize method - for example, an implementation that returns 1 would count capacity as the number of items in the map, while a different implementation might return the object's byte size and make capacity a maximum amount of memory.

    Useful for caching.

    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.concurrent.ConcurrentHashMap

        java.util.concurrent.ConcurrentHashMap.KeySetView<K extends java.lang.Object,​V extends java.lang.Object>
      • 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>
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      int currentSize()
      Return the current size of all of the table's values as judged by the implementation of getObjectSize.
      V get​(java.lang.Object key)
      Retrieve a value from the map.
      abstract long getObjectSize​(V obj)
      Return the size this object will be inside of this hashtable.
      V put​(K key, V value)
      Maps the specified key to the specified value in this table.
      V remove​(java.lang.Object key)
      Removes the key (and its corresponding value) from this map.
      • Methods inherited from class java.util.concurrent.ConcurrentHashMap

        clear, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, forEach, forEach, forEachEntry, forEachEntry, forEachKey, forEachKey, forEachValue, forEachValue, getOrDefault, hashCode, isEmpty, keys, keySet, keySet, mappingCount, merge, newKeySet, newKeySet, putAll, putIfAbsent, reduce, reduceEntries, reduceEntries, reduceEntriesToDouble, reduceEntriesToInt, reduceEntriesToLong, reduceKeys, reduceKeys, reduceKeysToDouble, reduceKeysToInt, reduceKeysToLong, reduceToDouble, reduceToInt, reduceToLong, reduceValues, reduceValues, reduceValuesToDouble, reduceValuesToInt, reduceValuesToLong, remove, replace, replace, replaceAll, search, searchEntries, searchKeys, searchValues, size, toString, values
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • CapacityLimitedHashtable

        public CapacityLimitedHashtable​(long capacity)
        Parameters:
        capacity - The maximum capacity the map will allow before ejecting values. What this value means depends on the implementation of getObjectSize.
    • Method Detail

      • currentSize

        public int currentSize()
        Return the current size of all of the table's values as judged by the implementation of getObjectSize.
        Returns:
        size of the values in the table
      • getObjectSize

        public abstract long getObjectSize​(V obj)

        Return the size this object will be inside of this hashtable. The implementation of this method will determine how the hash's capacity will work; for example, returning 1 for every object will make the capacity a simple length limit, while returning the object's byte size will make capacity a memory size limitation.

        Parameters:
        obj - An object to size
        Returns:
        the size of the object (unit will vary by implementation)
      • remove

        public V remove​(java.lang.Object key)
        Removes the key (and its corresponding value) from this map. This method does nothing if the key is not in the map.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.concurrent.ConcurrentHashMap<K,​V>
        Parameters:
        key - the key that needs to be removed
        Returns:
        the previous value associated with key, or null if there was no mapping for key
        Throws:
        java.lang.NullPointerException - if the specified key is null
      • put

        public V put​(K key,
                     V value)

        Maps the specified key to the specified value in this table. Neither the key nor the value can be null.

        If adding this value puts the table above its maximum capacity, objects that were accessed least recently will be removed from the table until it is back at capacity.

        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.concurrent.ConcurrentHashMap<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 previous value associated with key, or null if there was no mapping for key
        Throws:
        java.lang.NullPointerException - if the specified key or value is null
        See Also:
        ConcurrentHashMap.put(java.lang.Object, java.lang.Object)
      • get

        public V get​(java.lang.Object key)
        Retrieve a value from the map.
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.concurrent.ConcurrentHashMap<K,​V>
        Parameters:
        key - the key the value was stored with
        Returns:
        the value, or null if there is no value for that key
        See Also:
        ConcurrentHashMap.get(java.lang.Object)