Package com.dua3.utility.data
Class Counter
java.lang.Object
com.dua3.utility.data.Counter
- All Implemented Interfaces:
Comparable<Counter>
A Counter class that maintains a numerical value and provides methods to
manipulate and retrieve that value. It implements the
Comparable
interface to allow comparison between different Counter instances based
on their values.
This class is not thread-safe!
Motivation for this class is implementing histograms or related structures
where storing a wrapper type would lead to lots of temporary objects being
created. It is intended to be used where the thread-safety of
AtomicLong is not required.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintCompares this Counter instance with the specified Counter instance for order.booleanCompares the specified object with this Counter for equality.inthashCode()Computes the hash code for thisCounterobject based on its current value.
-
Constructor Details
-
Counter
public Counter()Constructs a newCounterinstance with an initial value of 0.
-
-
Method Details
-
compareTo
Compares this Counter instance with the specified Counter instance for order. The comparison is based on the numerical value of the counters.- Specified by:
compareToin interfaceComparable<Counter>- Parameters:
o- the Counter instance to be compared with this instance- Returns:
- a negative integer, zero, or a positive integer as the value of this Counter is less than, equal to, or greater than the value of the specified Counter, respectively
-
equals
Compares the specified object with this Counter for equality. Returns true if the specified object is also a Counter and both instances have the same value. -
hashCode
public int hashCode()Computes the hash code for thisCounterobject based on its current value.
-