Class AtomicLongMap<K>
- java.lang.Object
-
- com.google.common.util.concurrent.AtomicLongMap<K>
-
- All Implemented Interfaces:
java.io.Serializable
public final class AtomicLongMap<K> extends java.lang.Object implements java.io.SerializableA map containinglongvalues that can be atomically updated. While writes to a traditionalMaprely onput(K, V), the typical mechanism for writing to this map isaddAndGet(K, long), which adds alongto the value currently associated withK. 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, andtoString().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
Multisetsuch asConcurrentHashMultisetinstead.Warning: Unlike
Multiset, entries whose values are zero are not automatically removed from the map. Instead they must be removed manually withremoveAllZeros().- Since:
- 11.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description privateAtomicLongMap(java.util.concurrent.ConcurrentHashMap<K,java.lang.Long> map)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description longaccumulateAndGet(K key, long x, java.util.function.LongBinaryOperator accumulatorFunction)Updates the value currently associated withkeyby combining it withxvia the specified accumulator function, returning the new value.longaddAndGet(K key, long delta)Addsdeltato the value currently associated withkey, and returns the new value.java.util.Map<K,java.lang.Long>asMap()Returns a live, read-only view of the map backing thisAtomicLongMap.voidclear()Removes all of the mappings from this map.booleancontainsKey(java.lang.Object key)Returns true if this map contains a mapping for the specified key.static <K> AtomicLongMap<K>create()Creates anAtomicLongMap.static <K> AtomicLongMap<K>create(java.util.Map<? extends K,? extends java.lang.Long> m)Creates anAtomicLongMapwith the same mappings as the specifiedMap.private java.util.Map<K,java.lang.Long>createAsMap()longdecrementAndGet(K key)Decrements by one the value currently associated withkey, and returns the new value.longget(K key)Returns the value associated withkey, or zero if there is no value associated withkey.longgetAndAccumulate(K key, long x, java.util.function.LongBinaryOperator accumulatorFunction)Updates the value currently associated withkeyby combining it withxvia the specified accumulator function, returning the old value.longgetAndAdd(K key, long delta)Addsdeltato the value currently associated withkey, and returns the old value.longgetAndDecrement(K key)Decrements by one the value currently associated withkey, and returns the old value.longgetAndIncrement(K key)Increments by one the value currently associated withkey, and returns the old value.longgetAndUpdate(K key, java.util.function.LongUnaryOperator updaterFunction)Updates the value currently associated withkeywith the specified function, and returns the old value.longincrementAndGet(K key)Increments by one the value currently associated withkey, and returns the new value.booleanisEmpty()Returnstrueif this map contains no key-value mappings.longput(K key, long newValue)AssociatesnewValuewithkeyin this map, and returns the value previously associated withkey, or zero if there was no such value.voidputAll(java.util.Map<? extends K,? extends java.lang.Long> m)Copies all of the mappings from the specified map to this map.(package private) longputIfAbsent(K key, long newValue)Ifkeyis not already associated with a value or ifkeyis associated with zero, associate it withnewValue.longremove(K key)Removes and returns the value associated withkey.(package private) booleanremove(K key, long value)If(key, value)is currently in the map, this method removes it and returns true; otherwise, this method returns false.voidremoveAllZeros()Removes all mappings from this map whose values are zero.booleanremoveIfZero(K key)Atomically removekeyfrom the map iff its associated value is 0.(package private) booleanreplace(K key, long expectedOldValue, long newValue)If(key, expectedOldValue)is currently in the map, this method replacesexpectedOldValuewithnewValueand returns true; otherwise, this method returns false.intsize()Returns the number of key-value mappings in this map.longsum()Returns the sum of all values in this map.java.lang.StringtoString()longupdateAndGet(K key, java.util.function.LongUnaryOperator updaterFunction)Updates the value currently associated withkeywith the specified function, and returns the new value.
-
-
-
Constructor Detail
-
AtomicLongMap
private AtomicLongMap(java.util.concurrent.ConcurrentHashMap<K,java.lang.Long> map)
-
-
Method Detail
-
create
public static <K> AtomicLongMap<K> create()
Creates anAtomicLongMap.
-
create
public static <K> AtomicLongMap<K> create(java.util.Map<? extends K,? extends java.lang.Long> m)
Creates anAtomicLongMapwith the same mappings as the specifiedMap.
-
get
public long get(K key)
Returns the value associated withkey, or zero if there is no value associated withkey.
-
incrementAndGet
public long incrementAndGet(K key)
Increments by one the value currently associated withkey, and returns the new value.
-
decrementAndGet
public long decrementAndGet(K key)
Decrements by one the value currently associated withkey, and returns the new value.
-
addAndGet
public long addAndGet(K key, long delta)
Addsdeltato the value currently associated withkey, and returns the new value.
-
getAndIncrement
public long getAndIncrement(K key)
Increments by one the value currently associated withkey, and returns the old value.
-
getAndDecrement
public long getAndDecrement(K key)
Decrements by one the value currently associated withkey, and returns the old value.
-
getAndAdd
public long getAndAdd(K key, long delta)
Addsdeltato the value currently associated withkey, and returns the old value.
-
updateAndGet
public long updateAndGet(K key, java.util.function.LongUnaryOperator updaterFunction)
Updates the value currently associated withkeywith the specified function, and returns the new value. If there is not currently a value associated withkey, the function is applied to0L.- Since:
- 21.0
-
getAndUpdate
public long getAndUpdate(K key, java.util.function.LongUnaryOperator updaterFunction)
Updates the value currently associated withkeywith the specified function, and returns the old value. If there is not currently a value associated withkey, the function is applied to0L.- Since:
- 21.0
-
accumulateAndGet
public long accumulateAndGet(K key, long x, java.util.function.LongBinaryOperator accumulatorFunction)
Updates the value currently associated withkeyby combining it withxvia the specified accumulator function, returning the new value. The previous value associated withkey(or zero, if there is none) is passed as the first argument toaccumulatorFunction, andxis passed as the second argument.- Since:
- 21.0
-
getAndAccumulate
public long getAndAccumulate(K key, long x, java.util.function.LongBinaryOperator accumulatorFunction)
Updates the value currently associated withkeyby combining it withxvia the specified accumulator function, returning the old value. The previous value associated withkey(or zero, if there is none) is passed as the first argument toaccumulatorFunction, andxis passed as the second argument.- Since:
- 21.0
-
put
public long put(K key, long newValue)
AssociatesnewValuewithkeyin this map, and returns the value previously associated withkey, or zero if there was no such value.
-
putAll
public void putAll(java.util.Map<? extends K,? extends java.lang.Long> m)
Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of callingput(k, v)on this map once for each mapping from keykto valuevin the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
-
remove
public long remove(K key)
Removes and returns the value associated withkey. Ifkeyis not in the map, this method has no effect and returns zero.
-
remove
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.
-
removeIfZero
public boolean removeIfZero(K key)
Atomically removekeyfrom the map iff its associated value is 0.- Since:
- 20.0
-
removeAllZeros
public void removeAllZeros()
Removes all mappings from this map whose values are zero.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.
-
sum
public long sum()
Returns the sum of all values in this map.This method is not atomic: the sum may or may not include other concurrent operations.
-
asMap
public java.util.Map<K,java.lang.Long> asMap()
Returns a live, read-only view of the map backing thisAtomicLongMap.
-
createAsMap
private java.util.Map<K,java.lang.Long> createAsMap()
-
containsKey
public boolean containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key.
-
size
public int size()
Returns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.
-
isEmpty
public boolean isEmpty()
Returnstrueif this map contains no key-value mappings.
-
clear
public void clear()
Removes all of the mappings from this map. The map will be empty after this call returns.This method is not atomic: the map may not be empty after returning if there were concurrent writes.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
putIfAbsent
long putIfAbsent(K key, long newValue)
Ifkeyis not already associated with a value or ifkeyis associated with zero, associate it withnewValue. Returns the previous value associated withkey, or zero if there was no mapping forkey.
-
replace
boolean replace(K key, long expectedOldValue, long newValue)
If(key, expectedOldValue)is currently in the map, this method replacesexpectedOldValuewithnewValueand returns true; otherwise, this method returns false.If
expectedOldValueis zero, this method will succeed if(key, zero)is currently in the map, or ifkeyis not in the map at all.
-
-