Top Description Inners Fields Constructors Methods
com.sun.faces.util

public Class MultiKeyConcurrentHashMap<K, V>

extends Object
Class Inheritance
Imports
java.util.Collection, .Map, .Set, java.util.concurrent.locks.ReentrantLock

This code is based off the source for ConcurrentHashMap from JDK 5 with the ability of mapping multiple keys to a single value.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
MultiKeyConcurrentHashMap.HashEntry<K, V>

ConcurrentHashMap list entry.

pack-priv static class
MultiKeyConcurrentHashMap.Segment<K, V>

Segments are specialized versions of hash tables.

Field Summary

Modifier and TypeField and Description
pack-priv static int
DEFAULT_INITIAL_CAPACITY

The default initial number of table slots for this table.

pack-priv static final float
DEFAULT_LOAD_FACTOR

The default load factor for this table.

pack-priv static final int
DEFAULT_SEGMENTS

The default number of concurrency control segments.

pack-priv static final int
MAX_SEGMENTS

The maximum number of segments to allow; used to bound constructor arguments.

pack-priv static final int
MAXIMUM_CAPACITY

The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments.

pack-priv static final int
RETRIES_BEFORE_LOCK

Number of unsynchronized retries in size and containsValue methods before resorting to locking.

pack-priv final int
segmentMask

Mask value for indexing into segments.

pack-priv final MultiKeyConcurrentHashMap.Segment<K, V>[]
segments

The segments, each of which is a specialized hash table

pack-priv final int
segmentShift

Shift value for indexing within segments.

Constructor Summary

AccessConstructor and Description
public
MultiKeyConcurrentHashMap(int
the initial capacity. The implementation performs internal sizing to accommodate this many elements.
initialCapacity
,
float
the load factor threshold, used to control resizing. Resizing may be performed when the average number of elements per bin exceeds this threshold.
loadFactor
,
int
the estimated number of concurrently updating threads. The implementation performs internal sizing to try to accommodate this many threads.
concurrencyLevel
)

Creates a new, empty map with the specified initial capacity, load factor, and concurrency level.

public
MultiKeyConcurrentHashMap(int
the initial capacity. The implementation performs internal sizing to accommodate this many elements.
initialCapacity
)

Creates a new, empty map with the specified initial capacity, and with default load factor and concurrencyLevel.

public
MultiKeyConcurrentHashMap()

Creates a new, empty map with a default initial capacity, load factor, and concurrencyLevel.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Removes all mappings from this map.

public boolean

Returns:

true if and only if some key maps to the value argument in this table as determined by the equals method; false otherwise.
contains
(Object
a value to search for.
value
)

Legacy method testing if some key maps into the specified value in this table.

public boolean

Returns:

true if and only if the specified object is a key in this table, as determined by the equals method; false otherwise.
containsKey
(Object
possible key.
key
)

Tests if the specified object is a key in this table.

public boolean
public boolean
containsKey(Object key1, Object key2, Object key3)

public boolean
containsKey(Object key1, Object key2, Object key3, Object key4)

public boolean

Returns:

true if this map maps one or more keys to the specified value.
containsValue
(Object
value whose presence in this map is to be tested.
value
)

Returns true if this map maps one or more keys to the specified value.

public Set<Map.Entry<K, V>>
entrySet()

Unsupported.

public V

Returns:

the value to which the key is mapped in this table; null if the key is not mapped to any value in this table.
get
(Object
a key in the table.
key
)

Returns the value to which the specified key is mapped in this table.

public V
get(Object key1, Object key2)

public V
get(Object key1, Object key2, Object key3)

public V
get(Object key1, Object key2, Object key3, Object key4)

pack-priv static int

Returns:

the hash code
hash
(Object x1, Object x2, Object x3, Object x4)

Returns a hash code for non-null Objects.

public boolean
public Set<K>
keySet()

Unsupported

public V

Returns:

the previous value of the specified key in this table, or null if it did not have one.
put
(K
the table key.
key
,
V
the value.
value
)

Maps the specified key to the specified value in this table.

public V
put(K key1, K key2, V value)

public V
put(K key1, K key2, K key3, V value)

