public class Counters<K> extends Object implements Map<K,Long>, Iterable<Map.Entry<K,Long>>
Map
of thread-safe counters, identified by a user-defined key.
Typically used to aggregate information from a stream of data.
Counters are created explicitly by putting
a value, implicitly by
incrementing
a value that has not yet been set. Counters
may be set, retrieved, incremented, decremented, or removed. You can deal with
counters as primitive long
values, or as Long
objects.
Mmappings are stored in a
Values may not be ConcurrentHashMap
; behavior of iterators
and set retrieval methods reflect this fact. Note, however, that this class does
not implement ConcurrentMap, because the internal storage is
not conducive to implementing replace()
and similar methods (at
least not without adding lots of locks that aren't needed for the common case).
null
.
Constructor and Description |
---|
Counters() |
Modifier and Type | Method and Description |
---|---|
void |
clear() |
boolean |
containsKey(Object key)
Returns
true if the underlying map contains the specified key. |
boolean |
containsValue(Object value)
Returns
true if the underlying map contains the specified value. |
long |
decrement(K key)
Decrements the specified counter, creating it if necessary.
|
Set<Map.Entry<K,Long>> |
entrySet()
Returns the current mappings.
|
Long |
get(Object key)
Returns the value associated with the specified key,
null if
there is not a mapping for the key. |
int |
getInt(K key)
Retrieves the value of the specified key as a primitive int.
|
long |
getLong(K key)
Retrieves the value of the specified key as a primitive long.
|
long |
increment(K key)
Increments the specified counter, creating it if necessary.
|
boolean |
isEmpty()
Returns
true if the underlying map is empty. |
Iterator<Map.Entry<K,Long>> |
iterator()
Returns an iterator for the entries in the map.
|
Set<K> |
keySet()
Returns the current keys in the map.
|
Long |
put(K key,
Long newValue)
Stores a new mapping, returning the previous mapping.
|
void |
putAll(Map<? extends K,? extends Long> map)
Initializes this object from the passed map.
|
Long |
putIfAbsent(K key,
Long value)
Adds a mapping for the value, if one does not already exist.
|
void |
putLong(K key,
long value)
Sets the mapping to the specified value.
|
Long |
remove(Object key)
Removes a mapping and returns it.
|
int |
size()
Returns the number of mappings (distinct keys) in this object.
|
String |
toString()
Outputs all counters in the format "[ NAME: VALUE, ...]".
|
Collection<Long> |
values()
Returns the current values in the map.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, remove, replace, replace, replaceAll
forEach, spliterator
public String toString()
public int size()
public boolean isEmpty()
true
if the underlying map is empty.public boolean containsKey(Object key)
true
if the underlying map contains the specified key.containsKey
in interface Map<K,Long>
public boolean containsValue(Object value)
true
if the underlying map contains the specified value.
The passed value must be a Number
; its longValue
is
tested against the values in the map.
As this operation iterates the map, it represents a point in time, and may not reflect concurrent updates (of course, why anyone would test for a value at the same time they're updating the map is a mystery to me).
containsValue
in interface Map<K,Long>
public Long get(Object key)
null
if
there is not a mapping for the key. Note that this behavior differs from
getLong(K)
, which returns 0 if there is no mapping for the key.public Long put(K key, Long newValue)
This method does not attempt to prevent multiple threads from updating the
map at the same time; concurrent calls could return the same old value. If
this is a problem for you, call putIfAbsent(K, java.lang.Long)
.
public Long remove(Object key)
public void putAll(Map<? extends K,? extends Long> map)
public Set<K> keySet()
public Collection<Long> values()
Long
, so is relatively inefficient. As it
uses the underlying map iterator to work, the returned collection may not reflect
concurrent updates.public Set<Map.Entry<K,Long>> entrySet()
iterator()
.public Long putIfAbsent(K key, Long value)
ConcurrentMap.putIfAbsent()
for behavior.putIfAbsent
in interface Map<K,Long>
public long getLong(K key)
public int getInt(K key)
public void putLong(K key, long value)
put(K, java.lang.Long)
, with the same caveats regarding concurent access.public long increment(K key)
public long decrement(K key)