java.lang.Object
net.i2p.util.ObjectCounterUnsafe<K>
- Type Parameters:
K- type of objects being counted
Non-thread-safe counter for tracking occurrences of objects.
This class provides a lightweight alternative to ObjectCounter for single-threaded scenarios. It offers better performance with reduced object churn by using primitive integers instead of AtomicInteger.
Key differences from ObjectCounter:
- NOT thread-safe - use only in single-threaded contexts
- Lower memory overhead and better performance
- Additional add() method for incrementing by arbitrary values
sortedObjects()method for retrieving objects by count
Typical use cases include UI components and Sybil attack detection where thread safety is not required.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classModifiable integer.private classReverse sort.Field Summary
FieldsConstructor Summary
ConstructorsConstructorDescriptionA counter holding no entries; the backing map starts empty and is filled only byincrement(Object)andadd(Object, int).Method Summary
Field Details
map
Counts by key.
Constructor Details
ObjectCounterUnsafe
public ObjectCounterUnsafe()A counter holding no entries; the backing map starts empty and is filled only byincrement(Object)andadd(Object, int).
Method Details
increment
Add one.- Parameters:
h- the object whose count is raised by one- Returns:
- count after increment
add
Add a value.- Parameters:
h- the object whose count is raisedval- the number to add, which may be negative- Returns:
- count after adding
count
Look up the current count for one key.- Parameters:
h- the object whose count is looked up- Returns:
- the count recorded for the key, or 0 if it has never been added
objects
sortedObjects
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 from the map