net.sf.statcvs.util
Class IntegerMap

java.lang.Object
  extended by net.sf.statcvs.util.IntegerMap

public class IntegerMap
extends java.lang.Object

Utility class for storing a map from Objects to ints. This class makes it easy to sort by key or value, and provides useful features like sum(), max(), and percent calculation.

The keys must be comparable, for example Strings.

Behaviour for null keys is unspecified.

Version:
$Id: IntegerMap.java,v 1.16 2008/04/02 11:52:02 benoitx Exp $
Author:
Richard Cyganiak

Constructor Summary
IntegerMap()
           
 
Method Summary
 void addInt(java.lang.Object key, int addValue)
          Adds an int to the value stored at a key.
 double average()
          Returns the average of all values in the map.
 boolean contains(java.lang.Object key)
          Returns true if the map contains a value for this key.
 void dec(java.lang.Object key)
          Same as addInt(key, -1)
 int get(java.lang.Object key)
          Gets a value from the map.
 java.lang.Integer getInteger(java.lang.Object key)
          Same as get(Object), but returns an Integer, not an int.
 double getPercent(java.lang.Object key)
          Gets the value stored at a key as a percentage of all values in the map.
 double getPercentOfMaximum(java.lang.Object key)
          Gets the value stored at a key as a percentage of the maximum value in the map.
 void inc(java.lang.Object key)
          Same as addInt(key, 1)
 java.util.Iterator iteratorSortedByKey()
          Returns an iterator on the keys, sorted by key ascending.
 java.util.Iterator iteratorSortedByValue()
          Returns an iterator on the keys, sorted by values ascending.
 java.util.Iterator iteratorSortedByValueReverse()
          Returns an iterator on the keys, sorted by values descending.
 java.util.Set keySet()
          Returns a set view of the keys.
 int max()
          Returns the maximum value in the map.
 void put(java.lang.Object key, int value)
          Puts a value into the map, overwriting any previous value for the same key.
 void remove(java.lang.Object key)
          Deletes a value from the map.
 int size()
          Returns the number of key-value pairs stored in the map.
 int sum()
          Returns the sum of all values in the map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IntegerMap

public IntegerMap()
Method Detail

put

public void put(java.lang.Object key,
                int value)
Puts a value into the map, overwriting any previous value for the same key.

Parameters:
key - an Object which is used as key.
value - the int value to be stored at this key.

get

public int get(java.lang.Object key)
Gets a value from the map. Returns the value which was stored in the map at the same key before. If no value was stored for this key, 0 will be returned.

Parameters:
key - an Object which is used as key.
Returns:
the value for this key

getInteger

public java.lang.Integer getInteger(java.lang.Object key)
Same as get(Object), but returns an Integer, not an int.

Parameters:
key - the key to get the value for
Returns:
the value wrapped in an Integer object

getPercent

public double getPercent(java.lang.Object key)
Gets the value stored at a key as a percentage of all values in the map.

Parameters:
key - the key to get the value for
Returns:
the value as a percentage of the sum of all values

getPercentOfMaximum

public double getPercentOfMaximum(java.lang.Object key)
Gets the value stored at a key as a percentage of the maximum value in the map. For the maximum value, this will return 100.0. For a value half as large as the maximum value, this will return 50.0.

Parameters:
key - the key to get the value for
Returns:
the value as a percentage of largest value in the map

addInt

public void addInt(java.lang.Object key,
                   int addValue)
Adds an int to the value stored at a key. If no value was stored before at this key, the int will be stored there.

Parameters:
key - the key to whose value addValue should be added
addValue - the int to be added

inc

public void inc(java.lang.Object key)
Same as addInt(key, 1)

Parameters:
key - the key whose value should be increased

dec

public void dec(java.lang.Object key)
Same as addInt(key, -1)

Parameters:
key - the key whose value should be decreased

remove

public void remove(java.lang.Object key)
Deletes a value from the map. This is different from put(key, 0). Removing will reduce the size of the map, putting 0 will not.

Parameters:
key - the key that should be removed

contains

public boolean contains(java.lang.Object key)
Returns true if the map contains a value for this key.

Parameters:
key - the key to check for
Returns:
true if the key is in the map

size

public int size()
Returns the number of key-value pairs stored in the map.

Returns:
the number of key-value pairs stored in the map

keySet

public java.util.Set keySet()
Returns a set view of the keys. The set will be in ascending key order.

Returns:
a Set view of all keys

iteratorSortedByKey

public java.util.Iterator iteratorSortedByKey()
Returns an iterator on the keys, sorted by key ascending.

Returns:
an iterator on the keys

iteratorSortedByValue

public java.util.Iterator iteratorSortedByValue()
Returns an iterator on the keys, sorted by values ascending.

Returns:
an iterator on the keys

iteratorSortedByValueReverse

public java.util.Iterator iteratorSortedByValueReverse()
Returns an iterator on the keys, sorted by values descending.

Returns:
an iterator on the keys

sum

public int sum()
Returns the sum of all values in the map.

Returns:
the sum of all values in the map

average

public double average()
Returns the average of all values in the map.

Returns:
the average of all values in the map

max

public int max()
Returns the maximum value in the map.

Returns:
the maximum value in the map.


Copyright © 2008. All Rights Reserved.