|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.dellroad.stuff.java.IdGenerator
public class IdGenerator
Registry of unique IDs for objects.
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.
IdMapper| Constructor Summary | |
|---|---|
IdGenerator()
|
|
| Method Summary | ||
|---|---|---|
void |
flush()
Flush any cleared weak references. |
|
static IdGenerator |
get()
Get the IdGenerator associated with the current thread. |
|
long |
getId(Object obj)
Get a unique ID for the given object. |
|
Object |
getObject(long id)
Get the object assigned to the given ID. |
|
static
|
run(Callable<R> action)
Create a new IdGenerator and make it available via get() for the duration of the given operation. |
|
static void |
run(Runnable action)
Create a new IdGenerator and make it available via get() for the duration of the given operation. |
|
void |
setId(Object obj,
long id)
Assign a unique ID to the given object. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public IdGenerator()
| Method Detail |
|---|
public long getId(Object obj)
If this method has been previously invoked on this instance with the same obj parameter (where "same" means
object identity, not Object.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
via setId() is performed; if there is a conflict, an exception is thrown.
obj
IllegalArgumentException - if obj is null
IllegalStateException - if the next sequential ID has already been assigned to a different object
via setId()
IllegalStateException - if all 264-1 values have been used up
public void setId(Object obj,
long id)
obj - object to assignid - unique ID number to assign
IllegalArgumentException - if obj is null
IllegalArgumentException - if id has already been assigned to some other objectpublic Object getObject(long id)
id - unique ID
idpublic void flush()
This operation is invoked by getId(), so it's not necessary to explicitly invoke it.
However, if a lot of previously ID'd objects have been garbage collected since the last call to
getId(), then invoking this method may free up some additional memory.
public static void run(Runnable action)
IdGenerator and make it available via get() for the duration of the given operation.
This method is re-entrant: nested invocations of this method in the same thread will cause new IdGenerator
instances to be created and used for the duration of the nested action.
action - action to perform, and which may successfully invoke get()
NullPointerException - if action is null
public static <R> R run(Callable<R> action)
throws Exception
IdGenerator and make it available via get() for the duration of the given operation.
This method is re-entrant: nested invocations of this method in the same thread will cause new IdGenerator
instances to be created and used for the duration of the nested action.
action - action to perform, and which may successfully invoke get()
action
NullPointerException - if action is null
Exceptionpublic static IdGenerator get()
IdGenerator associated with the current thread.
This method only works when the current thread is running within an invocation of run();
otherwise, an IllegalStateException is thrown.
IdGenerator created in the most recent, still running invocation of run(java.lang.Runnable) in this thread
IllegalStateException - if there is not such instance
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||