@ThreadSafe public abstract class AbstractSingleton extends Object implements IScopeDestructionAware
| Modifier and Type | Field and Description |
|---|---|
protected ReadWriteLock |
m_aRWLock |
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractSingleton()
Ctor.
|
| Modifier and Type | Method and Description |
|---|---|
static <T extends AbstractSingleton> |
getAllSingletons(IScope aScope,
Class<T> aDesiredClass)
Get all singleton objects registered in the respective sub-class of this
class.
|
static <T extends AbstractSingleton> |
getSingleton(IScope aScope,
Class<T> aClass)
Get the singleton object in the passed scope, using the passed class.
|
static <T extends AbstractSingleton> |
getSingletonIfInstantiated(IScope aScope,
Class<T> aClass)
Get the singleton object if it is already instantiated inside a scope or
null if it is not instantiated. |
static String |
getSingletonScopeKey(Class<? extends AbstractSingleton> aClass)
Create the key which is used to reference the object within the scope.
|
boolean |
isDestroyed() |
boolean |
isInDestruction() |
boolean |
isInInstantiation() |
boolean |
isInPreDestruction() |
boolean |
isInstantiated() |
static boolean |
isSingletonInstantiated(IScope aScope,
Class<? extends AbstractSingleton> aClass)
Check if a singleton is already instantiated inside a scope
|
boolean |
isUsableObject() |
protected void |
onAfterInstantiation(IScope aScope)
Called after the singleton was instantiated.
|
protected void |
onBeforeDestroy(IScope aScopeToBeDestroyed)
Called before this singleton is destroyed.
|
void |
onBeforeScopeDestruction(IScope aScopeToBeDestroyed)
Called before the owning scope is destroyed.
|
protected void |
onDestroy(IScope aScopeInDestruction)
Called when this singleton is destroyed.
|
void |
onScopeDestruction(IScope aScopeInDestruction)
Called when the owning scope is destroyed.
|
protected void |
readAbstractSingletonFields(ObjectInputStream aOIS)
Set all internal status variables to the values read from the specified
ObjectInputStream. |
protected void |
setDestroyed(boolean bDestroyed) |
protected void |
setInDestruction(boolean bInDestruction) |
protected void |
setInInstantiation(boolean bInInstantiation) |
protected void |
setInPreDestruction(boolean bInPreDestruction) |
protected void |
setInstantiated(boolean bInstantiated) |
String |
toString() |
protected void |
writeAbstractSingletonFields(ObjectOutputStream aOOS)
Write the internal status variables to the passed
ObjectOutputStream. |
protected final ReadWriteLock m_aRWLock
@UsedViaReflection protected AbstractSingleton()
protected final void writeAbstractSingletonFields(@Nonnull ObjectOutputStream aOOS) throws IOException
ObjectOutputStream. This can be used to make singletons
serializable.aOOS - The output stream to write to. May not be null.IOException - In case writing failedprotected final void readAbstractSingletonFields(@Nonnull ObjectInputStream aOIS) throws IOException, ClassNotFoundException
ObjectInputStream. This can be used to make singletons
serializable.aOIS - The input stream to read from. May not be null.IOException - In case reading failedClassNotFoundException - In case reading failed@OverrideOnDemand protected void onAfterInstantiation(@Nonnull IScope aScope)
aScope - The scope in which the object was instantiated. Never
null.protected final void setInInstantiation(boolean bInInstantiation)
public final boolean isInInstantiation()
true if this singleton is currently in the phase of
instantiation, false if it is instantiated or already
destroyed.protected final void setInstantiated(boolean bInstantiated)
public final boolean isInstantiated()
true if this singleton was already instantiated,
false if it is active.protected final void setInPreDestruction(boolean bInPreDestruction)
public final boolean isInPreDestruction()
true if this singleton is currently in the phase of
pre destruction, false if it is active or already
destroyed.protected final void setInDestruction(boolean bInDestruction)
public final boolean isInDestruction()
true if this singleton is currently in the phase of
destruction, false if it is active or already
destroyed.protected final void setDestroyed(boolean bDestroyed)
public final boolean isDestroyed()
true if this singleton was already destroyed,
false if it is active.@OverrideOnDemand protected void onBeforeDestroy(@Nonnull IScope aScopeToBeDestroyed) throws Exception
true, "inDestruction" is still
false and "isDestroyed" is false.aScopeToBeDestroyed - The scope that will be destroyed. Never null.Exception - If something goes wrongpublic final void onBeforeScopeDestruction(@Nonnull IScope aScopeToBeDestroyed) throws Exception
IScopeDestructionAwareIScope.preDestroy() callback is invoked and before the
scope is set as being "in destruction".onBeforeScopeDestruction in interface IScopeDestructionAwareaScopeToBeDestroyed - The scope that will be destroyed. Never null.Exception - in case of an error@OverrideOnDemand protected void onDestroy(@Nonnull IScope aScopeInDestruction) throws Exception
false
, "inDestruction" is true and "isDestroyed" is
false.aScopeInDestruction - The scope in destruction. Never null.Exception - If something goes wrongpublic final void onScopeDestruction(@Nonnull IScope aScopeInDestruction) throws Exception
IScopeDestructionAwareonScopeDestruction in interface IScopeDestructionAwareaScopeInDestruction - The scope in destruction. Never null.Exception - in case of an errorpublic final boolean isUsableObject()
true if the object is instantiated and neither in
destruction nor destroyed.@Nonnull public static final String getSingletonScopeKey(@Nonnull Class<? extends AbstractSingleton> aClass)
aClass - The class for which the key is to be created. May not be
null.null key.@Nullable public static final <T extends AbstractSingleton> T getSingletonIfInstantiated(@Nullable IScope aScope, @Nonnull Class<T> aClass)
null if it is not instantiated.T - The type to be returnedaScope - The scope to check. May be null to avoid constructing a
scope.aClass - The class to be checked. May not be null.null otherwise.public static final boolean isSingletonInstantiated(@Nullable IScope aScope, @Nonnull Class<? extends AbstractSingleton> aClass)
aScope - The scope to check. May be null to avoid constructing a
scope.aClass - The class to be checked. May not be null.true if the singleton for the specified class is
already instantiated, false otherwise.@Nonnull public static final <T extends AbstractSingleton> T getSingleton(@Nonnull IScope aScope, @Nonnull Class<T> aClass)
T - The singleton typeaScope - The scope to be used. May not be null.aClass - The class to be used. May not be null. The class must
be public as needs to have a public no-argument constructor.null.@Nonnull @ReturnsMutableCopy public static final <T extends AbstractSingleton> List<T> getAllSingletons(@Nullable IScope aScope, @Nonnull Class<T> aDesiredClass)
T - The singleton type to be retrievedaScope - The scope to use. May be null to avoid creating a new
scope.aDesiredClass - The desired sub-class of this class. May not be null.null list with all instances of the passed class
in the passed scope.Copyright © 2014–2015 Philip Helger. All rights reserved.