public V
put(K key1, K key2, K key3, K key4, V value)

public V

Returns:

previous value associated with specified key, or null if there was no mapping for key.
putIfAbsent
(K
key with which the specified value is to be associated.
key
,
V
value to be associated with the specified key.
value
)

If the specified key is not already associated with a value, associate it with the given value.

public V
putIfAbsent(K key1, K key2, V value)

public V
putIfAbsent(K key1, K key2, K key3, V value)

public V
putIfAbsent(K key1, K key2, K key3, K key4, V value)

public V
remove(K key)

public V
remove(K key1, K key2)

public V
remove(K key1, K key2, K key3)

public V
remove(K key1, K key2, K key3, K key4)

public boolean

Returns:

true if the value was replaced
replace
(K
key with which the specified value is associated.
key
,
V
value expected to be associated with the specified key.
oldValue
,
V
value to be associated with the specified key.
newValue
)

Replace entry for key only if currently mapped to given value.

public V

Returns:

previous value associated with specified key, or null if there was no mapping for key.
replace
(K
key with which the specified value is associated.
key
,
V
value to be associated with the specified key.
value
)

Replace entry for key only if currently mapped to some value.

pack-priv final MultiKeyConcurrentHashMap.Segment<K, V>

Returns:

the segment
segmentFor
(int
the hash code for the key
hash
)

Returns the segment that should be used for key with given hash

public int
size()

public Collection<V>
values()

Unsupported.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_INITIAL_CAPACITYback to summary
pack-priv static int DEFAULT_INITIAL_CAPACITY

The default initial number of table slots for this table. Used when not otherwise specified in constructor.

DEFAULT_LOAD_FACTORback to summary
pack-priv static final float DEFAULT_LOAD_FACTOR

The default load factor for this table. Used when not otherwise specified in constructor.

DEFAULT_SEGMENTSback to summary
pack-priv static final int DEFAULT_SEGMENTS

The default number of concurrency control segments.

MAX_SEGMENTSback to summary
pack-priv static final int MAX_SEGMENTS

The maximum number of segments to allow; used to bound constructor arguments.

MAXIMUM_CAPACITYback to summary
pack-priv static final int MAXIMUM_CAPACITY

The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments. MUST be a power of two <= 1<<30 to ensure that entries are indexible using ints.

RETRIES_BEFORE_LOCKback to summary
pack-priv static final int RETRIES_BEFORE_LOCK

Number of unsynchronized retries in size and containsValue methods before resorting to locking. This is used to avoid unbounded retries if tables undergo continuous modification which would make it impossible to obtain an accurate result.

segmentMaskback to summary
pack-priv final int segmentMask

Mask value for indexing into segments. The upper bits of a key's hash code are used to choose the segment.

segmentsback to summary
pack-priv final MultiKeyConcurrentHashMap.Segment<K, V>[] segments

The segments, each of which is a specialized hash table

segmentShiftback to summary
pack-priv final int segmentShift

Shift value for indexing within segments.

Constructor Detail

MultiKeyConcurrentHashMapback to summary
public MultiKeyConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)

Creates a new, empty map with the specified initial capacity, load factor, and concurrency level.

Parameters
initialCapacity:int

the initial capacity. The implementation performs internal sizing to accommodate this many elements.

loadFactor:float

the load factor threshold, used to control resizing. Resizing may be performed when the average number of elements per bin exceeds this threshold.

concurrencyLevel:int

the estimated number of concurrently updating threads. The implementation performs internal sizing to try to accommodate this many threads.

Exceptions
IllegalArgumentException:
if the initial capacity is negative or the load factor or concurrencyLevel are nonpositive.
MultiKeyConcurrentHashMapback to summary
public MultiKeyConcurrentHashMap(int initialCapacity)

Creates a new, empty map with the specified initial capacity, and with default load factor and concurrencyLevel.

Parameters
initialCapacity:int

the initial capacity. The implementation performs internal sizing to accommodate this many elements.

Exceptions
IllegalArgumentException:
if the initial capacity of elements is negative.
MultiKeyConcurrentHashMapback to summary
public MultiKeyConcurrentHashMap()

Creates a new, empty map with a default initial capacity, load factor, and concurrencyLevel.

