Class Counter

java.lang.Object
com.dua3.utility.data.Counter
All Implemented Interfaces:
Comparable<Counter>

public final class Counter extends Object implements 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
    Constructor
    Description
    Constructs a new Counter instance with an initial value of 0.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Compares this Counter instance with the specified Counter instance for order.
    boolean
    Compares the specified object with this Counter for equality.
    int
    Computes the hash code for this Counter object based on its current value.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Counter

      public Counter()
      Constructs a new Counter instance with an initial value of 0.
  • Method Details

    • compareTo

      public int compareTo(Counter o)
      Compares this Counter instance with the specified Counter instance for order. The comparison is based on the numerical value of the counters.
      Specified by:
      compareTo in interface Comparable<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

      public boolean equals(Object obj)
      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.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to be compared for equality with this Counter
      Returns:
      true if the specified object is equal to this Counter, false otherwise
    • hashCode

      public int hashCode()
      Computes the hash code for this Counter object based on its current value.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code computed from the value of this counter