groovyx.gpars.extra166y
Class CustomConcurrentHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by groovyx.gpars.extra166y.CustomConcurrentHashMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
java.io.Serializable, java.util.concurrent.ConcurrentMap<K,V>, java.util.Map<K,V>

public class CustomConcurrentHashMap<K,V>
extends java.util.AbstractMap<K,V>
implements java.util.concurrent.ConcurrentMap<K,V>, java.io.Serializable

A java.util.ConcurrentMap supporting user-defined equivalence comparisons, soft, weak, or strong keys and values, and user-supplied computational methods for setting and updating values. In particular:

Per-map settings are established in constructors, as in the following usages (that assume static imports to simplify expression of configuration parameters):
 identityMap = new CustomConcurrentHashMap<Person,Salary>
     (STRONG, IDENTITY, STRONG, EQUALS, 0);
 weakKeyMap = new CustomConcurrentHashMap<Person,Salary>
     (WEAK, IDENTITY, STRONG, EQUALS, 0);
     .weakKeys());
 byNameMap = new CustomConcurrentHashMap<Person,Salary>
     (STRONG,
      new Equivalence<Person>() {
          public boolean equal(Person k, Object x) {
            return x instanceof Person && k.name.equals(((Person)x).name);
          }
          public int hash(Object x) {
             return (x instanceof Person) ? ((Person)x).name.hashCode() : 0;
          }
        },
      STRONG, EQUALS, 0);
 
 
The first usage above provides a replacement for IdentityHashMap, and the second a replacement for WeakHashMap, adding concurrency, asynchronous cleanup, and identity-based equality for keys. The third usage illustrates a map with a custom Equivalence that looks only at the name field of a (fictional) Person class.

This class also includes nested class CustomConcurrentHashMap.KeySet that provides space-efficient Set views of maps, also supporting method intern, which may be of use in canonicalizing elements.

When used with (Weak or Soft) Reference keys and/or values, elements that have asynchronously become null are treated as absent from the map and (eventually) removed from maps via a background thread common across all maps. Because of the potential for asynchronous clearing of References, methods such as containsValue have weaker guarantees than you might expect even in the absence of other explicitly concurrent operations. For example containsValue(value) may return true even if value is no longer available upon return from the method.

When Equivalences other than equality are used, the returned collections may violate the specifications of Map and/or Set interfaces, which mandate the use of the equals method when comparing objects. The methods of this class otherwise have properties similar to those of java.util.ConcurrentHashMap under its default settings. To adaptively maintain semantics and performance under varying conditions, this class does not support load factor or concurrency level parameters. This class does not permit null keys or values. This class is serializable; however, serializing a map that uses soft or weak references can give unpredictable results. This class supports all optional operations of the ConcurrentMap interface. It supports have weakly consistent iteration: an iterator over one of the map's view collections may reflect some, all or none of the changes made to the collection after the iterator was created.

This class is a member of the Java Collections Framework.

See Also:
Serialized Form

Nested Class Summary
(package private) static class CustomConcurrentHashMap.EmbeddedSoftReference
           
(package private) static class CustomConcurrentHashMap.EmbeddedWeakReference
           
(package private)  class CustomConcurrentHashMap.EntryIterator
           
(package private)  class CustomConcurrentHashMap.EntrySet
           
static interface CustomConcurrentHashMap.Equivalence<K>
          An object performing equality comparisons, along with a hash function consistent with this comparison.
(package private) static class CustomConcurrentHashMap.EquivalenceUsingEquals
           
(package private) static class CustomConcurrentHashMap.EquivalenceUsingIdentity
           
(package private)  class CustomConcurrentHashMap.HashIterator
           
(package private) static class CustomConcurrentHashMap.IntKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.IntKeyIntValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.IntKeyNode
           
(package private) static class CustomConcurrentHashMap.IntKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.IntKeySelfValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.IntKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.IntKeySoftValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.IntKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.IntKeyStrongValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.IntKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.IntKeyWeakValueNodeFactory
           
(package private)  class CustomConcurrentHashMap.KeyIterator
           
static class CustomConcurrentHashMap.KeySet<K>
          A hash-based set with properties identical to those of Collections.newSetFromMap applied to a CustomConcurrentHashMap, but possibly more space-efficient.
(package private)  class CustomConcurrentHashMap.KeySetView
           
(package private) static class CustomConcurrentHashMap.LinkedIntKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedIntKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedIntKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedIntKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedIntKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedSoftKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedSoftKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedSoftKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedSoftKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedSoftKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedStrongKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedStrongKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedStrongKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedStrongKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedStrongKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedWeakKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedWeakKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedWeakKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedWeakKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.LinkedWeakKeyWeakValueNode
           
static interface CustomConcurrentHashMap.MappingFunction<K,V>
          A function computing a mapping from the given key to a value, or null if there is no mapping.
(package private) static interface CustomConcurrentHashMap.Node
          An object maintaining a key-value mapping.
(package private) static interface CustomConcurrentHashMap.NodeFactory
          A factory for Nodes.
(package private) static interface CustomConcurrentHashMap.Reclaimable
          An object that may be subject to cleanup operations when removed from a ReferenceQueue
(package private) static class CustomConcurrentHashMap.ReclamationThread
           
static interface CustomConcurrentHashMap.RemappingFunction<K,V>
          A function computing a new mapping from the given key and its current value to a new value, or null if there is no mapping
(package private) static class CustomConcurrentHashMap.Segment
          Each Segment holds a count and table corresponding to a segment of the table.
(package private) static class CustomConcurrentHashMap.SoftKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.SoftKeyIntValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.SoftKeyNode
           
(package private) static class CustomConcurrentHashMap.SoftKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.SoftKeySelfValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.SoftKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.SoftKeySoftValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.SoftKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.SoftKeyStrongValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.SoftKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.SoftKeyWeakValueNodeFactory
           
static class CustomConcurrentHashMap.Strength
          The strength of keys and values that may be held by maps. strong denotes ordinary objects. weak and soft denote the corresponding Reference types.
(package private) static class CustomConcurrentHashMap.StrongKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.StrongKeyIntValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.StrongKeyNode
           
(package private) static class CustomConcurrentHashMap.StrongKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.StrongKeySelfValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.StrongKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.StrongKeySoftValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.StrongKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.StrongKeyStrongValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.StrongKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.StrongKeyWeakValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.TerminalIntKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalIntKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalIntKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalIntKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalIntKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalSoftKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalSoftKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalSoftKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalSoftKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalSoftKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalStrongKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalStrongKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalStrongKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalStrongKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalStrongKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalWeakKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalWeakKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalWeakKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalWeakKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.TerminalWeakKeyWeakValueNode
           
(package private)  class CustomConcurrentHashMap.ValueIterator
           
(package private)  class CustomConcurrentHashMap.Values
           
(package private) static class CustomConcurrentHashMap.WeakKeyIntValueNode
           
(package private) static class CustomConcurrentHashMap.WeakKeyIntValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.WeakKeyNode
           
(package private) static class CustomConcurrentHashMap.WeakKeySelfValueNode
           
(package private) static class CustomConcurrentHashMap.WeakKeySelfValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.WeakKeySoftValueNode
           
(package private) static class CustomConcurrentHashMap.WeakKeySoftValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.WeakKeyStrongValueNode
           
(package private) static class CustomConcurrentHashMap.WeakKeyStrongValueNodeFactory
           
(package private) static class CustomConcurrentHashMap.WeakKeyWeakValueNode
           
(package private) static class CustomConcurrentHashMap.WeakKeyWeakValueNodeFactory
           
(package private)  class CustomConcurrentHashMap.WriteThroughEntry
           
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
(package private)  java.util.Set<java.util.Map.Entry<K,V>> entrySet
           
static CustomConcurrentHashMap.Equivalence<java.lang.Object> EQUALS
          An Equivalence object performing Object.equals(java.lang.Object) based comparisons and using Object.hashCode() hashing
(package private)  CustomConcurrentHashMap.NodeFactory factory
          The factory for this map
static CustomConcurrentHashMap.Equivalence<java.lang.Object> IDENTITY
          An Equivalence object performing identity-based comparisons and using System.identityHashCode(java.lang.Object) for hashing
(package private)  int initialSegmentCapacity
          The initial size of Segment tables when they are first constructed
private static java.lang.String INT_STRING
          Config string for int maps
(package private)  CustomConcurrentHashMap.Equivalence<? super K> keyEquivalence
          Equivalence object for keys
(package private)  java.util.Set<K> keySet
           
(package private) static int MAX_SEGMENT_CAPACITY
           
(package private) static int MIN_SEGMENT_CAPACITY
           
