public class IndexedCounter<T> extends Object implements Counter<T>, Serializable
ObjectCounter
, this class uses an Indexer
to identify object
equivalence and record the count. This class is intended for use when
multiple Counter
instances are needed to be run in parallel or to
keep a record of the counters without storing references to the object
instances in each of the Counter
instances. This results in a linear
space savings at the cost of some minor performance from needing to index the
objects.
This class supports iterating over the set of instances being counted as well as the instances and counts together. All collections views that are returned are unmodifiable and will throw an exception if a mutating method is called.
This class is not thread-safe
Indexer
,
Serialized FormConstructor and Description |
---|
IndexedCounter(Indexer<T> objectIndices)
Creates an empty
Counter that uses the provided indexer to map
countable values to corresponding indices. |
IndexedCounter(Indexer<T> objectIndices,
boolean allowNewIndices)
Creates an empty
Counter that uses the provided indexer to map
countable values to corresponding indices. |
Modifier and Type | Method and Description |
---|---|
void |
add(Counter<? extends T> counter)
Adds the counts from the provided
Counter to the current counts,
adding new elements as needed. |
int |
count(T obj)
Counts the object, increasing its total count by 1.
|
int |
count(T obj,
int count)
Counts the object, increasing its total count by the specified positive amount.
|
void |
countAll(Collection<? extends T> c)
Counts all the elements in the collection.
|
boolean |
equals(Object o) |
int |
getCount(T obj)
Returns the number of times the specified object has been seen by this
counter.
|
double |
getFrequency(T obj)
Returns the frequency of this object relative to the counts of all other
objects.
|
int |
hashCode() |
Set<T> |
items()
Returns a view of the items currently being counted.
|
Iterator<Map.Entry<T,Integer>> |
iterator()
Returns an interator over the elements that have been counted thusfar and
their respective counts.
|
T |
max()
Returns the element that currently has the largest count.
|
T |
min()
Returns the element that currently has the smallest count.
|
void |
reset()
Resets the counts for all objects.
|
int |
size()
Returns the number of unique instances that are currently being counted.
|
int |
sum()
Returns the total number of instances that have been counted.
|
String |
toString() |
public IndexedCounter(Indexer<T> objectIndices)
Counter
that uses the provided indexer to map
countable values to corresponding indices. Should an object be counted
that was not in the indexer, a new index will be added for it.public IndexedCounter(Indexer<T> objectIndices, boolean allowNewIndices)
Counter
that uses the provided indexer to map
countable values to corresponding indices. Objects wth no corresponding
indices will only be counted (and added to the Indexer) if allowNewIndices
is true.allowNewIndices
- true
if objects with new index should be
added to objectIndices
public void add(Counter<? extends T> counter)
Counter
to the current counts,
adding new elements as needed.public int count(T obj)
public int count(T obj, int count)
count
in interface Counter<T>
count
- a positive value for the number of times the object occurredIllegalArgumentException
- if count
is not a positive value.public void countAll(Collection<? extends T> c)
public boolean equals(Object o)
public int getCount(T obj)
public double getFrequency(T obj)
obj
being seen in the items that have been counted.getFrequency
in interface Counter<T>
public int hashCode()
public Set<T> items()
UnsupportedOperationException
.public Iterator<Map.Entry<T,Integer>> iterator()
public T max()
null
is returned. Ties in counts are
arbitrarily broken.public T min()
null
is returned. Ties in counts are
arbitrarily broken.public void reset()
Counter.items()
will be
0 after this call. Note that this does not affect the object indices.public int size()
public int sum()
Copyright © 2012. All Rights Reserved.