gov.llnl.ontology.util
Class Counter<T>

java.lang.Object
  extended by gov.llnl.ontology.util.Counter<T>
All Implemented Interfaces:
Serializable, Iterable<Map.Entry<T,Integer>>
Direct Known Subclasses:
StringCounter

public class Counter<T>
extends Object
implements Iterable<Map.Entry<T,Integer>>, Serializable

A utility for counting unique instance of an object.

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

See Also:
Serialized Form

Nested Class Summary
 class Counter.EntryComparator
          Compares the integer value of two Map#Entry object based on ascending or descending order.
 
Constructor Summary
Counter()
          Creates an empty Counter.
Counter(Collection<? extends T> items)
          Creates a Counter whose initial state has counted all of the specified items.
 
Method Summary
 int count(T obj)
          Counts the object, increasing its total count by 1.
 int count(T obj, int delta)
          Counts the object, increasing its total count by 1.
 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.
 List<T> itemsSorted(boolean sortAscending)
          Returns a view of the items currently being counted in sorted order.
 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()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Counter

public Counter()
Creates an empty Counter.


Counter

public Counter(Collection<? extends T> items)
Creates a Counter whose initial state has counted all of the specified items.

Method Detail

count

public int count(T obj)
Counts the object, increasing its total count by 1.


count

public int count(T obj,
                 int delta)
Counts the object, increasing its total count by 1.


equals

public boolean equals(Object o)
Overrides:
equals in class Object

getCount

public int getCount(T obj)
Returns the number of times the specified object has been seen by this counter.


getFrequency

public double getFrequency(T obj)
Returns the frequency of this object relative to the counts of all other objects. This value may also be interpreted as the probability of the instance of obj being seen in the items that have been counted.


hashCode

public int hashCode()
Overrides:
hashCode in class Object

items

public Set<T> items()
Returns a view of the items currently being counted. The returned view is read-only; any attempts to modify this view will throw an UnsupportedOperationException.


itemsSorted

public List<T> itemsSorted(boolean sortAscending)
Returns a view of the items currently being counted in sorted order. The returned view is read-write.


iterator

public Iterator<Map.Entry<T,Integer>> iterator()
Returns an interator over the elements that have been counted thusfar and their respective counts.

Specified by:
iterator in interface Iterable<Map.Entry<T,Integer>>

max

public T max()
Returns the element that currently has the largest count. If no objects have been counted, null is returned. Ties in counts are arbitrarily broken.


min

public T min()
Returns the element that currently has the smallest count. If no objects have been counted, null is returned. Ties in counts are arbitrarily broken.


reset

public void reset()
Resets the counts for all objects. The size of items() will be 0 after this call.


size

public int size()
Returns the number of instances that are currently being counted.


sum

public int sum()
Returns the total number of instances that have been counted.


toString

public String toString()
Overrides:
toString in class Object


Copyright © 2010-2011. All Rights Reserved.