public class

ReferenceCounter

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.util.collect.ReferenceCounter<K>

Class Overview

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.

Summary

Public Constructors
ReferenceCounter()
Create a new reference counter.
Public Methods
synchronized void clear()
Reset the reference counter.
synchronized Map<K, Integer> copyToMap()
synchronized int dec(K key)
Unrecord an object, ie decrement its reference counter by one.
synchronized String formatAllReferences()
Format all references in descending order.
synchronized String formatTopReferences(int top)
Format the top references (= descending order) counted referenced by the counter.
synchronized int get(K key)
Get the reference counter for an object.
synchronized Map<K, Integer> getAsMap()
Get a read-only map of (referenced object -> count).
synchronized int getCount()
Get the total count for all the references actively counted by this counter.
synchronized boolean has(K key)
Check if an object is currently referenced, ie its reference counter is greater or equal than zero.
synchronized int inc(K key)
Record an object, ie increment its reference counter.
synchronized boolean isEmpty()
Determine whether there are references actively tracked by this counter.
synchronized Set<K> keys()
synchronized void load(Collection<? extends K> coll)
synchronized boolean remove(K key)
Remove an object from tracking.
synchronized int size()
Get the number of references actively counted by this counter.
synchronized String toString()
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public ReferenceCounter ()

Create a new reference counter.

Public Methods

public synchronized void clear ()

Reset the reference counter.

public synchronized Map<K, Integer> copyToMap ()

public synchronized int dec (K key)

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

public synchronized String formatAllReferences ()

Format all references in descending order.

Returns
  • a string

public synchronized String formatTopReferences (int top)

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

public synchronized int get (K key)

Get the reference counter for an object.

Parameters
key object
Returns
  • the counter; 0 if the object is currently not tracked

public synchronized Map<K, Integer> getAsMap ()

Get a read-only map of (referenced object -> count). To be used in a "this counter"-synchronized block.

public synchronized int getCount ()

Get the total count for all the references actively counted by this counter.

public synchronized boolean has (K key)

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

public synchronized int inc (K key)

Record an object, ie increment its reference counter. Will raise an IllegalStateException if the counter exceeds the capacity of a signed integer.

Parameters
key object
Returns
  • the updated counter for this object

public synchronized boolean isEmpty ()

Determine whether there are references actively tracked by this counter.

public synchronized Set<K> keys ()

public synchronized void load (Collection<? extends K> coll)

public synchronized boolean remove (K key)

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

public synchronized int size ()

Get the number of references actively counted by this counter.

public synchronized String toString ()