Package com.attivio.util
Class ValueCounter<K>
- java.lang.Object
-
- com.attivio.util.ValueCounter<K>
-
- Type Parameters:
K- the type of value to count
public class ValueCounter<K> extends java.lang.ObjectSimple class to map a key to a count. No space is taken in the map by zero-count items.This class is thread-safe.
-
-
Constructor Summary
Constructors Constructor Description ValueCounter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears the value countsintdecrement(K key)Decrement the count of a particular key.intgetCount(K key)Get the count for a given key or 0 if not present.KgetMaxValue()Gets the key that has been seen the most, if there is a tie, the key added first wins.intincrement(K key)Increment the count of a particular key.booleanisEmpty()intkeyCount()java.util.Set<K>keySet()This method is safe to call at any time.voidremove(K key)Removes the valueintsize()K[]toArray(K[] template)java.lang.StringtoString()intupdate(K key, int val)Update the count of a particularkeybyval.
-
-
-
Method Detail
-
increment
public int increment(K key)
Increment the count of a particular key. The key need not already exist.- Returns:
- the new value
-
decrement
public int decrement(K key)
Decrement the count of a particular key. If the count becomes 0, the key is removed.- Returns:
- the new value
-
update
public int update(K key, int val)
Update the count of a particularkeybyval. If the count becomes 0, the key is removed.- Parameters:
key-val-- Returns:
- the new value
-
remove
public void remove(K key)
Removes the value
-
getCount
public int getCount(K key)
Get the count for a given key or 0 if not present.
-
getMaxValue
public K getMaxValue()
Gets the key that has been seen the most, if there is a tie, the key added first wins. NOTE, this method does not reflect removal of a key from the map (if the maximum value key is removed, this method will still indicate it is the maximum).
-
isEmpty
public boolean isEmpty()
-
keyCount
public int keyCount()
- Returns:
- the number of keys with non-zero values
-
keySet
public java.util.Set<K> keySet()
This method is safe to call at any time. The returned value is a snapshot and copy of the the keys in place when called.- Returns:
- the set of keys which have non-zero values.
-
size
public int size()
- Returns:
- number of non-zero values
-
clear
public void clear()
Clears the value counts
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-