java.lang.Object
net.i2p.util.ObjectCounter<K>
- Type Parameters:
K- type of objects being counted
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionprivate final ConcurrentHashMap<K, AtomicInteger> Object counter storage keyed by tracked item.private static final longSerializable so it can be passed in an Android Bundle.Constructor Summary
ConstructorsConstructorDescriptionStart an empty counter; nothing is tracked until something is incremented.Method Summary
All MethodsModifier and TypeMethodDescriptionvoidclear()Start over.voidReset the count for this key to zero.intHow many times this object has been counted, without changing the count.voiddecay(int factor) Decay all counts by the given factor (integer division).intAdd one.voidJump the count straight to a high value instead of incrementing to it.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.
Field Details
serialVersionUID
private static final long serialVersionUIDSerializable so it can be passed in an Android Bundle.- See Also:
map
Object counter storage keyed by tracked item.
Constructor Details
ObjectCounter
public ObjectCounter()Start an empty counter; nothing is tracked until something is incremented.
Method Details
increment
Add one.- Parameters:
h- the object whose occurrence count is to be advanced by one- Returns:
- count after increment
max
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
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
clear
public void clear()Start over. Reset the count for all keys to zero.clear
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)