com.aliasi.util
Class ObjectToCounterMap<E>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<E,Counter>
          extended by com.aliasi.util.ObjectToCounterMap<E>
Type Parameters:
E - the type of objects used as keys for the map
All Implemented Interfaces:
Serializable, Cloneable, Map<E,Counter>

public class ObjectToCounterMap<E>
extends HashMap<E,Counter>

An ObjectToCounterMap maintains a mapping from objects to integer counters, which may be incremented or set. Objects not in the underlying map are assumed to have count 0, so that incrementing an object that is not mapped sets it to the increment amount.

Since:
LingPipe1.0
Version:
3.8
Author:
Bob Carpenter
See Also:
Serialized Form

Constructor Summary
ObjectToCounterMap()
          Construct an object to counter mapping.
ObjectToCounterMap(int initialSize)
          Construct an object to counter mapping with the specified initial size.
 
Method Summary
 Comparator<E> countComparator()
          Returns a CountComparator that compares objects based on their counts in this object to counter map.
 int getCount(E key)
          Returns the current count value for the specified key.
 void increment(E key)
          Increment the value of the specified key by 1.
 void increment(E key, int n)
          Increment the value of the specified key by the specified amount.
 Object[] keysOrderedByCount()
          Returns the array of keys for this object to counter map sorted in decreasing order of their value counts.
 List<E> keysOrderedByCountList()
          Returns a list of keys for this object to counter map sorted in descending order of count.
 void prune(int minCount)
          Removes all entries in this counter that have less than the specified minimum count.
 void set(E key, int n)
          Sets the value of the specified key to be the specified amount.
 String toString()
          Returns a string representation of this map.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode
 
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

ObjectToCounterMap

public ObjectToCounterMap()
Construct an object to counter mapping.


ObjectToCounterMap

public ObjectToCounterMap(int initialSize)
Construct an object to counter mapping with the specified initial size.

Parameters:
initialSize - Initial size of map.
Method Detail

increment

public void increment(E key)
Increment the value of the specified key by 1. Removes the key from the underlying map if the value after incrementing is 0. Sets the value of the key to 1 if it is not currently set in the underlying map.

Parameters:
key - Object whose count is incremented by 1

increment

public void increment(E key,
                      int n)
Increment the value of the specified key by the specified amount. Sets the value of the key to the specified increment amount if it is not currently set in the underlying map. If the value after incrementing is 0, the key is removed from the map.

Parameters:
key - Object whose count is incremented by the specified amount.
n - Amount to increment the object by.

set

public void set(E key,
                int n)
Sets the value of the specified key to be the specified amount. If the amount is 0, the key is removed from the mapping.

Parameters:
key - Object whose count is incremented by the specified amount.
n - Amount to increment the object by.

getCount

public int getCount(E key)
Returns the current count value for the specified key. If the key is not in the underlying map, the value returned is 0.

Returns:
Current value of count for the key.

toString

public String toString()
Returns a string representation of this map.

Overrides:
toString in class AbstractMap<E,Counter>
Returns:
String representation of this map.

keysOrderedByCountList

public List<E> keysOrderedByCountList()
Returns a list of keys for this object to counter map sorted in descending order of count. Modifying the resulting list does not affect this counter.

Returns:
List of keys for this object to counter map sorted in descending order of count.

keysOrderedByCount

public Object[] keysOrderedByCount()
Returns the array of keys for this object to counter map sorted in decreasing order of their value counts.

Returns:
The array of keys for this counter ordered in descending order of count.

prune

public void prune(int minCount)
Removes all entries in this counter that have less than the specified minimum count.

Parameters:
minCount - Minimum count of objects to retain.

countComparator

public Comparator<E> countComparator()
Returns a CountComparator that compares objects based on their counts in this object to counter map. It first compares objects based on their count, and if the counts are the same and the objects are comparable, on the objects themselves. Thus compare(obj1,obj2) method will not be consistent with equals (see Comparable) unless the set of keys has a natural ordering that is consistent with equals. This inconsistency with equals is tolerable in cases where the comparator is used for sorting, as in Arrays.sort(Object[],Comparator), but not for backing a sorted collection such as TreeMap or TreeSet.

Returns:
Comparator based on this object to counter map's counts.