|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractMap<K,V>
extra166y.CustomConcurrentHashMap<K,V>
K
- the type of keys maintained by this mapV
- the type of mapped valuespublic class CustomConcurrentHashMap<K,V>
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:
CustomConcurrentHashMap.Equivalence
-based comparisons controlling membership.
MappingFunctions
that may be
used in method computeIfAbsent(K, extra166y.CustomConcurrentHashMap.MappingFunction super K, ? extends V>)
to atomically
establish a computed value, along with
RemappingFunctions
that can be used in method
compute(K, extra166y.CustomConcurrentHashMap.RemappingFunction super K, V>)
to atomically
replace values.
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.
Nested Class Summary | |
---|---|
static interface |
CustomConcurrentHashMap.Equivalence<K>
An object performing equality comparisons, along with a hash function consistent with this comparison. |
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. |
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. |
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 |
static class |
CustomConcurrentHashMap.Strength
The strength of keys and values that may be held by maps. |
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 | |
---|---|
static CustomConcurrentHashMap.Equivalence<java.lang.Object> |
EQUALS
An Equivalence object performing Object.equals(java.lang.Object) based comparisons
and using Object.hashCode() hashing |
static CustomConcurrentHashMap.Equivalence<java.lang.Object> |
IDENTITY
An Equivalence object performing identity-based comparisons and using System.identityHashCode(java.lang.Object) for hashing |
static CustomConcurrentHashMap.Strength |
SOFT
The strength of soft references |
static CustomConcurrentHashMap.Strength |
STRONG
The strength of ordinary references |
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 |
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. |
|
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. |
|
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 |
|
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. |
|
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
|
newIntKeyMap(CustomConcurrentHashMap.Strength valueStrength,
CustomConcurrentHashMap.Equivalence<? super ValueType> valueEquivalence,
int expectedSize)
Returns a new map using Integer keys and the given value parameters |
|
static
|
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)
|
|
V |
remove(java.lang.Object key)
Removes the mapping for the specified key. |
|
boolean |
remove(java.lang.Object key,
java.lang.Object value)
|
|
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. |
|
java.util.Collection<V> |
values()
Returns a Collection view of the values contained in this map. |
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 |
---|
public static final CustomConcurrentHashMap.Strength STRONG
public static final CustomConcurrentHashMap.Strength WEAK
public static final CustomConcurrentHashMap.Strength SOFT
public static final CustomConcurrentHashMap.Equivalence<java.lang.Object> IDENTITY
System.identityHashCode(java.lang.Object)
for hashing
public static final CustomConcurrentHashMap.Equivalence<java.lang.Object> EQUALS
Object.equals(java.lang.Object)
based comparisons
and using Object.hashCode()
hashing
Constructor Detail |
---|
public CustomConcurrentHashMap(CustomConcurrentHashMap.Strength keyStrength, CustomConcurrentHashMap.Equivalence<? super K> keyEquivalence, CustomConcurrentHashMap.Strength valueStrength, CustomConcurrentHashMap.Equivalence<? super V> valueEquivalence, int expectedSize)
keyStrength
- the strength for keyskeyEquivalence
- the Equivalence to use for keysvalueStrength
- the strength for valuesvalueEquivalence
- the Equivalence to use for valuesexpectedSize
- an estimate of the number of elements
that will be held in the map. If no estimate is known,
zero is an acceptable value.public CustomConcurrentHashMap()
Method Detail |
---|
public static <ValueType> CustomConcurrentHashMap<java.lang.Integer,ValueType> newIntKeyMap(CustomConcurrentHashMap.Strength valueStrength, CustomConcurrentHashMap.Equivalence<? super ValueType> valueEquivalence, int expectedSize)
valueStrength
- the strength for valuesvalueEquivalence
- the Equivalence to use for valuesexpectedSize
- an estimate of the number of elements
that will be held in the map. If no estimate is known,
zero is an acceptable value.
public static <KeyType> CustomConcurrentHashMap<KeyType,java.lang.Integer> newIntValueMap(CustomConcurrentHashMap.Strength keyStrength, CustomConcurrentHashMap.Equivalence<? super KeyType> keyEquivalence, int expectedSize)
keyStrength
- the strength for keyskeyEquivalence
- the Equivalence to use for keysexpectedSize
- an estimate of the number of elements
that will be held in the map. If no estimate is known,
zero is an acceptable value.
public static CustomConcurrentHashMap<java.lang.Integer,java.lang.Integer> newIntKeyIntValueMap(int expectedSize)
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.
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map<K,V>
containsKey
in class java.util.AbstractMap<K,V>
key
- possible key
java.lang.NullPointerException
- if the specified key is nullpublic V get(java.lang.Object key)
null
if no such mapping exists
get
in interface java.util.Map<K,V>
get
in class java.util.AbstractMap<K,V>
key
- possible key
java.lang.NullPointerException
- if the specified key is nullpublic V put(K key, V value)
put
in interface java.util.Map<K,V>
put
in class java.util.AbstractMap<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key
java.lang.NullPointerException
- if the specified key or value is nullpublic V putIfAbsent(K key, V value)
putIfAbsent
in interface java.util.concurrent.ConcurrentMap<K,V>
java.lang.NullPointerException
- if the specified key or value is nullpublic void putAll(java.util.Map<? extends K,? extends V> m)
putAll
in interface java.util.Map<K,V>
putAll
in class java.util.AbstractMap<K,V>
m
- mappings to be stored in this mappublic V replace(K key, V value)
replace
in interface java.util.concurrent.ConcurrentMap<K,V>
java.lang.NullPointerException
- if any of the arguments are nullpublic boolean replace(K key, V oldValue, V newValue)
replace
in interface java.util.concurrent.ConcurrentMap<K,V>
java.lang.NullPointerException
- if the specified key or value is nullpublic V remove(java.lang.Object key)
remove
in interface java.util.Map<K,V>
remove
in class java.util.AbstractMap<K,V>
key
- the key to remove
java.lang.NullPointerException
- if the specified key is nullpublic boolean remove(java.lang.Object key, java.lang.Object value)
remove
in interface java.util.concurrent.ConcurrentMap<K,V>
java.lang.NullPointerException
- if the specified key is nullpublic final boolean isEmpty()
isEmpty
in interface java.util.Map<K,V>
isEmpty
in class java.util.AbstractMap<K,V>
public final int size()
size
in interface java.util.Map<K,V>
size
in class java.util.AbstractMap<K,V>
public final boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map<K,V>
containsValue
in class java.util.AbstractMap<K,V>
value
- value whose presence in this map is to be tested
java.lang.NullPointerException
- if the specified value is nullpublic final void clear()
clear
in interface java.util.Map<K,V>
clear
in class java.util.AbstractMap<K,V>
public V computeIfAbsent(K key, CustomConcurrentHashMap.MappingFunction<? super K,? extends V> mappingFunction)
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.
key
- key with which the specified value is to be associatedmappingFunction
- the function to compute a value
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.public V compute(K key, CustomConcurrentHashMap.RemappingFunction<? super K,V> remappingFunction)
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; }});
key
- key with which the specified value is to be associatedremappingFunction
- the function to compute a value
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 statepublic java.util.Set<K> keySet()
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.
keySet
in interface java.util.Map<K,V>
keySet
in class java.util.AbstractMap<K,V>
public java.util.Collection<V> values()
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.
values
in interface java.util.Map<K,V>
values
in class java.util.AbstractMap<K,V>
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
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.
entrySet
in interface java.util.Map<K,V>
entrySet
in class java.util.AbstractMap<K,V>
public boolean equals(java.lang.Object o)
equals
in interface java.util.Map<K,V>
equals
in class java.util.AbstractMap<K,V>
o
- object to be compared for equality with this map
public int hashCode()
hashCode
in interface java.util.Map<K,V>
hashCode
in class java.util.AbstractMap<K,V>
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |