public final class AtomicLongMap<K>
extends java.lang.Object
implements java.io.Serializable
long
values that can be atomically updated. While writes to a
traditional Map
rely on put(K, V)
, the typical mechanism for writing to this map
is addAndGet(K, long)
, which adds a long
to the value currently associated with
K
. If a key has not yet been associated with a value, its implicit value is zero.
Most methods in this class treat absent values and zero values identically, as individually
documented. Exceptions to this are containsKey(java.lang.Object)
, size()
, isEmpty()
, asMap
, and toString()
.
Instances of this class may be used by multiple threads concurrently. All operations are atomic unless otherwise noted.
Note: If your values are always positive and less than 2^31, you may wish to use a
Multiset
such as ConcurrentHashMultiset
instead.
Warning: Unlike Multiset
, entries whose values are zero are not automatically
removed from the map. Instead they must be removed manually with removeAllZeros()
.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<K,java.lang.Long> |
asMap |
private java.util.concurrent.ConcurrentHashMap<K,java.lang.Long> |
map |
Modifier | Constructor and Description |
---|---|
private |
AtomicLongMap(java.util.concurrent.ConcurrentHashMap<K,java.lang.Long> map) |
Modifier and Type | Method and Description |
---|---|
long |
accumulateAndGet(K key,
long x,
java.util.function.LongBinaryOperator accumulatorFunction)
Updates the value currently associated with
key by combining it with x via the
specified accumulator function, returning the new value. |
long |
addAndGet(K key,
long delta)
Adds
delta to the value currently associated with key , and returns the new
value. |
java.util.Map<K,java.lang.Long> |
asMap()
Returns a live, read-only view of the map backing this
AtomicLongMap . |
void |
clear()
Removes all of the mappings from this map.
|
boolean |
containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key.
|
static <K> AtomicLongMap<K> |
create()
Creates an
AtomicLongMap . |
static <K> AtomicLongMap<K> |
create(java.util.Map<? extends K,? extends java.lang.Long> m)
Creates an
AtomicLongMap with the same mappings as the specified Map . |
private java.util.Map<K,java.lang.Long> |
createAsMap() |
long |
decrementAndGet(K key)
Decrements by one the value currently associated with
key , and returns the new value. |
long |
get(K key)
Returns the value associated with
key , or zero if there is no value associated with
key . |
long |
getAndAccumulate(K key,
long x,
java.util.function.LongBinaryOperator accumulatorFunction)
Updates the value currently associated with
key by combining it with x via the
specified accumulator function, returning the old value. |
long |
getAndAdd(K key,
long delta)
Adds
delta to the value currently associated with key , and returns the old
value. |
long |
getAndDecrement(K key)
Decrements by one the value currently associated with
key , and returns the old value. |
long |
getAndIncrement(K key)
Increments by one the value currently associated with
key , and returns the old value. |
long |
getAndUpdate(K key,
java.util.function.LongUnaryOperator updaterFunction)
Updates the value currently associated with
key with the specified function, and
returns the old value. |
long |
incrementAndGet(K key)
Increments by one the value currently associated with
key , and returns the new value. |
boolean |
isEmpty()
Returns
true if this map contains no key-value mappings. |
long |
put(K key,
long newValue)
Associates
newValue with key in this map, and returns the value previously
associated with key , or zero if there was no such value. |
void |
putAll(java.util.Map<? extends K,? extends java.lang.Long> m)
Copies all of the mappings from the specified map to this map.
|
(package private) long |
putIfAbsent(K key,
long newValue)
If
key is not already associated with a value or if key is associated with
zero, associate it with newValue . |
long |
remove(K key)
Removes and returns the value associated with
key . |
(package private) boolean |
remove(K key,
long value)
If
(key, value) is currently in the map, this method removes it and returns true;
otherwise, this method returns false. |
void |
removeAllZeros()
Removes all mappings from this map whose values are zero.
|
boolean |
removeIfZero(K key)
Atomically remove
key from the map iff its associated value is 0. |
(package private) boolean |
replace(K key,
long expectedOldValue,
long newValue)
If
(key, expectedOldValue) is currently in the map, this method replaces expectedOldValue with newValue and returns true; otherwise, this method returns false. |
int |
size()
Returns the number of key-value mappings in this map.
|
long |
sum()
Returns the sum of all values in this map.
|
java.lang.String |
toString() |
long |
updateAndGet(K key,
java.util.function.LongUnaryOperator updaterFunction)
Updates the value currently associated with
key with the specified function, and
returns the new value. |
private final java.util.concurrent.ConcurrentHashMap<K,java.lang.Long> map
@CheckForNull private transient java.util.Map<K,java.lang.Long> asMap
private AtomicLongMap(java.util.concurrent.ConcurrentHashMap<K,java.lang.Long> map)
public static <K> AtomicLongMap<K> create()
AtomicLongMap
.public static <K> AtomicLongMap<K> create(java.util.Map<? extends K,? extends java.lang.Long> m)
AtomicLongMap
with the same mappings as the specified Map
.public long get(K key)
key
, or zero if there is no value associated with
key
.public long incrementAndGet(K key)
key
, and returns the new value.public long decrementAndGet(K key)
key
, and returns the new value.public long addAndGet(K key, long delta)
delta
to the value currently associated with key
, and returns the new
value.public long getAndIncrement(K key)
key
, and returns the old value.public long getAndDecrement(K key)
key
, and returns the old value.public long getAndAdd(K key, long delta)
delta
to the value currently associated with key
, and returns the old
value.public long updateAndGet(K key, java.util.function.LongUnaryOperator updaterFunction)
key
with the specified function, and
returns the new value. If there is not currently a value associated with key
, the
function is applied to 0L
.public long getAndUpdate(K key, java.util.function.LongUnaryOperator updaterFunction)
key
with the specified function, and
returns the old value. If there is not currently a value associated with key
, the
function is applied to 0L
.public long accumulateAndGet(K key, long x, java.util.function.LongBinaryOperator accumulatorFunction)
key
by combining it with x
via the
specified accumulator function, returning the new value. The previous value associated with
key
(or zero, if there is none) is passed as the first argument to accumulatorFunction
, and x
is passed as the second argument.public long getAndAccumulate(K key, long x, java.util.function.LongBinaryOperator accumulatorFunction)
key
by combining it with x
via the
specified accumulator function, returning the old value. The previous value associated with
key
(or zero, if there is none) is passed as the first argument to accumulatorFunction
, and x
is passed as the second argument.public long put(K key, long newValue)
newValue
with key
in this map, and returns the value previously
associated with key
, or zero if there was no such value.public void putAll(java.util.Map<? extends K,? extends java.lang.Long> m)
put(k, v)
on this map once for each mapping from key
k
to value v
in the specified map. The behavior of this operation is undefined
if the specified map is modified while the operation is in progress.public long remove(K key)
key
. If key
is not in the map,
this method has no effect and returns zero.boolean remove(K key, long value)
(key, value)
is currently in the map, this method removes it and returns true;
otherwise, this method returns false.public boolean removeIfZero(K key)
key
from the map iff its associated value is 0.public void removeAllZeros()
This method is not atomic: the map may be visible in intermediate states, where some of the zero values have been removed and others have not.
public long sum()
This method is not atomic: the sum may or may not include other concurrent operations.
public java.util.Map<K,java.lang.Long> asMap()
AtomicLongMap
.private java.util.Map<K,java.lang.Long> createAsMap()
public boolean containsKey(java.lang.Object key)
public int size()
Integer.MAX_VALUE
elements, returns Integer.MAX_VALUE
.public boolean isEmpty()
true
if this map contains no key-value mappings.public void clear()
This method is not atomic: the map may not be empty after returning if there were concurrent writes.
public java.lang.String toString()
toString
in class java.lang.Object
long putIfAbsent(K key, long newValue)
key
is not already associated with a value or if key
is associated with
zero, associate it with newValue
. Returns the previous value associated with key
, or zero if there was no mapping for key
.boolean replace(K key, long expectedOldValue, long newValue)
(key, expectedOldValue)
is currently in the map, this method replaces expectedOldValue
with newValue
and returns true; otherwise, this method returns false.
If expectedOldValue
is zero, this method will succeed if (key, zero)
is
currently in the map, or if key
is not in the map at all.