(package private) static int NSEGMENTS
           
(package private) static java.lang.ref.ReferenceQueue<java.lang.Object> refQueue
           
(package private) static int SEGMENT_BITS
           
(package private) static int SEGMENT_MASK
           
(package private) static int SEGMENT_SHIFT
           
(package private)  CustomConcurrentHashMap.Segment[] segments
          The segments, each of which acts as a hash table
(package private) static long segmentsBase
           
(package private) static int segmentsShift
           
private static java.lang.String SELF_STRING
          Config string for self-map (Set view) refs
private static long serialVersionUID
           
static CustomConcurrentHashMap.Strength SOFT
          The strength of soft references
static CustomConcurrentHashMap.Strength STRONG
          The strength of ordinary references
(package private) static long tableBase
           
(package private) static int tableShift
           
(package private) static sun.misc.Unsafe UNSAFE
           
(package private)  CustomConcurrentHashMap.Equivalence<? super V> valueEquivalence
          Equivalence object for values
(package private)  java.util.Collection<V> values
           
static CustomConcurrentHashMap.Strength WEAK
          The strength of weak references
 
Constructor Summary
CustomConcurrentHashMap()
          Creates a new CustomConcurrentHashMap with strong keys and values, and equality-based equivalence.
CustomConcurrentHashMap(CustomConcurrentHashMap.Strength keyStrength, CustomConcurrentHashMap.Equivalence<? super K> keyEquivalence, CustomConcurrentHashMap.Strength valueStrength, CustomConcurrentHashMap.Equivalence<? super V> valueEquivalence, int expectedSize)
          Creates a new CustomConcurrentHashMap with the given parameters
CustomConcurrentHashMap(java.lang.String ks, CustomConcurrentHashMap.Equivalence<? super K> keq, java.lang.String vs, CustomConcurrentHashMap.Equivalence<? super V> veq, int expectedSize)
          Internal constructor to set factory, equivalences and segment capacities, and to create segments array.
 
Method Summary
 void clear()
          Removes all of the mappings from this map.
 V compute(K key, CustomConcurrentHashMap.RemappingFunction<? super K,V> remappingFunction)
          Updates the mapping for the given key with the result of the given remappingFunction.
 V computeIfAbsent(K key, CustomConcurrentHashMap.MappingFunction<? super K,? extends V> mappingFunction)
          If the specified key is not already associated with a value, computes its value using the given mappingFunction, and if non-null, enters it into the map.
 boolean containsKey(java.lang.Object key)
          Returns true if this map contains a key equivalent to the given key with respect to this map's key Equivalence.
 boolean containsValue(java.lang.Object value)
          Returns true if this map maps one or more keys to a value equivalent to the given value with respect to this map's value Equivalence.
(package private)  V doPut(K key, V value, boolean onlyIfNull)
          Shared implementation for put, putIfAbsent
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
          Returns a Set view of the mappings contained in this map.
 boolean equals(java.lang.Object o)
          Compares the specified object with this map for equality.
(package private)  CustomConcurrentHashMap.Node findNode(java.lang.Object key, int hash, CustomConcurrentHashMap.Segment seg)
          Returns node for key, or null if none
 V get(java.lang.Object key)
          Returns the value associated with a key equivalent to the given key with respect to this map's key Equivalence, or null if no such mapping exists.
(package private) static java.lang.ref.ReferenceQueue<java.lang.Object> getReclamationQueue()
          Returns a queue that may be used as the ReferenceQueue argument to Reference constructors to arrange removal of reclaimed nodes from maps via a background thread.
(package private)  CustomConcurrentHashMap.Segment getSegmentForAdd(int hash)
          Returns the segment for possibly inserting into the table associated with given hash, constructing it if necessary.
(package private)  CustomConcurrentHashMap.Segment getSegmentForTraversal(int hash)
          Returns the segment for traversing table for key with given hash
private static sun.misc.Unsafe getUnsafe()
           
private static sun.misc.Unsafe getUnsafePrivileged()
           
 int hashCode()
          Returns the sum of the hash codes of each entry in this map's entrySet() view, which in turn are the hash codes computed using key and value Equivalences for this Map.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
(package private)  CustomConcurrentHashMap.KeyIterator keyIterator()
           
 java.util.Set<K> keySet()
          Returns a Set view of the keys contained in this map.
