Class SoftHashMap

  • All Implemented Interfaces:
    java.util.Map

    public class SoftHashMap
    extends java.util.AbstractMap
    Version:
    $Id: SoftHashMap.java,v 1.1 2004/10/23 15:24:35 thlee Exp $ $Name: $ Credits: Article by Heinz Kabutz at http://archive.devx.com/java/free/articles/Kabutz01/Kabutz01-1.asp
    Author:
    TiongHiang Lee (thlee@onemindsoft.org)
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  SoftHashMap.SoftValue
      We define our own subclass of SoftReference which contains not only the value but also the key to make it easier to find the entry in the HashMap after it's been garbage collected.
      • 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 HARD_REF_SIZE
      The number of "hard" references to hold internally.
      private java.util.LinkedList hardRefCache
      The FIFO list of hard references, order of last access.
      private java.util.Map hash
      The internal HashMap that will hold the SoftReference.
      private java.lang.ref.ReferenceQueue queue
      Reference queue for cleared SoftReference objects.
    • Constructor Summary

      Constructors 
      Constructor Description
      SoftHashMap()
      Constructor
      SoftHashMap​(int hardSize)
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void _cleanCollectedValues()
      Go through the ReferenceQueue and remove garbage collected SoftValue objects from the HashMap
      void clear()
      java.util.Set entrySet()
      java.lang.Object get​(java.lang.Object key)  
      java.lang.Object put​(java.lang.Object key, java.lang.Object value)
      Here we put the key, value pair into the HashMap using a SoftValue object.
      java.lang.Object remove​(java.lang.Object key)
      int size()
      • Methods inherited from class java.util.AbstractMap

        clone, containsKey, containsValue, equals, hashCode, isEmpty, 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, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • hash

        private final java.util.Map hash
        The internal HashMap that will hold the SoftReference.
      • HARD_REF_SIZE

        private final int HARD_REF_SIZE
        The number of "hard" references to hold internally.
      • hardRefCache

        private final java.util.LinkedList hardRefCache
        The FIFO list of hard references, order of last access.
      • queue

        private final java.lang.ref.ReferenceQueue queue
        Reference queue for cleared SoftReference objects.
    • Constructor Detail

      • SoftHashMap

        public SoftHashMap()
        Constructor
      • SoftHashMap

        public SoftHashMap​(int hardSize)
        Constructor
        Parameters:
        hardSize - the hard reference size to maintain
    • Method Detail

      • get

        public java.lang.Object get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map
        Overrides:
        get in class java.util.AbstractMap
      • _cleanCollectedValues

        private void _cleanCollectedValues()
        Go through the ReferenceQueue and remove garbage collected SoftValue objects from the HashMap
      • put

        public java.lang.Object put​(java.lang.Object key,
                                    java.lang.Object value)
        Here we put the key, value pair into the HashMap using a SoftValue object.
        Specified by:
        put in interface java.util.Map
        Overrides:
        put in class java.util.AbstractMap
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map
        Overrides:
        remove in class java.util.AbstractMap
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map
        Overrides:
        clear in class java.util.AbstractMap
      • size

        public int size()
        Specified by:
        size in interface java.util.Map
        Overrides:
        size in class java.util.AbstractMap
      • entrySet

        public java.util.Set entrySet()
        Specified by:
        entrySet in interface java.util.Map
        Specified by:
        entrySet in class java.util.AbstractMap