Class SoftValueHashMap

  • All Implemented Interfaces:
    java.util.Map

    public class SoftValueHashMap
    extends java.util.AbstractMap
    implements java.util.Map
    This Map will remove entries when the value in the map has been cleaned from garbage collection
    Version:
    $Revision: 1.4 $
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  SoftValueHashMap.SoftValueRef  
      • 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 java.util.Map hash  
      private java.lang.ref.ReferenceQueue queue  
    • Constructor Summary

      Constructors 
      Constructor Description
      SoftValueHashMap()
      Constructs a new, empty WeakHashMap with the default initial capacity and the default load factor, which is 0.75.
      SoftValueHashMap​(int initialCapacity)
      Constructs a new, empty WeakHashMap with the given initial capacity and the default load factor, which is 0.75.
      SoftValueHashMap​(int initialCapacity, float loadFactor)
      Constructs a new, empty WeakHashMap with the given initial capacity and the given load factor.
      SoftValueHashMap​(java.util.Map t)
      Constructs a new WeakHashMap with the same mappings as the specified Map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all mappings from this map.
      boolean containsKey​(java.lang.Object key)
      Returns true if this map contains a mapping for the specified key.
      java.util.Set entrySet()
      Returns a set of the mappings contained in this hash table.
      java.lang.Object get​(java.lang.Object key)
      Returns the value to which this map maps the specified key.
      boolean isEmpty()
      Returns true if this map contains no key-value mappings.
      private void processQueue()  
      java.lang.Object put​(java.lang.Object key, java.lang.Object value)
      Updates this map so that the given key maps to the given value.
      java.lang.Object remove​(java.lang.Object key)
      Removes the mapping for the given key from this map, if present.
      int size()
      Returns the number of key-value mappings in this map.
      • Methods inherited from class java.util.AbstractMap

        clone, containsValue, equals, hashCode, keySet, putAll, toString, values
      • Methods inherited from class java.lang.Object

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

        compute, computeIfAbsent, computeIfPresent, containsValue, equals, forEach, getOrDefault, hashCode, keySet, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll, values
    • Field Detail

      • hash

        private java.util.Map hash
      • queue

        private java.lang.ref.ReferenceQueue queue
    • Constructor Detail

      • SoftValueHashMap

        public SoftValueHashMap​(int initialCapacity,
                                float loadFactor)
        Constructs a new, empty WeakHashMap with the given initial capacity and the given load factor.
        Parameters:
        initialCapacity - The initial capacity of the WeakHashMap
        loadFactor - The load factor of the WeakHashMap
        Throws:
        java.lang.IllegalArgumentException - If the initial capacity is less than zero, or if the load factor is nonpositive
      • SoftValueHashMap

        public SoftValueHashMap​(int initialCapacity)
        Constructs a new, empty WeakHashMap with the given initial capacity and the default load factor, which is 0.75.
        Parameters:
        initialCapacity - The initial capacity of the WeakHashMap
        Throws:
        java.lang.IllegalArgumentException - If the initial capacity is less than zero
      • SoftValueHashMap

        public SoftValueHashMap()
        Constructs a new, empty WeakHashMap with the default initial capacity and the default load factor, which is 0.75.
      • SoftValueHashMap

        public SoftValueHashMap​(java.util.Map t)
        Constructs a new WeakHashMap with the same mappings as the specified Map. The WeakHashMap is created with an initial capacity of twice the number of mappings in the specified map or 11 (whichever is greater), and a default load factor, which is 0.75.
        Parameters:
        t - the map whose mappings are to be placed in this map.
    • Method Detail

      • entrySet

        public java.util.Set entrySet()
        Returns a set of the mappings contained in this hash table.
        Specified by:
        entrySet in interface java.util.Map
        Specified by:
        entrySet in class java.util.AbstractMap
      • processQueue

        private void processQueue()
      • size

        public int size()
        Returns the number of key-value mappings in this map. Note: In contrast with most implementations of the Map interface, the time required by this operation is linear in the size of the map.
        Specified by:
        size in interface java.util.Map
        Overrides:
        size in class java.util.AbstractMap
      • isEmpty

        public boolean isEmpty()
        Returns true if this map contains no key-value mappings.
        Specified by:
        isEmpty in interface java.util.Map
        Overrides:
        isEmpty in class java.util.AbstractMap
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns true if this map contains a mapping for the specified key.
        Specified by:
        containsKey in interface java.util.Map
        Overrides:
        containsKey in class java.util.AbstractMap
        Parameters:
        key - The key whose presence in this map is to be tested.
      • get

        public java.lang.Object get​(java.lang.Object key)
        Returns the value to which this map maps the specified key. If this map does not contain a value for this key, then return null.
        Specified by:
        get in interface java.util.Map
        Overrides:
        get in class java.util.AbstractMap
        Parameters:
        key - The key whose associated value, if any, is to be returned.
      • put

        public java.lang.Object put​(java.lang.Object key,
                                    java.lang.Object value)
        Updates this map so that the given key maps to the given value. If the map previously contained a mapping for key then that mapping is replaced and the previous value is returned.
        Specified by:
        put in interface java.util.Map
        Overrides:
        put in class java.util.AbstractMap
        Parameters:
        key - The key that is to be mapped to the given value
        value - The value to which the given key is to be mapped
        Returns:
        The previous value to which this key was mapped, or null if if there was no mapping for the key
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        Removes the mapping for the given key from this map, if present.
        Specified by:
        remove in interface java.util.Map
        Overrides:
        remove in class java.util.AbstractMap
        Parameters:
        key - The key whose mapping is to be removed.
        Returns:
        The value to which this key was mapped, or null if there was no mapping for the key.
      • clear

        public void clear()
        Removes all mappings from this map.
        Specified by:
        clear in interface java.util.Map
        Overrides:
        clear in class java.util.AbstractMap