static CustomConcurrentHashMap<java.lang.Integer,java.lang.Integer> newIntKeyIntValueMap(int expectedSize)
          Returns a new map using Integer keys and values
static
<ValueType>
CustomConcurrentHashMap<java.lang.Integer,ValueType>
newIntKeyMap(CustomConcurrentHashMap.Strength valueStrength, CustomConcurrentHashMap.Equivalence<? super ValueType> valueEquivalence, int expectedSize)
          Returns a new map using Integer keys and the given value parameters
static
<KeyType> CustomConcurrentHashMap<KeyType,java.lang.Integer>
newIntValueMap(CustomConcurrentHashMap.Strength keyStrength, CustomConcurrentHashMap.Equivalence<? super KeyType> keyEquivalence, int expectedSize)
          Returns a new map using the given key parameters and Integer values
 V put(K key, V value)
          Maps the specified key to the specified value in this map.
 void putAll(java.util.Map<? extends K,? extends V> m)
          Copies all of the mappings from the specified map to this one.
 V putIfAbsent(K key, V value)
          
private  void readObject(java.io.ObjectInputStream s)
          Reconstitutes the instance from a stream (that is, deserializes it).
 V remove(java.lang.Object key)
          Removes the mapping for the specified key.
 boolean remove(java.lang.Object key, java.lang.Object value)
          
(package private)  void removeIfReclaimed(CustomConcurrentHashMap.Node r)
          Removes node if its key or value are null.
 V replace(K key, V value)
          
 boolean replace(K key, V oldValue, V newValue)
          
 int size()
          Returns the number of key-value mappings in this map.
(package private) static int spreadHash(int h)
          Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions.
(package private) static java.lang.ref.ReferenceQueue<java.lang.Object> startReclamation()
           
(package private) static void storeNode(CustomConcurrentHashMap.Node[] table, int i, CustomConcurrentHashMap.Node r)
           
(package private) static void storeSegment(CustomConcurrentHashMap.Segment[] segs, int i, CustomConcurrentHashMap.Segment s)
           
 java.util.Collection<V> values()
          Returns a Collection view of the values contained in this map.
private  void writeObject(java.io.ObjectOutputStream s)
          Saves the state of the instance to a stream (i.e., serializes it).
 
Methods inherited from class java.util.AbstractMap
clone, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

STRONG

public static final CustomConcurrentHashMap.Strength STRONG
The strength of ordinary references


WEAK

public static final CustomConcurrentHashMap.Strength WEAK
The strength of weak references


SOFT

public static final CustomConcurrentHashMap.Strength SOFT
The strength of soft references


SELF_STRING

private static final java.lang.String SELF_STRING
Config string for self-map (Set view) refs

See Also:
Constant Field Values

INT_STRING

private static final java.lang.String INT_STRING
Config string for int maps

See Also:
Constant Field Values

IDENTITY

public static final CustomConcurrentHashMap.Equivalence<java.lang.Object> IDENTITY
An Equivalence object performing identity-based comparisons and using System.identityHashCode(java.lang.Object) for hashing


EQUALS

public static final CustomConcurrentHashMap.Equivalence<java.lang.Object> EQUALS
An Equivalence object performing Object.equals(java.lang.Object) based comparisons and using Object.hashCode() hashing


SEGMENT_BITS

static final int SEGMENT_BITS
See Also:
Constant Field Values

NSEGMENTS

static final int NSEGMENTS
See Also:
Constant Field Values

SEGMENT_MASK

static final int SEGMENT_MASK
See Also:
Constant Field Values

SEGMENT_SHIFT

static final int SEGMENT_SHIFT
See Also:
Constant Field Values

MIN_SEGMENT_CAPACITY

static final int MIN_SEGMENT_CAPACITY
See Also:
Constant Field Values

MAX_SEGMENT_CAPACITY

static final int MAX_SEGMENT_CAPACITY
See Also:
Constant Field Values

segments

transient volatile CustomConcurrentHashMap.Segment[] segments
The segments, each of which acts as a hash table


factory

final CustomConcurrentHashMap.NodeFactory factory
The factory for this map


keyEquivalence

final CustomConcurrentHashMap.Equivalence<? super K> keyEquivalence
Equivalence object for keys


valueEquivalence

final CustomConcurrentHashMap.Equivalence<? super V> valueEquivalence
Equivalence object for values


initialSegmentCapacity

final int initialSegmentCapacity
The initial size of Segment tables when they are first constructed


