wyvern.util
Class Counter

java.lang.Object
  extended bywyvern.util.Counter

public class Counter
extends java.lang.Object

A utility class that can keep counts associated with objects. The most common use is to count occurrences of Strings or some other object type, but you can also use it to keep track of times or other numbers, per object.

The counter is implemented as a hashtable, where the keys are the objects, and the values are the counts for those objects.

Version:
1.0, Oct 8, 2001
Author:
Steve Yegge

Nested Class Summary
static class Counter.Count
          Keeps track of an object and its count.
 
Constructor Summary
Counter()
          Constructs a new Counter
 
Method Summary
 void clear()
          Clears out all the counts - removes all keys from the table.
 void decrementCount(java.lang.Object obj)
          Decrements the count for this object.
 void decrementCount(java.lang.Object obj, int amount)
          Decrements the count for an object by the specified amount.
 java.lang.String dumpValues()
          Returns a printout of the (sorted) values in the list.
 java.lang.String dumpValues(int max)
          Returns a printout of the (sorted) values in the list.
 int getCount(java.lang.Object obj)
          Returns the count for a particular object.
 java.util.List getCounts()
          Returns a list of the counts we've accumulated so far.
 java.util.List getSortedCounts()
          Returns a list, sorted by count, from highest to lowest, of the keys in the data structure.
 int getTotalCount()
          Returns the sum of all the counts in this Counter.
 void incrementCount(java.lang.Object obj)
          Increments the count for this object.
 void incrementCount(java.lang.Object obj, int amount)
          Increments the count for this object by the desired amount.
static void main(java.lang.String[] args)
          Tests the data structure.
 void setCount(java.lang.Object obj, int total)
          Sets the count directly for this object.
 int size()
          Returns the number of distinct keys in the table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Counter

public Counter()
Constructs a new Counter

Method Detail

incrementCount

public void incrementCount(java.lang.Object obj)
Increments the count for this object.


incrementCount

public void incrementCount(java.lang.Object obj,
                           int amount)
Increments the count for this object by the desired amount.

Parameters:
obj - the object
amount - the amount to add to the count

decrementCount

public void decrementCount(java.lang.Object obj)
Decrements the count for this object.


decrementCount

public void decrementCount(java.lang.Object obj,
                           int amount)
Decrements the count for an object by the specified amount.

Parameters:
obj - the object
amount - the amount to subtract

getCount

public int getCount(java.lang.Object obj)
Returns the count for a particular object.


setCount

public void setCount(java.lang.Object obj,
                     int total)
Sets the count directly for this object. Replaces whatever we had for a count before.


getCounts

public java.util.List getCounts()
Returns a list of the counts we've accumulated so far.

Returns:
a list of Count objects, each containing a key and a count for that key

getSortedCounts

public java.util.List getSortedCounts()
Returns a list, sorted by count, from highest to lowest, of the keys in the data structure.

Returns:
a List of Count objects, sorted in order from highest count to lowest count. There is no sorting on the keys with the same count.

dumpValues

public java.lang.String dumpValues()
Returns a printout of the (sorted) values in the list.


dumpValues

public java.lang.String dumpValues(int max)
Returns a printout of the (sorted) values in the list.

Parameters:
max - the max number of values to print.

clear

public void clear()
Clears out all the counts - removes all keys from the table.


getTotalCount

public int getTotalCount()
Returns the sum of all the counts in this Counter.


size

public int size()
Returns the number of distinct keys in the table.


main

public static void main(java.lang.String[] args)
Tests the data structure.