Method Detail

clearback to summary
public void clear()

Removes all mappings from this map.

containsback to summary
public boolean contains(Object value)

Legacy method testing if some key maps into the specified value in this table. This method is identical in functionality to containsValue, and exists solely to ensure full compatibility with class java.util.Hashtable, which supported this method prior to introduction of the Java Collections framework.

Parameters
value:Object

a value to search for.

Returns:boolean

true if and only if some key maps to the value argument in this table as determined by the equals method; false otherwise.

Exceptions
NullPointerException:
if the value is null.
containsKeyback to summary
public boolean containsKey(Object key)

Tests if the specified object is a key in this table.

Parameters
key:Object

possible key.

Returns:boolean

true if and only if the specified object is a key in this table, as determined by the equals method; false otherwise.

Exceptions
NullPointerException:
if the key is null.
containsKeyback to summary
public boolean containsKey(Object key1, Object key2)
See Also
containsKey(Object)
containsKeyback to summary
public boolean containsKey(Object key1, Object key2, Object key3)
See Also
containsKey(Object)
containsKeyback to summary
public boolean containsKey(Object key1, Object key2, Object key3, Object key4)
See Also
containsKey(Object)
containsValueback to summary
public boolean containsValue(Object value)

Returns true if this map maps one or more keys to the specified value.

Note

This method requires a full internal traversal of the hash table, and so is much slower than method containsKey.

Parameters
value:Object

value whose presence in this map is to be tested.

Returns:boolean

true if this map maps one or more keys to the specified value.

Exceptions
NullPointerException:
if the value is null.
entrySetback to summary
public Set<Map.Entry<K, V>> entrySet()

Unsupported.

getback to summary
public V get(Object key)

Returns the value to which the specified key is mapped in this table.

Parameters
key:Object

a key in the table.

Returns:V

the value to which the key is mapped in this table; null if the key is not mapped to any value in this table.

Exceptions
NullPointerException:
if the key is null.
getback to summary
public V get(Object key1, Object key2)
See Also
get(Object)
getback to summary
public V get(Object key1, Object key2, Object key3)
See Also
get(Object)
getback to summary
public V get(Object key1, Object key2, Object key3, Object key4)
See Also
get(Object)
hashback to summary
pack-priv static int hash(Object x1, Object x2, Object x3, Object x4)

Returns a hash code for non-null Objects. Uses the same hash code spreader as most other java.util hash tables.

Returns:int

the hash code

isEmptyback to summary
public boolean isEmpty()
See Also
java.util.Map#isEmpty()
keySetback to summary
public Set<K> keySet()

Unsupported

putback to summary
public V put(K key, V value)

Maps the specified key to the specified value in this table. Neither the key nor the value can be null.

The value can be retrieved by calling the get method with a key that is equal to the original key.

Parameters
key:K

the table key.

value:V

the value.

Returns:V

the previous value of the specified key in this table, or null if it did not have one.

Exceptions
NullPointerException:
if the key or value is null.
putback to summary
public V put(K key1, K key2, V value)
See Also
put(Object, Object)
putback to summary
public V put(K key1, K key2, K key3, V value)
See Also
put(Object, Object)
putback to summary
public V put(K key1, K key2, K key3, K key4, V value)
See Also
put(Object, Object)
putIfAbsentback to summary
public V putIfAbsent(K key, V value)

If the specified key is not already associated with a value, associate it with the given value. This is equivalent to

if (!map.containsKey(key))
    return map.put(key, value);
else
    return map.get(key);
Except that the action is performed atomically.
Parameters
key:K

key with which the specified value is to be associated.

value:V

value to be associated with the specified key.

Returns:V

previous value associated with specified key, or null if there was no mapping for key.

