Class WeakFastHashMap<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​V>

    class WeakFastHashMap<K,​V>
    extends java.util.HashMap<K,​V>

    A customized implementation of java.util.HashMap designed to operate in a multithreaded environment where the large majority of method calls are read-only, instead of structural changes. When operating in "fast" mode, read calls are non-synchronized and write calls perform the following steps:

    • Clone the existing collection
    • Perform the modification on the clone
    • Replace the existing collection with the (modified) clone

    When first created, objects of this class default to "slow" mode, where all accesses of any type are synchronized but no cloning takes place. This is appropriate for initially populating the collection, followed by a switch to "fast" mode (by calling setFast(true)) after initialization is complete.

    NOTE: If you are creating and accessing a HashMap only within a single thread, you should use java.util.HashMap directly (with no synchronization), for maximum performance.

    NOTE: This class is not cross-platform. Using it may cause unexpected failures on some architectures. It suffers from the same problems as the double-checked locking idiom. In particular, the instruction that clones the internal collection and the instruction that sets the internal reference to the clone can be executed or perceived out-of-order. This means that any read operation might fail unexpectedly, as it may be reading the state of the internal collection before the internal collection is fully formed. For more information on the double-checked locking idiom, see the Double-Checked Locking Idiom Is Broken Declaration.

    Since:
    Commons Collections 1.0
    Version:
    $Id: WeakFastHashMap.java 1540186 2013-11-08 21:08:30Z oheger $
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private class  WeakFastHashMap.CollectionView<E>
      Abstract collection implementation shared by keySet(), values() and entrySet().
      private class  WeakFastHashMap.EntrySet
      Set implementation over the entries of the FastHashMap
      private class  WeakFastHashMap.KeySet
      Set implementation over the keys of the FastHashMap
      private class  WeakFastHashMap.Values
      Collection implementation over the values of the FastHashMap
      • 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 boolean fast
      Are we currently operating in "fast" mode?
      private java.util.Map<K,​V> map
      The underlying map we are managing.
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakFastHashMap()
      Construct an empty map.
      WeakFastHashMap​(int capacity)
      Construct an empty map with the specified capacity.
      WeakFastHashMap​(int capacity, float factor)
      Construct an empty map with the specified capacity and load factor.
      WeakFastHashMap​(java.util.Map<? extends K,​? extends V> map)
      Construct a new map with the same mappings as the specified map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Remove all mappings from this map.
      java.lang.Object clone()
      Return a shallow copy of this FastHashMap instance.
      protected java.util.Map<K,​V> cloneMap​(java.util.Map<? extends K,​? extends V> map)  
      boolean containsKey​(java.lang.Object key)
      Return true if this map contains a mapping for the specified key.
      boolean containsValue​(java.lang.Object value)
      Return true if this map contains one or more keys mapping to the specified value.
      protected java.util.Map<K,​V> createMap()  
      protected java.util.Map<K,​V> createMap​(int capacity)  
      protected java.util.Map<K,​V> createMap​(int capacity, float factor)  
      protected java.util.Map<K,​V> createMap​(java.util.Map<? extends K,​? extends V> map)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Return a collection view of the mappings contained in this map.
      boolean equals​(java.lang.Object o)
      Compare the specified object with this list for equality.
      V get​(java.lang.Object key)
      Return the value to which this map maps the specified key.
      boolean getFast()
      Returns true if this map is operating in fast mode.
      int hashCode()
      Return the hash code value for this map.
      boolean isEmpty()
      Return true if this map contains no mappings.
      java.util.Set<K> keySet()
      Return a set view of the keys contained in this map.
      V put​(K key, V value)
      Associate the specified value with the specified key in this map.
      void putAll​(java.util.Map<? extends K,​? extends V> in)
      Copy all of the mappings from the specified map to this one, replacing any mappings with the same keys.
      V remove​(java.lang.Object key)
      Remove any mapping for this key, and return any previously mapped value.
      void setFast​(boolean fast)
      Sets whether this map is operating in fast mode.
      int size()
      Return the number of key-value mappings in this map.
      java.util.Collection<V> values()
      Return a collection view of the values contained in this map.
      • Methods inherited from class java.util.HashMap

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
      • Methods inherited from class java.util.AbstractMap

        toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • map

        private java.util.Map<K,​V> map
        The underlying map we are managing.
      • fast

        private boolean fast
        Are we currently operating in "fast" mode?
    • Constructor Detail

      • WeakFastHashMap

        public WeakFastHashMap()
        Construct an empty map.
      • WeakFastHashMap

        public WeakFastHashMap​(int capacity)
        Construct an empty map with the specified capacity.
        Parameters:
        capacity - the initial capacity of the empty map
      • WeakFastHashMap

        public WeakFastHashMap​(int capacity,
                               float factor)
        Construct an empty map with the specified capacity and load factor.
        Parameters:
        capacity - the initial capacity of the empty map
        factor - the load factor of the new map
      • WeakFastHashMap

        public WeakFastHashMap​(java.util.Map<? extends K,​? extends V> map)
        Construct a new map with the same mappings as the specified map.
        Parameters:
        map - the map whose mappings are to be copied
    • Method Detail

      • getFast

        public boolean getFast()
        Returns true if this map is operating in fast mode.
        Returns:
        true if this map is operating in fast mode
      • setFast

        public void setFast​(boolean fast)
        Sets whether this map is operating in fast mode.
        Parameters:
        fast - true if this map should operate in fast mode
      • get

        public V get​(java.lang.Object key)
        Return the value to which this map maps the specified key. Returns null if the map contains no mapping for this key, or if there is a mapping with a value of null. Use the containsKey() method to disambiguate these cases.
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.HashMap<K,​V>
        Parameters:
        key - the key whose value is to be returned
        Returns:
        the value mapped to that key, or null
      • size

        public int size()
        Return the number of key-value mappings in this map.
        Specified by:
        size in interface java.util.Map<K,​V>
        Overrides:
        size in class java.util.HashMap<K,​V>
        Returns:
        the current size of the map
      • isEmpty

        public boolean isEmpty()
        Return true if this map contains no mappings.
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Overrides:
        isEmpty in class java.util.HashMap<K,​V>
        Returns:
        is the map currently empty
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Return true if this map contains a mapping for the specified key.
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Overrides:
        containsKey in class java.util.HashMap<K,​V>
        Parameters:
        key - the key to be searched for
        Returns:
        true if the map contains the key
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Return true if this map contains one or more keys mapping to the specified value.
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Overrides:
        containsValue in class java.util.HashMap<K,​V>
        Parameters:
        value - the value to be searched for
        Returns:
        true if the map contains the value
      • put

        public V put​(K key,
                     V value)
        Associate the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced and returned.
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.HashMap<K,​V>
        Parameters:
        key - the key with which the value is to be associated
        value - the value to be associated with this key
        Returns:
        the value previously mapped to the key, or null
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> in)
        Copy all of the mappings from the specified map to this one, replacing any mappings with the same keys.
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Overrides:
        putAll in class java.util.HashMap<K,​V>
        Parameters:
        in - the map whose mappings are to be copied
      • remove

        public V remove​(java.lang.Object key)
        Remove any mapping for this key, and return any previously mapped value.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.HashMap<K,​V>
        Parameters:
        key - the key whose mapping is to be removed
        Returns:
        the value removed, or null
      • clear

        public void clear()
        Remove all mappings from this map.
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.HashMap<K,​V>
      • equals

        public boolean equals​(java.lang.Object o)
        Compare the specified object with this list for equality. This implementation uses exactly the code that is used to define the list equals function in the documentation for the Map.equals method.
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.util.AbstractMap<K,​V>
        Parameters:
        o - the object to be compared to this list
        Returns:
        true if the two maps are equal
      • hashCode

        public int hashCode()
        Return the hash code value for this map. This implementation uses exactly the code that is used to define the list hash function in the documentation for the Map.hashCode method.
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.util.AbstractMap<K,​V>
        Returns:
        suitable integer hash code
      • clone

        public java.lang.Object clone()
        Return a shallow copy of this FastHashMap instance. The keys and values themselves are not copied.
        Overrides:
        clone in class java.util.HashMap<K,​V>
        Returns:
        a clone of this map
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Return a collection view of the mappings contained in this map. Each element in the returned collection is a Map.Entry.
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Overrides:
        entrySet in class java.util.HashMap<K,​V>
        Returns:
        the set of map Map entries
      • keySet

        public java.util.Set<K> keySet()
        Return a set view of the keys contained in this map.
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Overrides:
        keySet in class java.util.HashMap<K,​V>
        Returns:
        the set of the Map's keys
      • values

        public java.util.Collection<V> values()
        Return a collection view of the values contained in this map.
        Specified by:
        values in interface java.util.Map<K,​V>
        Overrides:
        values in class java.util.HashMap<K,​V>
        Returns:
        the set of the Map's values
      • createMap

        protected java.util.Map<K,​V> createMap()
      • createMap

        protected java.util.Map<K,​V> createMap​(int capacity)
      • createMap

        protected java.util.Map<K,​V> createMap​(int capacity,
                                                     float factor)
      • createMap

        protected java.util.Map<K,​V> createMap​(java.util.Map<? extends K,​? extends V> map)
      • cloneMap

        protected java.util.Map<K,​V> cloneMap​(java.util.Map<? extends K,​? extends V> map)