com.twitter.common.stats
Class CounterMap<K>

java.lang.Object
  extended by com.twitter.common.stats.CounterMap<K>
All Implemented Interfaces:
Cloneable, Iterable<Map.Entry<K,Integer>>
Direct Known Subclasses:
CounterMapWithTopKey

public class CounterMap<K>
extends Object
implements Iterable<Map.Entry<K,Integer>>, Cloneable

A map from a key type to integers. This simplifies the process of storing counters for multiple values of the same type.


Constructor Summary
CounterMap()
           
 
Method Summary
 CounterMap<K> clone()
           
 int get(K key)
          Gets the value associated with a key.
 int incrementAndGet(K key)
          Increments the counter value associated with key, and returns the new value.
 int incrementAndGet(K key, int count)
          Increments the value associated with key by value, returning the new value.
 Iterator<Map.Entry<K,Integer>> iterator()
          Gets an iterator for the mapped values.
 Set<K> keySet()
           
 void reset(K key)
          Resets the value for key.
 void set(K key, int newValue)
          Assigns a value to a key.
 int size()
          Gets the number of entries stored in the map.
 Map<K,Integer> toMap()
           
 String toString()
           
 Collection<Integer> values()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CounterMap

public CounterMap()
Method Detail

incrementAndGet

public int incrementAndGet(K key)
Increments the counter value associated with key, and returns the new value.

Parameters:
key - The key to increment
Returns:
The incremented value.

incrementAndGet

public int incrementAndGet(K key,
                           int count)
Increments the value associated with key by value, returning the new value.

Parameters:
key - The key to increment
Returns:
The incremented value.

get

public int get(K key)
Gets the value associated with a key.

Parameters:
key - The key to look up.
Returns:
The counter value stored for key, or 0 if no mapping exists.

set

public void set(K key,
                int newValue)
Assigns a value to a key.

Parameters:
key - The key to assign a value to.
newValue - The value to assign.

reset

public void reset(K key)
Resets the value for key. This will remove the key from the counter.

Parameters:
key - The key to reset.

size

public int size()
Gets the number of entries stored in the map.

Returns:
The size of the map.

iterator

public Iterator<Map.Entry<K,Integer>> iterator()
Gets an iterator for the mapped values.

Specified by:
iterator in interface Iterable<Map.Entry<K,Integer>>
Returns:
Iterator for mapped values.

values

public Collection<Integer> values()

keySet

public Set<K> keySet()

toString

public String toString()
Overrides:
toString in class Object

toMap

public Map<K,Integer> toMap()

clone

public CounterMap<K> clone()
Overrides:
clone in class Object