Class SoftHashMap<K,​V>

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

    public class SoftHashMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    A memory-constrained SoftHashMap that stores its values in SoftReferences.

    N.B. JDK's WeakHashMap, which uses WeakReferences for its keys rather than for its values. See SoftKeyHashMap for a WeakHashMap-similar implementation using SoftReference-ed keys.

    Having the values wrapped by soft references allows the cache to automatically reduce its size based on memory limitations and garbage collection. This ensures that the cache will not cause memory leaks by holding strong references to all of its values.

    This implementation is thread-safe and usable in concurrent environments.

    Author:
    Heinz Kabutz, original author public version, rstein, adapted to use within de.gsi.dataset
    See Also:
    SoftKeyHashMap
    • 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>
    • Constructor Summary

      Constructors 
      Constructor Description
      SoftHashMap()
      Creates a new SoftHashMap with a default retention size size of DEFAULT_RETENTION_SIZE (100 entries).
      SoftHashMap​(int retentionSize)
      Creates a new SoftHashMap with the specified retention size.
      SoftHashMap​(java.util.Map<K,​V> source)
      Creates a SoftHashMap backed by the specified source, with a default retention size of DEFAULT_RETENTION_SIZE (100 entries).
      SoftHashMap​(java.util.Map<K,​V> source, int retentionSize)
      Creates a SoftHashMap backed by the specified source, with the specified retention size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      V get​(java.lang.Object key)  
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      V put​(K key, V value)
      Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection.
      void putAll​(java.util.Map<? extends K,​? extends V> m)  
      V remove​(java.lang.Object key)  
      int size()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

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

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • SoftHashMap

        public SoftHashMap​(int retentionSize)
        Creates a new SoftHashMap with the specified retention size.

        The retention size (n) is the total number of most recent entries in the map that will be strongly referenced (ie 'retained') to prevent them from being eagerly garbage collected. That is, the point of a SoftHashMap is to allow the garbage collector to remove as many entries from this map as it desires, but there will always be (n) elements retained after a GC due to the strong references.

        Note that in a highly concurrent environments the exact total number of strong references may differ slightly than the actual retentionSize value. This number is intended to be a best-effort retention low water mark.

        Parameters:
        retentionSize - the total number of most recent entries in the map that will be strongly referenced (retained), preventing them from being eagerly garbage collected by the JVM.
      • SoftHashMap

        public SoftHashMap​(java.util.Map<K,​V> source)
        Creates a SoftHashMap backed by the specified source, with a default retention size of DEFAULT_RETENTION_SIZE (100 entries).
        Parameters:
        source - the backing map to populate this SoftHashMap
        See Also:
        SoftHashMap(Map,int)
      • SoftHashMap

        public SoftHashMap​(java.util.Map<K,​V> source,
                           int retentionSize)
        Creates a SoftHashMap backed by the specified source, with the specified retention size.

        The retention size (n) is the total number of most recent entries in the map that will be strongly referenced (ie 'retained') to prevent them from being eagerly garbage collected. That is, the point of a SoftHashMap is to allow the garbage collector to remove as many entries from this map as it desires, but there will always be (n) elements retained after a GC due to the strong references.

        Note that in a highly concurrent environments the exact total number of strong references may differ slightly than the actual retentionSize value. This number is intended to be a best-effort retention low water mark.

        Parameters:
        source - the backing map to populate this SoftHashMap
        retentionSize - the total number of most recent entries in the map that will be strongly referenced (retained), preventing them from being eagerly garbage collected by the JVM.
    • Method Detail

      • clear

        public void clear()
        Specified by:
        clear 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>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet 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>
      • isEmpty

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

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

        public V put​(K key,
                     V value)
        Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection.
        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 previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Specified by:
        putAll 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>
      • size

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

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