bb.util
Class ObjectCounter<E>

java.lang.Object
  extended by bb.util.ObjectCounter<E>
Type Parameters:
E - Type of element stored

public class ObjectCounter<E>
extends Object

Class to which arbitrary objects may be added, and statistical inquiries may be made about (e.g. getSet, getMostCommon, getFraction, getCountTotal, getCount).

This class is multithread safe: all public methods are synchronized.

Author:
Brent Boyer

Nested Class Summary
static class ObjectCounter.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  long countTotal
           
private  Map<E,AtomicLong> objectToCount
           
 
Constructor Summary
ObjectCounter()
           
 
Method Summary
 void add(E obj)
          Places obj into an internal Map field and increments its occurrence count.
 long getCount(E obj)
          Returns obj's count (i.e. the number of objects that have been added to this instance which equal obj).
 long getCountTotal()
          Returns the total count (i.e. the total number of objects that have been added to this instance).
 double getFraction(E obj)
          Returns the fraction of the total population of objects stored by this instance which equal obj (i.e.
 E getMostCommon()
          Returns that object (of this instance's parameterized type E) which is the most common (i.e. has been added to this instance the most times, and thus has the largest count).
 Set<E> getSet()
          Returns the set of all objects that have been added to this instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

objectToCount

private final Map<E,AtomicLong> objectToCount

countTotal

private long countTotal
Constructor Detail

ObjectCounter

public ObjectCounter()
Method Detail

add

public void add(E obj)
         throws IllegalStateException
Places obj into an internal Map field and increments its occurrence count.

Using a Map means that the set of Objects added via this method which are considered equivalent (and thus share a common count) is determined on the basis of their equals method.

Parameters:
obj - an arbitrary Object (of this instance's parameterized type E) or null
Throws:
IllegalStateException - if countTotal == Long.MAX_VALUE

getSet

public Set<E> getSet()
              throws IllegalStateException
Returns the set of all objects that have been added to this instance.

Contract: the result may be zero-length, but is never null.

Throws:
IllegalStateException

getMostCommon

public E getMostCommon()
                throws IllegalStateException
Returns that object (of this instance's parameterized type E) which is the most common (i.e. has been added to this instance the most times, and thus has the largest count).

Throws:
IllegalStateException - if zero objects have been added to this instance

getFraction

public double getFraction(E obj)
                   throws IllegalStateException
Returns the fraction of the total population of objects stored by this instance which equal obj (i.e. getCount(obj) divided by getCountTotal).

Throws:
IllegalStateException - if zero objects have been added to this instance

getCountTotal

public long getCountTotal()
Returns the total count (i.e. the total number of objects that have been added to this instance).


getCount

public long getCount(E obj)
Returns obj's count (i.e. the number of objects that have been added to this instance which equal obj).