Exceptions
NullPointerException:
if the specified key or value is null.
putIfAbsentback to summary
public V putIfAbsent(K key1, K key2, V value)
See Also
putIfAbsent(Object, Object)
putIfAbsentback to summary
public V putIfAbsent(K key1, K key2, K key3, V value)
See Also
putIfAbsent(Object, Object)
putIfAbsentback to summary
public V putIfAbsent(K key1, K key2, K key3, K key4, V value)
See Also
putIfAbsent(Object, Object)
removeback to summary
public V remove(K key)
See Also
Map#remove(Object)
removeback to summary
public V remove(K key1, K key2)
See Also
Map#remove(Object)
removeback to summary
public V remove(K key1, K key2, K key3)
See Also
Map#remove(Object)
removeback to summary
public V remove(K key1, K key2, K key3, K key4)
See Also
Map#remove(Object)
replaceback to summary
public boolean replace(K key, V oldValue, V newValue)

Replace entry for key only if currently mapped to given value. Acts as

if (map.get(key).equals(oldValue)) {
    map.put(key, newValue);
    return true;
} else
    return false;
except that the action is performed atomically.
Parameters
key:K

key with which the specified value is associated.

oldValue:V

value expected to be associated with the specified key.

newValue:V

value to be associated with the specified key.

Returns:boolean

true if the value was replaced

Exceptions
NullPointerException:
if the specified key or values are null.
replaceback to summary
public V replace(K key, V value)

