public class ClassicCounter<E> extends java.lang.Object implements java.io.Serializable, Counter<E>, java.lang.Iterable<E>
addAll
method can be used to copy another Counter's contents
over.
Implementation notes:
You shouldn't casually add further methods to
this interface. Rather, they should be added to the Counters
class.
Note that this class stores a
totalCount
field as well as the map. This makes certain
operations much more efficient, but means that any methods that change the
map must also update totalCount
appropriately. If you use the
setCount
method, then you cannot go wrong.
This class is not threadsafe: If multiple threads are accessing the same
counter, then access should be synchronized externally to this class.
Constructor and Description |
---|
ClassicCounter()
Constructs a new (empty) Counter backed by a HashMap.
|
ClassicCounter(java.util.Collection<E> collection)
Constructs a new Counter by counting the elements in the given Collection.
|
ClassicCounter(Counter<E> c)
Constructs a new Counter with the contents of the given Counter.
|
ClassicCounter(int initialCapacity) |
ClassicCounter(MapFactory<E,MutableDouble> mapFactory)
Pass in a MapFactory and the map it vends will back your Counter.
|
ClassicCounter(MapFactory<E,MutableDouble> mapFactory,
int initialCapacity)
Pass in a MapFactory and the map it vends will back your Counter.
|
Modifier and Type | Method and Description |
---|---|
void |
addAll(Counter<E> counter)
Adds the counts in the given Counter to the counts in this Counter.
|
void |
clear()
Removes all entries from the counter.
|
boolean |
containsKey(E key)
Returns whether a Counter contains a key.
|
double |
decrementCount(E key)
Decrements the count for this key by 1.0.
|
double |
decrementCount(E key,
double count)
Decrements the count for this key by the given value.
|
double |
defaultReturnValue()
Returns the default return value.
|
java.util.Set<java.util.Map.Entry<E,java.lang.Double>> |
entrySet()
Returns a view of the entries in this counter.
|
boolean |
equals(java.lang.Object o)
Equality is defined over all Counter implementations.
|
static ClassicCounter<java.lang.String> |
fromString(java.lang.String s)
Converts from the format printed by the toString method back into
a Counter<String>.
|
double |
getCount(java.lang.Object key)
Returns the count for this key as a double.
|
Factory<Counter<E>> |
getFactory()
Returns a factory that can create new instances of this kind of Counter.
|
int |
hashCode()
Returns a hashCode which is the underlying Map's hashCode.
|
static <E> ClassicCounter<E> |
identityHashMapCounter() |
double |
incrementCount(E key)
Increments the count for this key by 1.0.
|
double |
incrementCount(E key,
double count)
Increments the count for the given key by the given value.
|
boolean |
isEmpty()
Returns whether a Counter has no keys in it.
|
java.util.Iterator<E> |
iterator()
This is a shorthand for keySet.iterator().
|
java.util.Set<E> |
keySet()
Returns the Set of keys in this counter.
|
double |
logIncrementCount(E key,
double count)
Increments the count stored in log space for this key by the given
log-transformed value.
|
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 and its associated value from this Counter.
|
void |
removeAll(java.util.Collection<E> keys)
Removes all the given keys from this Counter.
|
void |
setCount(E key,
double count)
Sets the count for the given key to be the given value.
|
void |
setDefaultReturnValue(double rv)
Sets the default return value.
|
int |
size()
Returns the number of entries stored in this counter.
|
java.lang.String |
toString()
Returns a String representation of the Counter, as formatted by
the underlying Map.
|
double |
totalCount()
Computes the total of all counts in this counter, and returns it
as a double.
|
static ClassicCounter<java.lang.String> |
valueOfIgnoreComments(java.lang.String s)
Returns the Counter over Strings specified by this String.
|
java.util.Collection<java.lang.Double> |
values()
Returns a copy of the values currently in this counter.
|
public ClassicCounter()
public ClassicCounter(int initialCapacity)
public ClassicCounter(MapFactory<E,MutableDouble> mapFactory)
mapFactory
- The Map this factory vends will back your Counter.public ClassicCounter(MapFactory<E,MutableDouble> mapFactory, int initialCapacity)
mapFactory
- The Map this factory vends will back your Counter.initialCapacity
- initial capacity of the counterpublic ClassicCounter(Counter<E> c)
c
- The Counter which will be copied.public ClassicCounter(java.util.Collection<E> collection)
collection
- Each item in the Collection is made a key in the
Counter with count being its multiplicity in the Collection.public static <E> ClassicCounter<E> identityHashMapCounter()
public Factory<Counter<E>> getFactory()
getFactory
in interface Counter<E>
public final void setDefaultReturnValue(double rv)
setDefaultReturnValue
in interface Counter<E>
rv
- The default valuepublic double defaultReturnValue()
defaultReturnValue
in interface Counter<E>
public double getCount(java.lang.Object key)
public void setCount(E key, double count)
Counter.incrementCount(Object,double)
.public double incrementCount(E key, double count)
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>
key
- The key to incrementcount
- The amount to increment it bypublic final double incrementCount(E key)
Counter.incrementCount(Object,double)
.
To set a count to a specific value instead of incrementing it, use
Counter.setCount(Object,double)
.incrementCount
in interface Counter<E>
key
- The key to increment by 1.0public double decrementCount(E key, double count)
incrementCount
.
To more conveniently decrement the count by 1.0, use
Counter.decrementCount(Object)
.
To set a count to a specific value instead of decrementing it, use
Counter.setCount(Object,double)
.decrementCount
in interface Counter<E>
key
- The key to decrementcount
- The amount to decrement it bypublic double decrementCount(E key)
Counter.decrementCount(Object,double)
.
To set a count to a specific value instead of decrementing it, use
Counter.setCount(Object,double)
.decrementCount
in interface Counter<E>
key
- The key to decrement by 1.0public double logIncrementCount(E key, double count)
Counter.setCount(Object,double)
.logIncrementCount
in interface Counter<E>
key
- The key to incrementcount
- The amount to increment it by, in log spacepublic void addAll(Counter<E> counter)
public double remove(E key)
public boolean containsKey(E key)
containsKey
in interface Counter<E>
key
- The keypublic java.util.Set<E> keySet()
public java.util.Collection<java.lang.Double> values()
public java.util.Set<java.util.Map.Entry<E,java.lang.Double>> entrySet()
public void clear()
public int size()
public double totalCount()
totalCount
in interface Counter<E>
public java.util.Iterator<E> iterator()
iterator
in interface java.lang.Iterable<E>
public void removeAll(java.util.Collection<E> keys)
keys
- The keys to remove from the Counter. Their values are
subtracted from the total count mass of the Counter.public boolean isEmpty()
public boolean equals(java.lang.Object o)
Note that a Counter with a key with value defaultReturnValue will not be judged equal to a Counter that is lacking that key. In order for two Counters to be correctly judged equal in such cases, you should call Counters.retainNonDefaultValues() on both Counters first.
equals
in class java.lang.Object
o
- Object to compare for equalitypublic int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public static ClassicCounter<java.lang.String> valueOfIgnoreComments(java.lang.String s)
StringKey\tdoubleValue\n
s
- String representation of a Counter, where entries are one per
line such that each line is either a comment (begins with #)
or key \t valuepublic static ClassicCounter<java.lang.String> fromString(java.lang.String s)
s
- A String representation of a Counterpublic 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.