Class IdGenerator
Instances support creating unique long
ID numbers for objects, as well as setting the unique ID
to a specific value for any unregistered object.
This class uses object identity, not Object.equals()
, to distinguish objects.
Weak references are used to ensure that registered objects can be garbage collected normally.
New long
ID numbers are issued serially; after 264-1 invocations of getId()
,
an IllegalStateException
will be thrown.
Instances are thread safe.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionlong
Test whether the given object is already registered with this instance.void
flush()
Flush any cleared weak references.static IdGenerator
get()
Get theIdGenerator
associated with the current thread.long
Get a unique ID for the given object.getObject
(long id) Get the object assigned to the given ID.long
nextId()
Get the next ID to be assigned.static void
Create a newIdGenerator
and make it available viaget()
for the duration of the given operation.static <R> R
Create a newIdGenerator
and make it available viaget()
for the duration of the given operation.void
Assign a unique ID to the given object.
-
Constructor Details
-
IdGenerator
public IdGenerator()
-
-
Method Details
-
getId
Get a unique ID for the given object.If this method has been previously invoked on this instance with the same
obj
parameter (where "same" means object identity, notObject.equals()
identity), then the same ID value will be returned. Otherwise a new ID value will be returned.New IDs are assigned sequentially starting at
1
. No conflict avoidance with IDs assigned viasetId()
is performed; if there is a conflict, an exception is thrown.- Parameters:
obj
- object to ID; must not be null- Returns:
- a non-zero, unique identifier for
obj
- Throws:
IllegalArgumentException
- ifobj
is nullIllegalStateException
- if the next sequential ID has already been assigned to a different object viasetId()
IllegalStateException
- if all 264-1 values have been used up
-
nextId
public long nextId()Get the next ID to be assigned.This method does not actually assign the ID; it only returns the ID that would be assigned by the next invocation of
getId(java.lang.Object)
.- Returns:
- next available ID
-
checkId
Test whether the given object is already registered with this instance.- Parameters:
obj
- object to test; must not be null- Returns:
- a non-zero, unique identifier for
obj
if already registered, otherwise zero - Throws:
IllegalArgumentException
- ifobj
is null
-
setId
Assign a unique ID to the given object. Does nothing if the object and ID number are already associated.- Parameters:
obj
- object to assignid
- unique ID number to assign- Throws:
IllegalArgumentException
- ifobj
is nullIllegalArgumentException
- ifid
has already been assigned to some other object
-
getObject
Get the object assigned to the given ID.- Parameters:
id
- unique ID- Returns:
- object associated with that ID, or null if no object is assigned to
id
-
flush
public void flush()Flush any cleared weak references.This operation is invoked by
getId()
andsetId()
, so it's usually not necessary to explicitly invoke it. However, if a lot of previously ID'd objects have been garbage collected since the last call togetId()
, then invoking this method may free up some additional memory. -
run
Create a newIdGenerator
and make it available viaget()
for the duration of the given operation.This method is re-entrant: nested invocations of this method in the same thread re-use the same
IdGenerator
instance.- Parameters:
action
- action to perform, and which may successfully invokeget()
- Throws:
IllegalArgumentException
- ifaction
is null
-
run
Create a newIdGenerator
and make it available viaget()
for the duration of the given operation.This method is re-entrant: nested invocations of this method in the same thread re-use the same
IdGenerator
instance.- Type Parameters:
R
- action return type- Parameters:
action
- action to perform, and which may successfully invokeget()
- Returns:
- result of invoking
action
- Throws:
IllegalArgumentException
- ifaction
is null
-
get
Get theIdGenerator
associated with the current thread. This method only works when the current thread is running within an invocation ofrun()
; otherwise, anIllegalStateException
is thrown.- Returns:
- the
IdGenerator
created in the most recent, still running invocation ofrun(java.lang.Runnable)
in this thread - Throws:
IllegalStateException
- if there is not such instance
-