public abstract class DependencyScope
extends java.lang.Object
DependencyScope
provides an abstract base class for implementing objects that provide
and cache a scoped dependencies. An instance of a concrete implementation of DependencyScope
is not used directly to access dependencies, but via the static methods of class Dependency
(or convenience class D
).Modifier and Type | Field and Description |
---|---|
protected java.util.ArrayList<java.lang.Object> |
dependants |
protected java.util.HashMap<java.lang.Class<?>,java.util.ArrayList<java.lang.Object>> |
dependencies |
protected DependencyProvider |
dependencyProvider |
protected boolean |
initialized |
protected DependencyScope |
mockScope
A reference to an optional overriding
DependencyScope used providing mock dependencies. |
protected DependencyScope |
parentScope |
Modifier | Constructor and Description |
---|---|
protected |
DependencyScope() |
Modifier and Type | Method and Description |
---|---|
void |
addDependant(java.lang.Object dependant)
Adds the given
Object as a dependant to this DependencyScope . |
protected <T> T |
cache(java.lang.Class<T> dependencyType,
java.lang.Object dependency)
Caches the given requested dependency
Object using the requested type as a key. |
protected DependencyProvider |
createDependencyProvider()
This method can be overridden in extending classes to provide a method that explicitly
instantiates a
DependencyProvider for this DependencyScope . |
protected <T> T |
dependency(java.lang.Object dependency)
Caches the requested dependency
Object before it is returned to a requester. |
protected void |
dispose()
Disposes this
DependencyScope to support effective GC and to avoid memory leaks. |
protected <T> void |
getDependencies(DependencyQuery<T> query,
java.lang.Object dependant)
Gets an dependency instance of the specified type.
|
protected <T> T |
getDependency()
This method is meant to be implemented by each concrete implementation of
DependencyScope . |
protected <T> T |
getDependency(java.lang.Class<T> dependencyType,
java.lang.Object dependant,
boolean createNew)
Gets an dependency instance of the specified type.
|
protected DependencyProvider |
getDependencyProvider()
Gets the
DependencyProvider . |
java.lang.String |
getId()
Gets the identifier of this
DependencyScope . |
DependencyScope |
getMockedScope()
Gets the
DependencyScope that overrides this DependencyScope . |
DependencyScopeOwner |
getOwner()
Get the
DependencyScopeOwner that manages the lifecycle this DependencyScope . |
DependencyScope |
getParentScope()
Gets the parent
DependencyScope . |
boolean |
hasDependency(java.lang.Class<?> dependencyType)
Tests if an instance of the specified dependency type exists in this
DependencyScope . |
boolean |
hasDependency(java.lang.Object dependency)
Tests if given dependency exists in this
DependencyScope . |
void |
initialize()
Invoked to initialize this
DependencyScope . |
boolean |
isAppScope()
Tests if this
DependencyScope is a application scoped DependencyScope . |
boolean |
isDisposable()
Tests if this
DependencyScope can be disposed. |
void |
onActivated(DependencyScopeOwner owner)
This method is invoked by
Dependency when this DependencyScope is activated
for the given DependencyScopeOwner . |
void |
onDeactivated(DependencyScopeOwner owner)
This method is invoked by
Dependency when this DependencyScope is deactivated
for the given DependencyScopeOwner . |
protected void |
removeDependant(java.lang.Object dependant)
Removes the given
Object as a dependant from this DependencyScope . |
java.util.List<java.lang.Class<?>> |
removeDependency(java.lang.Object dependency)
Removes the given dependency from this
DependencyScope . |
void |
setMockedScope(DependencyScope scope)
Sets the given
DependencyScope to override this DependencyScope . |
void |
setOwner(DependencyScopeOwner owner)
Set the
DependencyScopeOwner that manages the lifecycle this DependencyScope . |
void |
setParentScope(DependencyScope parent)
Sets the parent
DependencyScope . |
protected boolean |
type(java.lang.Class<?> dependencyType)
Tests if the specified type represents the requested dependency type.
|
protected final java.util.ArrayList<java.lang.Object> dependants
protected final java.util.HashMap<java.lang.Class<?>,java.util.ArrayList<java.lang.Object>> dependencies
protected DependencyScope mockScope
DependencyScope
used providing mock dependencies.protected DependencyScope parentScope
protected DependencyProvider dependencyProvider
protected boolean initialized
protected DependencyProvider getDependencyProvider()
DependencyProvider
. A DependencyProvider
is either code generated
by the annotation processor of Fuusio API Compiler or explicitly created by method
createDependencyProvider()
.DependencyProvider
. May return null
.public java.lang.String getId()
DependencyScope
. The default identifier is the canonical
class name of the DependencyScope
.String
.public final DependencyScopeOwner getOwner()
DependencyScopeOwner
that manages the lifecycle this DependencyScope
.DependencyScopeOwner
.public void setOwner(DependencyScopeOwner owner)
DependencyScopeOwner
that manages the lifecycle this DependencyScope
.owner
- A DependencyScopeOwner
.public final DependencyScope getParentScope()
DependencyScope
.DependencyScope
. May return null
if the parent is not set.public void setParentScope(DependencyScope parent)
DependencyScope
.parent
- A DependencyScope
. May be null
.public final DependencyScope getMockedScope()
DependencyScope
that overrides this DependencyScope
. An overriding
DependencyScope
can be used for providing mock dependencies for unit tests.DependencyScope
. May return null
.public void setMockedScope(DependencyScope scope)
DependencyScope
to override this DependencyScope
. An overriding
DependencyScope
can be used for providing mock dependencies for unit tests.scope
- A DependencyScope
.public boolean isDisposable()
DependencyScope
can be disposed.boolean
value.public void addDependant(java.lang.Object dependant)
Object
as a dependant to this DependencyScope
.dependant
- An Object
.protected void removeDependant(java.lang.Object dependant)
Object
as a dependant from this DependencyScope
.dependant
- An Object
.protected final boolean type(java.lang.Class<?> dependencyType)
dependencyType
- A Class
specifying the type of the requested dependency.boolean
value.protected final <T> T dependency(java.lang.Object dependency)
Object
before it is returned to a requester.T
- The generic return type of the cached dependency.dependency
- The requested dependency Object
to be cached.Object
.protected <T> T cache(java.lang.Class<T> dependencyType, java.lang.Object dependency)
Object
using the requested type as a key.T
- The generic return type of the cached dependency.dependencyType
- The dependence type as a Class
used as a key.dependency
- The requested dependency Object
to be cached.Object
.protected <T> T getDependency()
DependencyScope
.
The requested dependency instance is returned by the implementation, if it is capable of providing
a such instance. The implementation of this method should not delegate the request to any other
DependencyScope
. If the implementation is not able to cache the requested instance,
the design contract is to return null
instead.T
- A type parameter for casting the requested instance to expected type.DependencyScope
implementation is
capable of providing such instance otherwise null
. This default implementation returns
null
.protected <T> T getDependency(java.lang.Class<T> dependencyType, java.lang.Object dependant, boolean createNew)
DependencyScope
. If a requested instance is not found the search
is delegated to parent DependencyScope
. As a last attempt,
the AppDependencyScope
is searched.T
- A type parameter for casting the requested instance to expected type.dependencyType
- A Class
specifying the type of the requested instance.dependant
- The requesting object. This parameter is required when the requesting dependant
is also a requested within the object graph represented by a DependencyScope
.createNew
- A boolean
parameter specifying if an instance of the requested
type should be created using reflection if an instance does not exists already.null
is returned it indicates an error in
the implementation of the DependencyScope
.protected <T> void getDependencies(DependencyQuery<T> query, java.lang.Object dependant)
DependencyScope
. If a requested instance is not found the search
is delegated to parent DependencyScope
. As a last attempt,
the AppDependencyScope
is searched.T
- A type parameter for casting the requested instance to expected type.query
- A DependencyQuery
that defines the context for finding dependencies and
a container for storing the found dependencies.dependant
- The requesting object. This parameter is required when the requesting dependant
is also a requested within the object graph represented by a DependencyScope
.protected void dispose()
DependencyScope
to support effective GC and to avoid memory leaks.public boolean isAppScope()
DependencyScope
is a application scoped DependencyScope
.boolean
,public void onActivated(DependencyScopeOwner owner)
Dependency
when this DependencyScope
is activated
for the given DependencyScopeOwner
.owner
- A DependencyScopeOwner
.public void onDeactivated(DependencyScopeOwner owner)
Dependency
when this DependencyScope
is deactivated
for the given DependencyScopeOwner
.owner
- A DependencyScopeOwner
.public void initialize()
DependencyScope
.protected DependencyProvider createDependencyProvider()
DependencyProvider
for this DependencyScope
. By default this
method returns null
. If this method returns a null
, then an instance of
a DependencyProvider
is attempted to instantiate dynamically.DependencyProvider
. The default implementation returns null
.public java.util.List<java.lang.Class<?>> removeDependency(java.lang.Object dependency)
DependencyScope
.dependency
- The dependency to be removed as an Object
.List
of removed dependency types for the given dependency.public boolean hasDependency(java.lang.Object dependency)
DependencyScope
.dependency
- The dependency as an Object
.public boolean hasDependency(java.lang.Class<?> dependencyType)
DependencyScope
.dependencyType
- A Class
specifying the dependency type.