public class ObjectCounter<T> extends Object implements Counter<T>, Serializable
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
Constructor and Description |
---|
ObjectCounter()
Creates an empty
Counter . |
ObjectCounter(Collection<? extends T> items)
Creates a
Counter whose initial state has counted all of the
specified items. |
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 instances that are currently being counted.
|
int |
sum()
Returns the total number of instances that have been counted.
|
String |
toString() |
public ObjectCounter()
Counter
.public ObjectCounter(Collection<? extends T> items)
Counter
whose initial state has counted all of the
specified items.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()
items()
will be
0 after this call.public int size()
public int sum()
Copyright © 2012. All Rights Reserved.