com.googlecode.whatswrong.javautils
Class Counter<T>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<T,java.lang.Integer>
          extended by com.googlecode.whatswrong.javautils.Counter<T>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<T,java.lang.Integer>

public class Counter<T>
extends java.util.HashMap<T,java.lang.Integer>

A Counter counts objects of class T.

Author:
Sebastian Riedel
See Also:
Serialized Form

Constructor Summary
Counter()
           
 
Method Summary
 java.lang.Integer get(java.lang.Object o)
          Gets the count of object o.
 int getMaximum()
          Gets the maximum count of all objects in the counter.
 void increment(T value, int howmuch)
          Increments the count for the given object by howmuch
static Counter<java.lang.String> loadFromFile(java.io.File file)
          Loads counts from a column separated file where row looks like "value count".
 void save(java.io.OutputStream outputStream)
          Saves the counts to column separated text file with format "value count" in each row.
 java.util.List<java.util.Map.Entry<T,java.lang.Integer>> sorted(boolean descending)
          Sort map entries by counts.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

Counter

public Counter()
Method Detail

get

public java.lang.Integer get(java.lang.Object o)
Gets the count of object o.

Specified by:
get in interface java.util.Map<T,java.lang.Integer>
Overrides:
get in class java.util.HashMap<T,java.lang.Integer>
Parameters:
o - the object to get the count of.
Returns:
the count of object o. If no count for o has specified zero is returned.

increment

public void increment(T value,
                      int howmuch)
Increments the count for the given object by howmuch

Parameters:
value - the object to increment the count for.
howmuch - how much the count should be incremented.

loadFromFile

public static Counter<java.lang.String> loadFromFile(java.io.File file)
                                              throws java.io.IOException
Loads counts from a column separated file where row looks like "value count".

Parameters:
file - the file to load from.
Returns:
the Counter object representing the counts in the file
Throws:
java.io.IOException - if I/O goes wrong.

sorted

public java.util.List<java.util.Map.Entry<T,java.lang.Integer>> sorted(boolean descending)
Sort map entries by counts.

Parameters:
descending - the list start with the highest or lowest count.
Returns:
a list of map entries ordered by count.

save

public void save(java.io.OutputStream outputStream)
Saves the counts to column separated text file with format "value count" in each row.

Parameters:
outputStream - the output stream to print to.

getMaximum

public int getMaximum()
Gets the maximum count of all objects in the counter.

Returns:
the maximum count of all objects in the counter.


Copyright © 2009. All Rights Reserved.