keySet

transient java.util.Set<K> keySet

entrySet

transient java.util.Set<java.util.Map.Entry<K,V>> entrySet

values

transient java.util.Collection<V> values

refQueue

static volatile java.lang.ref.ReferenceQueue<java.lang.Object> refQueue

UNSAFE

static final sun.misc.Unsafe UNSAFE

tableBase

static final long tableBase

tableShift

static final int tableShift

segmentsBase

static final long segmentsBase

segmentsShift

static final int segmentsShift
Constructor Detail

CustomConcurrentHashMap

CustomConcurrentHashMap(java.lang.String ks,
                        CustomConcurrentHashMap.Equivalence<? super K> keq,
                        java.lang.String vs,
                        CustomConcurrentHashMap.Equivalence<? super V> veq,
                        int expectedSize)
Internal constructor to set factory, equivalences and segment capacities, and to create segments array.


CustomConcurrentHashMap

public CustomConcurrentHashMap(CustomConcurrentHashMap.Strength keyStrength,
                               CustomConcurrentHashMap.Equivalence<? super K> keyEquivalence,
                               CustomConcurrentHashMap.Strength valueStrength,
                               CustomConcurrentHashMap.Equivalence<? super V> valueEquivalence,
                               int expectedSize)
Creates a new CustomConcurrentHashMap with the given parameters

Parameters:
keyStrength - the strength for keys
keyEquivalence - the Equivalence to use for keys
valueStrength - the strength for values
valueEquivalence - the Equivalence to use for values
expectedSize - an estimate of the number of elements that will be held in the map. If no estimate is known, zero is an acceptable value.

CustomConcurrentHashMap

public CustomConcurrentHashMap()
Creates a new CustomConcurrentHashMap with strong keys and values, and equality-based equivalence.

Method Detail

spreadHash

static int spreadHash(int h)
Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions. This is critical because we use power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower or upper bits.


newIntKeyMap

public static <ValueType> CustomConcurrentHashMap<java.lang.Integer,ValueType> newIntKeyMap(CustomConcurrentHashMap.Strength valueStrength,
                                                                                            CustomConcurrentHashMap.Equivalence<? super ValueType> valueEquivalence,
                                                                                            int expectedSize)
Returns a new map using Integer keys and the given value parameters

Parameters:
valueStrength - the strength for values
valueEquivalence - the Equivalence to use for values
expectedSize - an estimate of the number of elements that will be held in the map. If no estimate is known, zero is an acceptable value.
Returns:
the map

newIntValueMap

public static <KeyType> CustomConcurrentHashMap<KeyType,java.lang.Integer> newIntValueMap(CustomConcurrentHashMap.Strength keyStrength,
                                                                                          CustomConcurrentHashMap.Equivalence<? super KeyType> keyEquivalence,
                                                                                          int expectedSize)
Returns a new map using the given key parameters and Integer values

Parameters:
keyStrength - the strength for keys
keyEquivalence - the Equivalence to use for keys
expectedSize - an estimate of the number of elements that will be held in the map. If no estimate is known, zero is an acceptable value.
Returns:
the map

newIntKeyIntValueMap

public static CustomConcurrentHashMap<java.lang.Integer,java.lang.Integer> newIntKeyIntValueMap(int expectedSize)
Returns a new map using Integer keys and values

Parameters:
expectedSize - an estimate of the number of elements that will be held in the map. If no estimate is known, zero is an acceptable value.
Returns:
the map

getSegmentForTraversal

final CustomConcurrentHashMap.Segment getSegmentForTraversal(int hash)
Returns the segment for traversing table for key with given hash

Parameters:
hash - the hash code for the key
Returns:
the segment, or null if not yet initialized

getSegmentForAdd

final CustomConcurrentHashMap.Segment getSegmentForAdd(int hash)
Returns the segment for possibly inserting into the table associated with given hash, constructing it if necessary.

Parameters:
hash - the hash code for the key
Returns:
the segment

findNode

final CustomConcurrentHashMap.Node findNode(java.lang.Object key,
                                            int hash,
                                            CustomConcurrentHashMap.Segment seg)
Returns node for key, or null if none


containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this map contains a key equivalent to the given key with respect to this map's key Equivalence.

Specified by:
containsKey in interface java.util.Map<K,V>
Overrides:
containsKey in class java.util.AbstractMap<K,V>
Parameters:
key - possible key
Returns:
true if this map contains the specified key
Throws:
java.lang.NullPointerException - if the specified key is null

