public class IntCounter<E> extends AbstractCounter<E> implements java.io.Serializable
addAll
method can be used to copy another Counter's contents
over. This class also provides access
to Comparators that can be used to sort the keys or entries of this Counter
by the counts, in either ascending or descending order.Constructor and Description |
---|
IntCounter()
Constructs a new (empty) Counter.
|
IntCounter(IntCounter<E> c)
Constructs a new Counter with the contents of the given Counter.
|
IntCounter(MapFactory<E,MutableInteger> mapFactory)
Pass in a MapFactory and the map it vends will back your counter.
|
Modifier and Type | Method and Description |
---|---|
void |
addAll(IntCounter<E> counter)
Adds the counts in the given Counter to the counts in this Counter.
|
E |
argmax()
Finds and returns the key in this Counter with the largest count.
|
E |
argmax(java.util.Comparator<E> tieBreaker)
Finds and returns the key in this Counter with the largest count.
|
E |
argmin()
Finds and returns the key in this Counter with the smallest count.
|
E |
argmin(java.util.Comparator<E> tieBreaker)
Finds and returns the key in this Counter with the smallest count.
|
double |
averageCount()
Returns the mean of all the counts (totalCount/size).
|
void |
clear()
Removes all counts from this Counter.
|
java.lang.Object |
clone() |
boolean |
containsKey(E key)
Returns whether a Counter contains a key.
|
double |
decrementCount(E key)
Subtracts 1 from the count for the given key.
|
int |
decrementCount(E key,
int count)
Subtracts the given count from the current count for the given key.
|
void |
decrementCounts(java.util.Collection<E> keys)
Subtracts 1 from the counts of each of the given keys.
|
void |
decrementCounts(java.util.Collection<E> keys,
int count)
Subtracts the given count from the current counts for each of the given keys.
|
double |
defaultReturnValue()
Returns the default return value.
|
double |
doubleMax() |
java.util.Set<java.util.Map.Entry<E,java.lang.Double>> |
entrySet()
Returns a view of the doubles in this map.
|
boolean |
equals(java.lang.Object o) |
double |
getCount(java.lang.Object key)
Returns the current count for the given key, which is 0 if it hasn't
been
seen before.
|
java.lang.String |
getCountAsString(E key) |
Factory<Counter<E>> |
getFactory()
Returns a factory that can create new instances of this kind of Counter.
|
int |
getIntCount(java.lang.Object key)
Returns the current count for the given key, which is 0 if it hasn't
been
seen before.
|
MapFactory<E,MutableInteger> |
getMapFactory() |
double |
getNormalizedCount(E key)
This has been de-deprecated in order to reduce compilation warnings, but
really you should create a
Distribution instead of using this method. |
int |
hashCode() |
double |
incrementCount(E key)
Adds 1 to the count for the given key.
|
double |
incrementCount(E key,
double value)
Increments the count for the given key by the given value.
|
int |
incrementCount(E key,
int count)
Adds the given count to the current count for the given key.
|
void |
incrementCounts(java.util.Collection<E> keys)
Adds 1 to the counts for each of the given keys.
|
void |
incrementCounts(java.util.Collection<E> keys,
int count)
Adds the given count to the current counts for each of the given keys.
|
boolean |
isEmpty() |
java.util.Iterator<E> |
iterator() |
java.util.Set<E> |
keysAbove(int countThreshold)
Returns the set of keys whose counts are at or above the given threshold.
|
java.util.Set<E> |
keysAt(int count)
Returns the set of keys that have exactly the given count.
|
java.util.Set<E> |
keysBelow(int countThreshold)
Returns the set of keys whose counts are at or below the given threshold.
|
java.util.Set<E> |
keySet()
Returns the Set of keys in this counter.
|
int |
max()
Finds and returns the largest count in this Counter.
|
int |
min()
Finds and returns the smallest count in this Counter.
|
void |
prettyLog(Redwood.RedwoodChannels channels,
java.lang.String description)
Pretty logs the current object to specific Redwood channels.
|
double |
remove(E key)
Removes the given key from this Counter.
|
void |
removeAll(java.util.Collection<E> c)
Removes all the given keys from this Counter.
|
void |
removeZeroCounts()
Removes all keys whose count is 0.
|
void |
setCount(E key,
double value)
Sets the count for the given key to be the given value.
|
void |
setCount(E key,
int count)
Sets the current count for the given key.
|
void |
setCount(E key,
java.lang.String s) |
void |
setCounts(java.util.Collection<E> keys,
int count)
Sets the current count for each of the given keys.
|
void |
setDefaultReturnValue(double rv)
Sets the default return value.
|
void |
setDefaultReturnValue(int rv) |
int |
size()
Returns the number of entries stored in this counter.
|
void |
subtractAll(IntCounter<E> counter)
Subtracts the counts in the given Counter from the counts in this Counter.
|
java.lang.String |
toString() |
java.lang.String |
toString(java.text.NumberFormat nf) |
java.lang.String |
toString(java.text.NumberFormat nf,
java.lang.String preAppend,
java.lang.String postAppend,
java.lang.String keyValSeparator,
java.lang.String itemSeparator) |
double |
totalCount()
Computes the total of all counts in this counter, and returns it
as a double.
|
double |
totalCount(java.util.function.Predicate<E> filter) |
double |
totalDoubleCount() |
double |
totalDoubleCount(java.util.function.Predicate<E> filter) |
int |
totalIntCount()
Returns the current total count for all objects in this Counter.
|
int |
totalIntCount(java.util.function.Predicate<E> filter)
Returns the total count for all objects in this Counter that pass the
given Filter.
|
java.util.Collection<java.lang.Double> |
values()
Returns a copy of the values currently in this counter.
|
addAll, decrementCount, logIncrementCount
public IntCounter()
public IntCounter(MapFactory<E,MutableInteger> mapFactory)
public IntCounter(IntCounter<E> c)
public MapFactory<E,MutableInteger> getMapFactory()
public void setDefaultReturnValue(double rv)
Counter
setDefaultReturnValue
in interface Counter<E>
rv
- The default valuepublic void setDefaultReturnValue(int rv)
public double defaultReturnValue()
Counter
defaultReturnValue
in interface Counter<E>
public int totalIntCount()
public double totalDoubleCount()
public int totalIntCount(java.util.function.Predicate<E> filter)
totalCount()
.public double totalDoubleCount(java.util.function.Predicate<E> filter)
public double totalCount(java.util.function.Predicate<E> filter)
public double averageCount()
public double getCount(java.lang.Object key)
get
that casts
and extracts the primitive value.public java.lang.String getCountAsString(E key)
public int getIntCount(java.lang.Object key)
get
that casts
and extracts the primitive value.public double getNormalizedCount(E key)
Distribution
instead of using this method.public void setCount(E key, int count)
To add to a count instead of replacing it, use
incrementCount(Object,int)
.
public void setCount(E key, java.lang.String s)
public void setCounts(java.util.Collection<E> keys, int count)
To add to the counts of a collection of objects instead of replacing them,
use incrementCounts(Collection,int)
.
public int incrementCount(E key, int count)
decrementCount
.
To more conveniently increment the count by 1, use
incrementCount(Object)
.
To set a count to a specific value instead of incrementing it, use
setCount(Object,int)
.
public double incrementCount(E key)
To increment the count by a value other than 1, use
incrementCount(Object,int)
.
To set a count to a specific value instead of incrementing it, use
setCount(Object,int)
.
incrementCount
in interface Counter<E>
incrementCount
in class AbstractCounter<E>
key
- The key to increment by 1.0public void incrementCounts(java.util.Collection<E> keys, int count)
decrementCounts
.
To more conveniently increment the counts of a collection of objects by
1, use incrementCounts(Collection)
.
To set the counts of a collection of objects to a specific value instead
of incrementing them, use setCounts(Collection,int)
.
public void incrementCounts(java.util.Collection<E> keys)
To increment the counts of a collection of object by a value other
than 1, use incrementCounts(Collection,int)
.
To set the counts of a collection of objects to a specific value instead
of incrementing them, use setCounts(Collection,int)
.
public int decrementCount(E key, int count)
incrementCount
.
To more conveniently decrement the count by 1, use
decrementCount(Object)
.
To set a count to a specifc value instead of decrementing it, use
setCount(Object,int)
.
public double decrementCount(E key)
To decrement the count by a value other than 1, use
decrementCount(Object,int)
.
To set a count to a specifc value instead of decrementing it, use
setCount(Object,int)
.
decrementCount
in interface Counter<E>
decrementCount
in class AbstractCounter<E>
key
- The key to decrement by 1.0public void decrementCounts(java.util.Collection<E> keys, int count)
incrementCount
.
To more conveniently decrement the counts of a collection of objects by
1, use decrementCounts(Collection)
.
To set the counts of a collection of objects to a specific value instead
of decrementing them, use setCounts(Collection,int)
.
public void decrementCounts(java.util.Collection<E> keys)
To decrement the counts of a collection of object by a value other
than 1, use decrementCounts(Collection,int)
.
To set the counts of a collection of objects to a specifc value instead
of decrementing them, use setCounts(Collection,int)
.
public void addAll(IntCounter<E> counter)
To copy the values from another Counter rather than adding them, use
public void subtractAll(IntCounter<E> counter)
To copy the values from another Counter rather than subtracting them, use
public boolean containsKey(E key)
Counter
containsKey
in interface Counter<E>
key
- The keypublic double remove(E key)
public void removeAll(java.util.Collection<E> c)
public void clear()
public int size()
Counter
public boolean isEmpty()
public java.util.Set<E> keySet()
Counter
public java.util.Set<java.util.Map.Entry<E,java.lang.Double>> entrySet()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(java.text.NumberFormat nf, java.lang.String preAppend, java.lang.String postAppend, java.lang.String keyValSeparator, java.lang.String itemSeparator)
public java.lang.String toString(java.text.NumberFormat nf)
public java.lang.Object clone()
clone
in class java.lang.Object
public void removeZeroCounts()
Maybe in the future we should try to do this more on-the-fly, though it's not clear whether a distinction should be made between "never seen" (i.e. null count) and "seen with 0 count". Certainly there's no distinction in getCount() but there is in containsKey().
public int max()
public double doubleMax()
public int min()
public E argmax(java.util.Comparator<E> tieBreaker)
public E argmax()
argmax(Comparator)
. Returns null if this Counter is empty.public E argmin(java.util.Comparator<E> tieBreaker)
public E argmin()
argmin(Comparator)
. Returns null if this Counter is empty.public java.util.Set<E> keysAbove(int countThreshold)
public java.util.Set<E> keysBelow(int countThreshold)
public java.util.Set<E> keysAt(int count)
public Factory<Counter<E>> getFactory()
Counter
getFactory
in interface Counter<E>
public void setCount(E key, double value)
Counter
Counter.incrementCount(Object,double)
.public double incrementCount(E key, double value)
Counter
Counter.incrementCount(Object)
.
To set a count to a specific value instead of incrementing it, use
Counter.setCount(Object,double)
.incrementCount
in interface Counter<E>
incrementCount
in class AbstractCounter<E>
key
- The key to incrementvalue
- The amount to increment it bypublic double totalCount()
Counter
totalCount
in interface Counter<E>
public java.util.Collection<java.lang.Double> values()
Counter
public java.util.Iterator<E> iterator()
public void prettyLog(Redwood.RedwoodChannels channels, java.lang.String description)
prettyLog
in interface PrettyLoggable
channels
- the channels which should be logged to -- all logging calls should
use logging methods on the channels (e.g. channels.log(), etc.)description
- The description of the object. It will potentially identify the
object's functional role or (failing that) its class. This is
typically used as a track name surrounding the contents of this
object.