suramadu.util.stats
Class Counter

java.lang.Object
  extended bysuramadu.util.stats.Counter
All Implemented Interfaces:
java.lang.Comparable, java.util.Map.Entry

public class Counter
extends java.lang.Object
implements java.util.Map.Entry, java.lang.Comparable

A counter object suitable for storing in a Map

Author:
mbi

Constructor Summary
Counter(java.lang.Object key)
          Creates a new instance of Counter with the specified key
Counter(java.lang.Object key, int initialValue)
          Creates a new instance of Counter with the specified key, and with the initial value of the counter specified.
 
Method Summary
 void clear()
          Clear the counter to zero.
 int compareTo(java.lang.Object o)
          Comparaable Interface Implementation
 boolean equals(java.lang.Object o)
          Compares the specified object with this entry for equality.
 java.lang.Object getKey()
          Gets the key that is used to put this Object in a Map.
 java.lang.Object getValue()
          Gets the current value of the counter as an Integer.
 int getValueAsInt()
          Returns the current value of the counter as an int.
 int hashCode()
          Returns the hash code value for this map entry.
 int postDec()
          Return the current value of the counter, and then decrement it.
 int postInc()
          Return the current value of the counter, and then increment it.
 int preDec()
          Return the value of the counter after incrementing it.
 int preInc()
          Return the value of the counter after incrementing it.
 java.lang.Object setValue(java.lang.Object value)
          Sets the current value of the counter as Number.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Counter

public Counter(java.lang.Object key)
Creates a new instance of Counter with the specified key

Parameters:
key - the key to use to put in a Map.

Counter

public Counter(java.lang.Object key,
               int initialValue)
Creates a new instance of Counter with the specified key, and with the initial value of the counter specified.

Parameters:
key - the key to use to put in a Map.
initialValue - the initial value of the counter.
Method Detail

postInc

public int postInc()
Return the current value of the counter, and then increment it.

Returns:
the value of the counter before incrementing it.

postDec

public int postDec()
Return the current value of the counter, and then decrement it.

Returns:
the value of the counter before decrementing it.

preInc

public int preInc()
Return the value of the counter after incrementing it.

Returns:
the incremented value of the counter.

preDec

public int preDec()
Return the value of the counter after incrementing it.

Returns:
the decremented value of the counter.

clear

public void clear()
Clear the counter to zero.


getKey

public final java.lang.Object getKey()
Gets the key that is used to put this Object in a Map.

Specified by:
getKey in interface java.util.Map.Entry
Returns:
the Object used as a key when putting this Object in a Map.

getValue

public java.lang.Object getValue()
Gets the current value of the counter as an Integer. This is a method for implementing the Map.Entry interface.

Specified by:
getValue in interface java.util.Map.Entry
Returns:
an Integer Object representing the current value of the counter.

setValue

public java.lang.Object setValue(java.lang.Object value)
Sets the current value of the counter as Number. This is a method for implementing the Map.Entry interface.

Specified by:
setValue in interface java.util.Map.Entry
Parameters:
value - the Number Object to use to set the value of the counter.
Returns:
an Integer Object representing the current value of the counter.

getValueAsInt

public int getValueAsInt()
Returns the current value of the counter as an int.

Returns:
the current value of the counter as an int.

hashCode

public int hashCode()
Returns the hash code value for this map entry. The hash code of a map entry e is defined to be:
(e.getKey()==null ? 0 : e.getKey().hashCode()) ^ (e.getValue()==null ? 0 : e.getValue().hashCode())
This ensures that e1.equals(e2) implies that e1.hashCode()==e2.hashCode() for any two Entries e1 and e2, as required by the general contract of Object.hashCode.

Specified by:
hashCode in interface java.util.Map.Entry
Returns:
Returns the hash code value for this map entry. The hash code of a map entry e is defined to be:
(e.getKey()==null ? 0 : e.getKey().hashCode()) ^ (e.getValue()==null ? 0 : e.getValue().hashCode())
This ensures that e1.equals(e2) implies that e1.hashCode()==e2.hashCode() for any two Entries e1 and e2, as required by the general contract of Object.hashCode.
See Also:
Object.hashCode()

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this entry for equality. Returns true if the given object is also a map entry and the two entries represent the same mapping. More formally, two entries e1 and e2 represent the same mapping if
(e1.getKey()==null ? e2.getKey()==null : e1.getKey().equals(e2.getKey())) && (e1.getValue()==null ? e2.getValue()==null : e1.getValue().equals(e2.getValue()))
This ensures that the equals method works properly across different implementations of the Map.Entry interface.

Specified by:
equals in interface java.util.Map.Entry
Parameters:
o - object to be compared for equality with this map entry.
Returns:
object to be compared for equality with this map entry, otherwise false.

compareTo

public int compareTo(java.lang.Object o)
Comparaable Interface Implementation

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - The Object that we will be comparing.
Returns:
see java.lang.Comparable
Throws:
java.lang.ClassCastException - if the Object being compared is not of the same type.
See Also:
Comparable