java.lang.Object
net.i2p.util.ObjectCounter<K>
Type Parameters:
K - type of objects being counted
All Implemented Interfaces:
Serializable

public class ObjectCounter<K>extends Object implements Serializable
Thread-safe counter for tracking occurrences of objects.

This class provides a concurrent way to count how many times specific objects appear. It uses ConcurrentHashMap and AtomicInteger to ensure thread-safe operations in multi-threaded environments.

Typical use cases include:

  • Counting message frequencies
  • Tracking request rates by client
  • Monitoring event occurrences
  • Statistical data collection
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Object counter storage keyed by tracked item.
    private static final long
    Serializable so it can be passed in an Android Bundle.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Start an empty counter; nothing is tracked until something is incremented.
  • Method Summary

    All Methods
    Modifier and Type
    Method
    Description
    void
    Start over.
    void
    clear(K h)
    Reset the count for this key to zero.
    int
    count(K h)
    How many times this object has been counted, without changing the count.
    void
    decay(int factor)
    Decay all counts by the given factor (integer division).
    int
    Add one.
    void
    max(K h)
    Jump the count straight to a high value instead of incrementing to it.
    Every object currently held, which is the live key set rather than a copy, so it reflects later increments and clears as they happen.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • ObjectCounter

      public ObjectCounter()
      Start an empty counter; nothing is tracked until something is incremented.
  • Method Details

    • increment

      public int increment(K h)
      Add one.
      Parameters:
      h - the object whose occurrence count is to be advanced by one
      Returns:
      count after increment
    • max

      public void max(K h)
      Jump the count straight to a high value instead of incrementing to it.
      Parameters:
      h - the object whose count is pinned high so later increments cannot overflow
    • count

      public int count(K h)
      How many times this object has been counted, without changing the count.
      Parameters:
      h - the object whose count is to be read; 0 if it has never been counted
      Returns:
      the count so far, 0 if the object has never been counted
    • objects

      public Set<K> objects()
      Every object currently held, which is the live key set rather than a copy, so it reflects later increments and clears as they happen.
      Returns:
      set of objects with counts > 0
    • clear

      public void clear()
      Start over. Reset the count for all keys to zero.
    • clear

      public void clear(K h)
      Reset the count for this key to zero.
      Parameters:
      h - the object to drop, leaving later reads of it at 0
    • decay

      public void decay(int factor)
      Decay all counts by the given factor (integer division). Removes entries that decay to zero.
      Parameters:
      factor - divisor for decay (e.g., 2 for 50% decay)