Replace entry for key only if currently mapped to some value. Acts as

 if ((map.containsKey(key)) {
    return map.put(key, value);
} else return null;
except that the action is performed atomically.
Parameters
key:K

key with which the specified value is associated.

value:V

value to be associated with the specified key.

Returns:V

previous value associated with specified key, or null if there was no mapping for key.

Exceptions
NullPointerException:
if the specified key or value is null.
segmentForback to summary
pack-priv final MultiKeyConcurrentHashMap.Segment<K, V> segmentFor(int hash)

Returns the segment that should be used for key with given hash

Parameters
hash:int

the hash code for the key

Returns:MultiKeyConcurrentHashMap.Segment<K, V>

the segment

sizeback to summary
public int size()
See Also
java.util.Map#size()
valuesback to summary
public Collection<V> values()

Unsupported.

com.sun.faces.util back to summary

pack-priv final Class MultiKeyConcurrentHashMap.HashEntry<K, V>

extends Object
Class Inheritance

ConcurrentHashMap list entry. Note that this is never exported out as a user-visible Map.Entry. Because the value field is volatile, not final, it is legal wrt the Java Memory Model for an unsynchronized reader to see null instead of initial value when read via a data race. Although a reordering leading to this is not likely to ever actually occur, the Segment.readValueUnderLock method is used as a backup in case a null (pre-initialized) value is ever seen in an unsynchronized access method.

Field Summary

Modifier and TypeField and Description
pack-priv final int
pack-priv final K
pack-priv final K
pack-priv final K
pack-priv final K
pack-priv final MultiKeyConcurrentHashMap.HashEntry<K, V>
pack-priv volatile V

Constructor Summary

AccessConstructor and Description
pack-priv
HashEntry(K key1, K key2, K key3, K key4, int hash, MultiKeyConcurrentHashMap.HashEntry<K, V> next, V value)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hashback to summary
pack-priv final int hash
key1back to summary
pack-priv final K key1
key2back to summary
pack-priv final K key2
key3back to summary
pack-priv final K key3
key4back to summary
pack-priv final K key4
nextback to summary
pack-priv final MultiKeyConcurrentHashMap.HashEntry<K, V> next
valueback to summary
pack-priv volatile V value

Constructor Detail

HashEntryback to summary
pack-priv HashEntry(K key1, K key2, K key3, K key4, int hash, MultiKeyConcurrentHashMap.HashEntry<K, V> next, V value)
com.sun.faces.util back to summary

pack-priv final Class MultiKeyConcurrentHashMap.Segment<K, V>

extends ReentrantLock
Class Inheritance

Segments are specialized versions of hash tables. This subclasses from ReentrantLock opportunistically, just to simplify some locking and avoid separate construction.

Field Summary

Modifier and TypeField and Description
pack-priv volatile int
count

The number of elements in this segment's region.

pack-priv final float
loadFactor

The load factor for the hash table.

pack-priv int
modCount

Number of updates that alter the size of the table.

private static final long
pack-priv volatile MultiKeyConcurrentHashMap.HashEntry<K, V>[]
table

The per-segment table.

pack-priv int
threshold

The table is rehashed when its size exceeds this threshold.

Constructor Summary

AccessConstructor and Description
pack-priv
Segment(int initialCapacity, float lf)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
pack-priv boolean
containsKey(Object key1, Object key2, Object key3, Object key4, int hash)

pack-priv boolean
pack-priv V
get(Object key1, Object key2, Object key3, Object key4, int hash)

pack-priv MultiKeyConcurrentHashMap.HashEntry<K, V>
getFirst(int hash)

Return properly casted first entry of bin for given hash

pack-priv V
put(K key1, K key2, K key3, K key4, int hash, V value, boolean onlyIfAbsent)

pack-priv V
readValueUnderLock(MultiKeyConcurrentHashMap.HashEntry<K, V> e)

Read value field of an entry under lock.

pack-priv void
pack-priv V
remove(Object key1, Object key2, Object key3, Object key4, int hash, Object value)

Remove; match on key only if value null, else match both.

pack-priv boolean
replace(K key1, K key2, K key3, K key4, int hash, V oldValue, V newValue)

pack-priv V
replace(K key1, K key2, K key3, K key4, int hash, V newValue)

pack-priv void
setTable(MultiKeyConcurrentHashMap.HashEntry<K, V>[] newTable)

Set table to new HashEntry array.

Inherited from java.util.concurrent.locks.ReentrantLock:
getHoldCountgetOwnergetQueuedThreadsgetQueueLengthgetWaitingThreadsgetWaitQueueLengthhasQueuedThreadhasQueuedThreadshasWaitersisFairisHeldByCurrentThreadisLockedlocklockInterruptiblynewConditiontoStringtryLocktryLockunlock

Field Detail

countback to summary
pack-priv volatile int count

The number of elements in this segment's region.

loadFactorback to summary
pack-priv final float loadFactor

The load factor for the hash table. Even though this value is same for all segments, it is replicated to avoid needing links to outer object.

modCountback to summary
pack-priv int modCount

Number of updates that alter the size of the table. This is used during bulk-read methods to make sure they see a consistent snapshot: If modCounts change during a traversal of segments computing size or checking containsValue, then we might have an inconsistent view of state so (usually) must retry.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.locks.ReentrantLock.serialVersionUID.

tableback to summary
pack-priv volatile MultiKeyConcurrentHashMap.HashEntry<K, V>[] table

The per-segment table. Declared as a raw type, casted to HashEntry on each use.

Annotations
@SuppressWarnings:NonSerializableFieldInSerializableClass
thresholdback to summary
pack-priv int threshold

The table is rehashed when its size exceeds this threshold. (The value of this field is always (int)(capacity * loadFactor).)

Constructor Detail

Segmentback to summary
pack-priv Segment(int initialCapacity, float lf)

Method Detail

clearback to summary
pack-priv void clear()
containsKeyback to summary
pack-priv boolean containsKey(Object key1, Object key2, Object key3, Object key4, int hash)
containsValueback to summary
pack-priv boolean containsValue(Object value)
getback to summary
pack-priv V get(Object key1, Object key2, Object key3, Object key4, int hash)
getFirstback to summary
pack-priv MultiKeyConcurrentHashMap.HashEntry<K, V> getFirst(int hash)

Return properly casted first entry of bin for given hash

putback to summary
pack-priv V put(K key1, K key2, K key3, K key4, int hash, V value, boolean onlyIfAbsent)
readValueUnderLockback to summary
pack-priv V readValueUnderLock(MultiKeyConcurrentHashMap.HashEntry<K, V> e)

Read value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur.

rehashback to summary
pack-priv void rehash()
removeback to summary
pack-priv V remove(Object key1, Object key2, Object key3, Object key4, int hash, Object value)

Remove; match on key only if value null, else match both.

replaceback to summary
pack-priv boolean replace(K key1, K key2, K key3, K key4, int hash, V oldValue, V newValue)
replaceback to summary
pack-priv V replace(K key1, K key2, K key3, K key4, int hash, V newValue)
setTableback to summary
pack-priv void setTable(MultiKeyConcurrentHashMap.HashEntry<K, V>[] newTable)

Set table to new HashEntry array. Call only while holding lock or in constructor.