Class ConcurrentReferenceHashMap

  • All Implemented Interfaces:
    java.lang.Cloneable, java.util.Map, ConcurrentMap, ReferenceMap, SizedMap

    public class ConcurrentReferenceHashMap
    extends java.util.AbstractMap
    implements ConcurrentMap, ReferenceMap, SizedMap, java.lang.Cloneable
    This class implements a HashMap which has limited synchronization and reference keys or values(but not both). In particular mutators are generally synchronized while accessors are generally not. Additionally the Iterators returned by this class are not "fail-fast", but instead try to continue to iterate over the data structure after changes have been made. Finally purging of the reference queue is only done inside mutators. Null keys are not supported if keys use references. Null values are not supported if values use references. This class is based heavily on the WeakHashMap class in the Java collections package.
    • Nested Class Summary

      • 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>
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all mappings from this HashMap.
      java.lang.Object clone()
      Returns a shallow copy of this HashMap.
      boolean containsKey​(java.lang.Object key)
      Returns true if this HashMap contains a mapping for the specified key.
      boolean containsValue​(java.lang.Object value)
      Returns true if this HashMap maps one or more keys to the specified value.
      java.util.Set entrySet()
      Returns a Collection view of the mappings contained in this HashMap.
      protected boolean eq​(java.lang.Object x, java.lang.Object y)
      Compare two objects.
      java.lang.Object get​(java.lang.Object key)
      Returns the value to which this HashMap maps the specified key.
      int getMaxSize()
      The maximum number of entries, or Integer.MAX_VALUE for no limit.
      protected int hc​(java.lang.Object o)
      Obtain the hashcode of an object.
      boolean isEmpty()
      Returns true if this Map contains no key-value mappings.
      boolean isFull()
      Whether the map is full.
      void keyExpired​(java.lang.Object value)
      Overridable callback for when a key reference expires.
      java.util.Set keySet()
      Returns a Set view of the keys contained in this HashMap.
      void overflowRemoved​(java.lang.Object key, java.lang.Object value)
      Overridable callback for when an overflow entry is automatically removed.
      java.lang.Object put​(java.lang.Object key, java.lang.Object value)
      Associates the specified value with the specified key in this HashMap.
      void putAll​(java.util.Map t)
      Copies all of the mappings from the specified Map to this HashMap These mappings will replace any mappings that this HashMap had for any of the keys currently in the specified Map.
      java.util.Iterator randomEntryIterator()
      Iterate over map entries, beginning at an arbitrary (not strictly random) entry.
      java.lang.Object remove​(java.lang.Object key)
      Removes the mapping for this key from this HashMap if present.
      void removeExpired()
      Purge stale entries.
      java.util.Map.Entry removeRandom()
      Remove an arbitrary(not strictly random) entry from the map.
      void setMaxSize​(int maxSize)
      The maximum number of entries, or Integer.MAX_VALUE for no limit.
      int size()
      Returns the number of key-value mappings in this Map.
      void valueExpired​(java.lang.Object key)
      Overridable callback for when a value reference expires.
      java.util.Collection values()
      Returns a Collection view of the values contained in this HashMap.
      • Methods inherited from class java.util.AbstractMap

        equals, hashCode, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, 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

      • ConcurrentReferenceHashMap

        public ConcurrentReferenceHashMap​(AbstractReferenceMap.ReferenceStrength keyType,
                                          AbstractReferenceMap.ReferenceStrength valueType,
                                          int initialCapacity,
                                          float loadFactor)
        Constructs a new, empty HashMap with the specified initial capacity and the specified load factor.
        Parameters:
        keyType - the reference type of map keys
        valueType - the reference type of map values
        initialCapacity - the initial capacity of the HashMap.
        loadFactor - a number between 0.0 and 1.0.
        Throws:
        java.lang.IllegalArgumentException - if neither keys nor values use hard references, if the initial capacity is less than or equal to zero, or if the load factor is less than or equal to zero
      • ConcurrentReferenceHashMap

        public ConcurrentReferenceHashMap​(AbstractReferenceMap.ReferenceStrength keyType,
                                          AbstractReferenceMap.ReferenceStrength valueType,
                                          int initialCapacity)
        Constructs a new, empty HashMap with the specified initial capacity and default load factor.
        Parameters:
        keyType - the reference type of map keys
        valueType - the reference type of map values
        initialCapacity - the initial capacity of the HashMap.
      • ConcurrentReferenceHashMap

        public ConcurrentReferenceHashMap​(AbstractReferenceMap.ReferenceStrength keyType,
                                          AbstractReferenceMap.ReferenceStrength valueType,
                                          java.util.Map t)
        Constructs a new HashMap with the same mappings as the given Map. The HashMap is created with a capacity of thrice the number of entries in the given Map or 11 (whichever is greater), and a default load factor.
        Parameters:
        keyType - the reference type of map keys
        valueType - the reference type of map values
    • Method Detail

      • eq

        protected boolean eq​(java.lang.Object x,
                             java.lang.Object y)
        Compare two objects. These might be keys, values, or Entry instances. This implementation uses a normal null-safe object equality algorithm.
        Since:
        1.0.0
      • hc

        protected int hc​(java.lang.Object o)
        Obtain the hashcode of an object. The object might be a key, a value, or an Entry. This implementation just delegates to Object.hashCode()
        Since:
        1.0.0
      • 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​(java.lang.Object key,
                                    java.lang.Object value)
        Description copied from interface: SizedMap
        Overridable callback for when an overflow entry is automatically removed.
        Specified by:
        overflowRemoved in interface SizedMap
      • size

        public int size()
        Returns the number of key-value mappings in this Map. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.
        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. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.
        Specified by:
        isEmpty in interface java.util.Map
        Overrides:
        isEmpty in class java.util.AbstractMap
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Returns true if this HashMap maps one or more keys to the specified value.
        Specified by:
        containsValue in interface java.util.Map
        Overrides:
        containsValue in class java.util.AbstractMap
        Parameters:
        value - value whose presence in this Map is to be tested.
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns true if this HashMap contains a mapping for the specified key.
        Specified by:
        containsKey in interface java.util.Map
        Overrides:
        containsKey in class java.util.AbstractMap
        Parameters:
        key - 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 HashMap maps the specified key. Returns null if the HashMap contains no mapping for this key.
        Specified by:
        get in interface java.util.Map
        Overrides:
        get in class java.util.AbstractMap
        Parameters:
        key - key whose associated value is to be returned.
      • put

        public java.lang.Object put​(java.lang.Object key,
                                    java.lang.Object value)
        Associates the specified value with the specified key in this HashMap. If the HashMap previously contained a mapping for this key, the old value is replaced.
        Specified by:
        put in interface java.util.Map
        Overrides:
        put in class java.util.AbstractMap
        Parameters:
        key - key with which the specified value is to be associated.
        value - value to be associated with the specified key.
        Returns:
        previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the HashMap previously associated null with the specified key.
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        Removes the mapping for this key from this HashMap if present.
        Specified by:
        remove in interface java.util.Map
        Overrides:
        remove in class java.util.AbstractMap
        Parameters:
        key - key whose mapping is to be removed from the Map.
        Returns:
        previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the HashMap previously associated null with the specified key.
      • keyExpired

        public void keyExpired​(java.lang.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
      • valueExpired

        public void valueExpired​(java.lang.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
      • removeRandom

        public java.util.Map.Entry removeRandom()
        Description copied from interface: ConcurrentMap
        Remove an arbitrary(not strictly random) entry from the map. This allows implementation of concurrent caches with size ceilings.
        Specified by:
        removeRandom in interface ConcurrentMap
        Returns:
        the removed entry, or null if map is empty
      • randomEntryIterator

        public java.util.Iterator randomEntryIterator()
        Description copied from interface: ConcurrentMap
        Iterate over map entries, beginning at an arbitrary (not strictly random) entry.
        Specified by:
        randomEntryIterator in interface ConcurrentMap
      • putAll

        public void putAll​(java.util.Map t)
        Copies all of the mappings from the specified Map to this HashMap These mappings will replace any mappings that this HashMap had for any of the keys currently in the specified Map.
        Specified by:
        putAll in interface java.util.Map
        Overrides:
        putAll in class java.util.AbstractMap
        Parameters:
        t - Mappings to be stored in this Map.
      • clear

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

        public java.lang.Object clone()
        Returns a shallow copy of this HashMap. The keys and values themselves are not cloned.
        Overrides:
        clone in class java.util.AbstractMap
      • keySet

        public java.util.Set keySet()
        Returns a Set view of the keys contained in this HashMap. The Set is backed by the HashMap, so changes to the HashMap are reflected in the Set, and vice-versa. The Set supports element removal, which removes the corresponding mapping from the HashMap, via the Iterator.remove, Set.remove, removeAll retainAll, and clear operations. It does not support the add or addAll operations.
        Specified by:
        keySet in interface java.util.Map
        Overrides:
        keySet in class java.util.AbstractMap
      • values

        public java.util.Collection values()
        Returns a Collection view of the values contained in this HashMap. The Collection is backed by the HashMap, so changes to the HashMap are reflected in the Collection, and vice-versa. The Collection supports element removal, which removes the corresponding mapping from the HashMap, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
        Specified by:
        values in interface java.util.Map
        Overrides:
        values in class java.util.AbstractMap
      • entrySet

        public java.util.Set entrySet()
        Returns a Collection view of the mappings contained in this HashMap. Each element in the returned collection is a Map.Entry. The Collection is backed by the HashMap, so changes to the HashMap are reflected in the Collection, and vice-versa. The Collection supports element removal, which removes the corresponding mapping from the HashMap, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
        Specified by:
        entrySet in interface java.util.Map
        Specified by:
        entrySet in class java.util.AbstractMap
        See Also:
        Map.Entry