Class ReferenceHashMap

All Implemented Interfaces:
Serializable, Map, IterableMap, ReferenceMap, SizedMap

public class ReferenceHashMap extends ReferenceMap implements ReferenceMap, SizedMap
Map in which the key, value, or both may be weak/soft references.
Since:
0.4.0
Author:
Abe White
See Also:
  • Constructor Details

  • Method Details

    • getMaxSize

      public int getMaxSize()
      Description copied from interface: SizedMap
      The maximum number of entries, or Integer.MAX_VALUE for no limit.
      Specified by:
      getMaxSize in interface SizedMap
    • setMaxSize

      public void setMaxSize(int maxSize)
      Description copied from interface: SizedMap
      The maximum number of entries, or Integer.MAX_VALUE for no limit.
      Specified by:
      setMaxSize in interface SizedMap
    • isFull

      public boolean isFull()
      Description copied from interface: SizedMap
      Whether the map is full.
      Specified by:
      isFull in interface SizedMap
    • overflowRemoved

      public void overflowRemoved(Object key, Object value)
      Description copied from interface: SizedMap
      Overridable callback for when an overflow entry is automatically removed.
      Specified by:
      overflowRemoved in interface SizedMap
    • valueExpired

      public void valueExpired(Object key)
      Description copied from interface: ReferenceMap
      Overridable callback for when a value reference expires.
      Specified by:
      valueExpired in interface ReferenceMap
      Parameters:
      key - the key for the expired value
    • keyExpired

      public void keyExpired(Object value)
      Description copied from interface: ReferenceMap
      Overridable callback for when a key reference expires.
      Specified by:
      keyExpired in interface ReferenceMap
      Parameters:
      value - the value for the expired key
    • removeExpired

      public void removeExpired()
      Description copied from interface: ReferenceMap
      Purge stale entries.
      Specified by:
      removeExpired in interface ReferenceMap
    • addMapping

      protected void addMapping(int hashIndex, int hashCode, Object key, Object value)
      Description copied from class: AbstractHashedMap
      Adds a new key-value mapping into this map.

      This implementation calls createEntry(), addEntry() and checkCapacity(). It also handles changes to modCount and size. Subclasses could override to fully control adds to the map.

      Overrides:
      addMapping in class AbstractHashedMap
      Parameters:
      hashIndex - the index into the data array to store at
      hashCode - the hash code of the key to add
      key - the key to add
      value - the value to add
    • createEntry

      protected AbstractReferenceMap.ReferenceEntry createEntry(AbstractHashedMap.HashEntry next, int hashCode, Object key, Object value)
      Description copied from class: AbstractReferenceMap
      Creates a ReferenceEntry instead of a HashEntry.
      Overrides:
      createEntry in class AbstractReferenceMap
      Parameters:
      next - the next entry in sequence
      hashCode - the hash code to use
      key - the key to store
      value - the value to store
      Returns:
      the newly created entry
    • doWriteObject

      protected void doWriteObject(ObjectOutputStream out) throws IOException
      Description copied from class: AbstractReferenceMap
      Replaces the superclass method to store the state of this class.

      Serialization is not one of the JDK's nicest topics. Normal serialization will initialise the superclass before the subclass. Sometimes however, this isn't what you want, as in this case the put() method on read can be affected by subclass state.

      The solution adopted here is to serialize the state data of this class in this protected method. This method must be called by the writeObject() of the first serializable subclass.

      Subclasses may override if they have a specific field that must be present on read before this implementation will work. Generally, the read determines what must be serialized here, if anything.

      Overrides:
      doWriteObject in class AbstractReferenceMap
      Parameters:
      out - the output stream
      Throws:
      IOException - if an error occurs while writing to the stream
    • doReadObject

      protected void doReadObject(ObjectInputStream in) throws ClassNotFoundException, IOException
      Description copied from class: AbstractReferenceMap
      Replaces the superclass method to read the state of this class.

      Serialization is not one of the JDK's nicest topics. Normal serialization will initialise the superclass before the subclass. Sometimes however, this isn't what you want, as in this case the put() method on read can be affected by subclass state.

      The solution adopted here is to deserialize the state data of this class in this protected method. This method must be called by the readObject() of the first serializable subclass.

      Subclasses may override if the subclass has a specific field that must be present before put() or calculateThreshold() will work correctly.

      Overrides:
      doReadObject in class AbstractReferenceMap
      Parameters:
      in - the input stream
      Throws:
      ClassNotFoundException - if an object read from the stream can not be loaded
      IOException - if an error occurs while reading from the stream