public class BlockingSingletonRegistry extends Object
This might be useful in server environments where the container (e.g. a CDI container) takes care about instantiation (at some time) but clients want or need to access the the instances from without the containers context (e.g. via plain old static getter calls).
The class can be directly instantiated using the default constructor or a static default instance can be used.
This class intensively uses BlockingObjectContainer internally
BlockingObjectContainer| Constructor and Description |
|---|
BlockingSingletonRegistry() |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
get(Class<T> clazz)
Retrieves the singleton instance of the given class waiting
uninterruptibly until it is registered (i.e. the waiting Thread will NOT
react to
Thread.interrupt() calls)
Attention: this call blocks FOREVER - so you REALLY should take care that
the requested instance is finally registered |
<T> T |
get(Class<T> clazz,
long time,
TimeUnit timeUnit)
Retrieves the singleton instance of the given class waiting interruptibly
(i.e. the waiting Thread will react to
Thread.interrupt() calls)
until it is registered or the given timeout has elapsed |
static BlockingSingletonRegistry |
getDefault()
Returns a static default (singleton) instance of the
BlockingSingletonRegistry |
<T> T |
getInterruptibly(Class<T> clazz)
Retrieves the singleton instance of the given class waiting interruptibly
until it is registered (i.e. the waiting Thread will react to
Thread.interrupt() calls)
Attention: this call block FOREVER (if not interrupted) - so you REALLY
should take care that the requested instance is finally registered |
<T> T |
getNoWait(Class<T> clazz)
Retrieves the singleton instance of the given class if currently
registered without potentially waiting for it (i.e. the call will always
return immediately)
|
<T> void |
remove(Class<T> clazz)
Removes the singleton of the given class from the registry
|
<T> void |
remove(T instance)
Remove the given singleton instance from the registry
|
<T> void |
set(T instance)
Registers a new singleton instance in the registry
|
public static BlockingSingletonRegistry getDefault()
BlockingSingletonRegistrypublic <T> void set(T instance)
throws IllegalStateException
T - the type of the singleton object to registerinstance - the singleton instance to registerIllegalStateException - if there is already another instance of the given class
registered (it's not a singleton)public <T> void remove(Class<T> clazz)
T - the type of the singleton object to removeclazz - the singleton class whose instance should be removedpublic <T> void remove(T instance)
T - the type of the singleton object to removeinstance - the singleton instance that should be removedIllegalArgumentException - if another one than the registered singleton instance should be
removedpublic <T> T get(Class<T> clazz)
Thread.interrupt() calls)
Attention: this call blocks FOREVER - so you REALLY should take care that the requested instance is finally registered
T - the type of the singleton object to retrieveclazz - the class whose singleton instance should be retrievedpublic <T> T getInterruptibly(Class<T> clazz) throws InterruptedException
Thread.interrupt() calls)
Attention: this call block FOREVER (if not interrupted) - so you REALLY should take care that the requested instance is finally registered
T - the type of the singleton object to retrieveclazz - the class whose singleton instance should be retrievedInterruptedException - if the waiting Thread is interrupted while waitingpublic <T> T get(Class<T> clazz, long time, TimeUnit timeUnit) throws InterruptedException
Thread.interrupt() calls)
until it is registered or the given timeout has elapsedT - the type of the singleton object to retrieveclazz - the class whose singleton instance should be retrievedtime - the number of TimeUnits to wait at until the call
returnstimeUnit - the TimeUnit of the given time amount to waitInterruptedException - if the waiting Thread is interrupted while waitingpublic <T> T getNoWait(Class<T> clazz)
T - the type of the singleton object to retrieveclazz - the class whose singleton instance should be retrievednull if non is
registeredCopyright © 2017. All rights reserved.