get

public V get(java.lang.Object key)
Returns the value associated with a key equivalent to the given key with respect to this map's key Equivalence, or null if no such mapping exists.

Specified by:
get in interface java.util.Map<K,V>
Overrides:
get in class java.util.AbstractMap<K,V>
Parameters:
key - possible key
Returns:
the value associated with the key or null if there is no mapping.
Throws:
java.lang.NullPointerException - if the specified key is null

doPut

final V doPut(K key,
              V value,
              boolean onlyIfNull)
Shared implementation for put, putIfAbsent


put

public V put(K key,
             V value)
Maps the specified key to the specified value in this map.

Specified by:
put in interface java.util.Map<K,V>
Overrides:
put in class java.util.AbstractMap<K,V>
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key
Returns:
the previous value associated with key, or null if there was no mapping for key
Throws:
java.lang.NullPointerException - if the specified key or value is null

putIfAbsent

public V putIfAbsent(K key,
                     V value)

Specified by:
putIfAbsent in interface java.util.concurrent.ConcurrentMap<K,V>
Returns:
the previous value associated with the specified key, or null if there was no mapping for the key
Throws:
java.lang.NullPointerException - if the specified key or value is null

putAll

public void putAll(java.util.Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this one. These mappings replace any mappings that this map had for any of the keys currently in the specified map.

Specified by:
putAll in interface java.util.Map<K,V>
Overrides:
putAll in class java.util.AbstractMap<K,V>
Parameters:
m - mappings to be stored in this map

replace

public V replace(K key,
                 V value)

Specified by:
replace in interface java.util.concurrent.ConcurrentMap<K,V>
Throws:
java.lang.NullPointerException - if any of the arguments are null

replace

public boolean replace(K key,
                       V oldValue,
                       V newValue)

Specified by:
replace in interface java.util.concurrent.ConcurrentMap<K,V>
Returns:
the previous value associated with the specified key, or null if there was no mapping for the key
Throws:
java.lang.NullPointerException - if the specified key or value is null

remove

public V remove(java.lang.Object key)
Removes the mapping for the specified key.

Specified by:
remove in interface java.util.Map<K,V>
Overrides:
remove in class java.util.AbstractMap<K,V>
Parameters:
key - the key to remove
Returns:
the previous value associated with key, or null if there was no mapping for key
Throws:
java.lang.NullPointerException - if the specified key is null

remove

public boolean remove(java.lang.Object key,
                      java.lang.Object value)

Specified by:
remove in interface java.util.concurrent.ConcurrentMap<K,V>
Throws:
java.lang.NullPointerException - if the specified key is null

removeIfReclaimed

final void removeIfReclaimed(CustomConcurrentHashMap.Node r)
Removes node if its key or value are null.


isEmpty

public final boolean isEmpty()
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface java.util.Map<K,V>
Overrides:
isEmpty in class java.util.AbstractMap<K,V>
Returns:
true if this map contains no key-value mappings

size

public final int size()
Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface java.util.Map<K,V>
Overrides:
size in class java.util.AbstractMap<K,V>
Returns:
the number of key-value mappings in this map

containsValue

public final boolean containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to a value equivalent to the given value with respect to this map's value Equivalence. Note: This method requires a full internal traversal of the hash table, and so is much slower than method containsKey.

Specified by:
containsValue in interface java.util.Map<K,V>
Overrides:
containsValue in class java.util.AbstractMap<K,V>
Parameters:
value - value whose presence in this map is to be tested
Returns:
true if this map maps one or more keys to the specified value
Throws:
java.lang.NullPointerException - if the specified value is null

clear

public final void clear()
Removes all of the mappings from this map.

Specified by:
clear in interface java.util.Map<K,V>
Overrides:
clear in class java.util.AbstractMap<K,V>

computeIfAbsent

public V computeIfAbsent(K key,
                         CustomConcurrentHashMap.MappingFunction<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value, computes its value using the given mappingFunction, and if non-null, enters it into the map. This is equivalent to
   if (map.containsKey(key))
       return map.get(key);
   value = mappingFunction.map(key);
   if (value != null)
      return map.put(key, value);
   else
      return null;
 
except that the action is performed atomically. Some attempted operations on this map by other threads may be blocked while computation is in progress. Because this function is invoked within atomicity control, the computation should be short and simple. The most common usage is to construct a new object serving as an initial mapped value, or memoized result.

Parameters:
key - key with which the specified value is to be associated
mappingFunction - the function to compute a value
Returns:
the current (existing or computed) value associated with the specified key, or null if the computation returned null.
Throws:
java.lang.NullPointerException - if the specified key or mappingFunction is null,
java.lang.RuntimeException - or Error if the mappingFunction does so, in which case the mapping is left unestablished.

compute

public V compute(K key,
                 CustomConcurrentHashMap.RemappingFunction<? super K,V> remappingFunction)
Updates the mapping for the given key with the result of the given remappingFunction. This is equivalent to
   value = remappingFunction.remap(key, get(key));
   if (value != null)
     return put(key, value):
   else
     return remove(key);
 
except that the action is performed atomically. Some attempted operations on this map by other threads may be blocked while computation is in progress.

Sample Usage. A remapping function can be used to perform frequency counting of words using code such as:

 map.compute(word, new RemappingFunction<String,Integer>() {
   public Integer remap(String k, Integer v) {
     return (v == null) ? 1 : v + 1;
   }});
 

Parameters:
key - key with which the specified value is to be associated
remappingFunction - the function to compute a value
Returns:
the updated value or null if the computation returned null
Throws:
java.lang.NullPointerException - if the specified key or remappingFunction is null,
java.lang.RuntimeException - or Error if the remappingFunction does so, in which case the mapping is left in its previous state

keyIterator

final CustomConcurrentHashMap.KeyIterator keyIterator()

keySet

public java.util.Set<K> keySet()
Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

Specified by:
keySet in interface java.util.Map<K,V>
Overrides:
keySet in class java.util.AbstractMap<K,V>

values

public java.util.Collection<V> values()
Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

Specified by:
values in interface java.util.Map<K,V>
Overrides:
values in class java.util.AbstractMap<K,V>

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

Specified by:
entrySet in interface java.util.Map<K,V>
Specified by:
entrySet in class java.util.AbstractMap<K,V>

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this map for equality. Returns true if the given object is also a map of the same size, holding keys that are equal using this Map's key Equivalence, and which map to values that are equal according to this Map's value equivalence.

Specified by:
equals in interface java.util.Map<K,V>
Overrides:
equals in class java.util.AbstractMap<K,V>
Parameters:
o - object to be compared for equality with this map
Returns:
true if the specified object is equal to this map

hashCode

public int hashCode()
Returns the sum of the hash codes of each entry in this map's entrySet() view, which in turn are the hash codes computed using key and value Equivalences for this Map.

Specified by:
hashCode in interface java.util.Map<K,V>
Overrides:
hashCode in class java.util.AbstractMap<K,V>
Returns:
the hash code

writeObject

private void writeObject(java.io.ObjectOutputStream s)
                  throws java.io.IOException
Saves the state of the instance to a stream (i.e., serializes it).

Parameters:
s - the stream
Throws:
java.io.IOException

readObject

private void readObject(java.io.ObjectInputStream s)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Reconstitutes the instance from a stream (that is, deserializes it).

Parameters:
s - the stream
Throws:
java.io.IOException
java.lang.ClassNotFoundException

getReclamationQueue

static java.lang.ref.ReferenceQueue<java.lang.Object> getReclamationQueue()
Returns a queue that may be used as the ReferenceQueue argument to Reference constructors to arrange removal of reclaimed nodes from maps via a background thread.

Returns:
the reference queue associated with the background cleanup thread.

startReclamation

static java.lang.ref.ReferenceQueue<java.lang.Object> startReclamation()

getUnsafe

private static sun.misc.Unsafe getUnsafe()
                                  throws java.lang.Throwable
Throws:
java.lang.Throwable

getUnsafePrivileged

private static sun.misc.Unsafe getUnsafePrivileged()
                                            throws java.lang.NoSuchFieldException,
                                                   java.lang.IllegalAccessException
Throws:
java.lang.NoSuchFieldException
java.lang.IllegalAccessException

storeNode

static final void storeNode(CustomConcurrentHashMap.Node[] table,
                            int i,
                            CustomConcurrentHashMap.Node r)

storeSegment

static final void storeSegment(CustomConcurrentHashMap.Segment[] segs,
                               int i,
                               CustomConcurrentHashMap.Segment s)

Copyright © 2008–2012 Václav Pech. All Rights Reserved.