Package com.pnfsoftware.jeb.util.collect
Class ReferenceCounter<K>
java.lang.Object
com.pnfsoftware.jeb.util.collect.ReferenceCounter<K>
- Type Parameters:
K
-
A simple, thread-safe object reference counter. A counter can be associated to any object of the
provided
K
type.
Note that this utility class has nothing to do with Garbage Collection reference counting.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Reset the reference counter.int
Unrecord an object, ie decrement its reference counter by one.Format all references in descending order.formatTopReferences
(int top) Format the top references (= descending order) counted referenced by the counter.int
Get the reference counter for an object.getAsMap()
Get a read-only map of (referenced object -> count).int
getCount()
Get the total count for all the references actively counted by this counter.boolean
Check if an object is currently referenced, ie its reference counter is greater or equal than zero.int
Record an object, ie increment its reference counter.boolean
isEmpty()
Determine whether there are references actively tracked by this counter.keys()
void
load
(Collection<? extends K> coll) boolean
Remove an object from tracking.int
size()
Get the number of references actively counted by this counter.toString()
-
Constructor Details
-
ReferenceCounter
public ReferenceCounter()Create a new reference counter.
-
-
Method Details
-
clear
public void clear()Reset the reference counter. -
isEmpty
public boolean isEmpty()Determine whether there are references actively tracked by this counter.- Returns:
-
size
public int size()Get the number of references actively counted by this counter.- Returns:
-
getCount
public int getCount()Get the total count for all the references actively counted by this counter.- Returns:
-
inc
Record an object, ie increment its reference counter. Will raise anIllegalStateException
if the counter exceeds the capacity of a signed integer.- Parameters:
key
- object- Returns:
- the updated counter for this object
-
dec
Unrecord an object, ie decrement its reference counter by one. If the object is currently untracked, an exception will be raised.- Parameters:
key
- object- Returns:
- the updated counter for this object
- Throws:
IllegalArgumentException
- if the object is not present
-
has
Check if an object is currently referenced, ie its reference counter is greater or equal than zero.- Parameters:
key
- object- Returns:
- true if the object is referenced
-
get
Get the reference counter for an object.- Parameters:
key
- object- Returns:
- the counter; 0 if the object is currently not tracked
-
getAsMap
Get a read-only map of (referenced object -> count). To be used in a "this counter"-synchronized block.- Returns:
-
remove
Remove an object from tracking. This method is equivalent to decrementing the object counter until zero.- Parameters:
key
- object- Returns:
- true if the object was actually referenced (ie its counter was >= 1), false if it was not
-
load
- Parameters:
coll
-
-
keys
- Returns:
-
copyToMap
- Returns:
-
toString
-
formatAllReferences
Format all references in descending order.- Returns:
- a string
-
formatTopReferences
Format the top references (= descending order) counted referenced by the counter.- Parameters:
top
- the number of top references to format; a negative number means to format all references- Returns:
- a string
-