Class SubContext
- All Implemented Interfaces:
Context
A tool allowing to build a new context, called "sub-context", that inherits behavior of an existing context with some changes or extensions.
There are two basic ways to create a sub-context.
The first way is inheriting this class with using the protected constructor
SubContext(Context superContext)
.
New subclass cannot override as(Class)
and is(Class)
methods, but may (and should) implement some additional specific context interfaces
(for example, ProgressUpdater
, ArrayMemoryContext
or your custom contexts).
The as(Class)
and is(Class)
method of the created sub-context
will just call these methods of the super-context, excepting the case
when the required contextClass is an interface implemented by your sub-context class.
In the last case, as(Class)
method will return the reference to your sub-context instance
and is(Class)
will return true.
This technique is useful when you need to extend or override some
functionality of the given context.
The second way of creating sub-context is using the public constructor
SubContext(Context superContext, Class ...allowedClasses)
,
maybe, together with inheritance as in the first way.
In this case, as(Class)
and is(Class)
will also pass the request
to the super-context, as described above, if the required contextClass
is implemented by your sub-context class (in particular, if you do not extend
this class and just call the public constructor).
However, the contextClass will be checked, is it in the classes list
passed to the constructor. If contextClass is not in this list
(and is not implemented by your sub-context), it is considered as unallowed,
and the request is declined: as(Class)
throws an exception,
and is(Class)
method returns false.
This technique allows to restrict a set of passed contexts
by only well-known, safe contexts.
Another constructors may provide another behavior: please see comments to that constructors.
- Author:
- Daniel Alievsky
-
Field Summary
Fields inherited from class net.algart.contexts.AbstractContext
useServiceLoader
-
Constructor Summary
ModifierConstructorDescriptionprotected
SubContext
(Context superContext) Creates new context on the base of the passed super-context.SubContext
(Context superContext, Class<?>... allowedClasses) Creates new context on the base of the passed super-context with the restricted set of allowed context classes.SubContext
(Context superContext, MemoryModel memoryModel) Creates new context alike thebase constructor
with the only difference thatas(ArrayMemoryContext.class)
method will return aArrayMemoryContext
describing the passed memory model. -
Method Summary
Modifier and TypeMethodDescriptionfinal <T extends Context>
TThis implementation returns the reference to this instance, if contextClass.isAssignableFrom(thisInstance.getClass()), or calls superContext.as
(contextClass) in other case.final boolean
This implementation returns true if contextClass is not null and and contextClass.isAssignableFrom(thisInstance.getClass()).
-
Constructor Details
-
SubContext
Creates new context on the base of the passed super-context.- Parameters:
superContext
- super-context.
-
SubContext
Creates new context on the base of the passed super-context with the restricted set of allowed context classes.- Parameters:
superContext
- super-context.allowedClasses
- the set of served specific contexts (in addition to interfaces implemented by this instance).- Throws:
NullPointerException
- if superContext or one of allowedClasses is null.IllegalArgumentException
- if one of allowedClasses is not aContext
.
-
SubContext
Creates new context alike thebase constructor
with the only difference thatas(ArrayMemoryContext.class)
method will return aArrayMemoryContext
describing the passed memory model. See comments toas(Class)
andis(Class)
method for more details.- Parameters:
superContext
- super-context.memoryModel
- desired memory model.- Throws:
NullPointerException
- if one of the arguments is null.
-
-
Method Details
-
as
This implementation returns the reference to this instance, if contextClass.isAssignableFrom(thisInstance.getClass()), or calls superContext.as
(contextClass) in other case.If this instance was created by the constructor with the specified set of allowed classes (
SubContext(Context, Class[])
), and the condition contextClass.isAssignableFrom(thisInstance.getClass()) is not fulfilled, then context class is checked before passing to the superContext.as
method. Namely, if this class is not in the list of allowed contexts, passed to the constructor, this method throwsUnsupportedContextException
.If this instance was created by the constructor with the specified memory model (
SubContext(Context, MemoryModel)
), and if contextClass==ArrayMemoryContext.class, but !contextClass.isAssignableFrom(thisInstance.getClass()), then this method does not call superContext, but creates new implementation ofArrayMemoryContext
withgetMemoryModel()
,getMemoryModel(Class)
andgetMemoryModel(String)
methods, returning the memory model specified in the constructor. The secondgetMemoryModel(Class)
method will returnSimpleMemoryModel.getInstance()
, if the required element type is not supported by the memory model specified in the constructor.- Specified by:
as
in interfaceContext
- Overrides:
as
in classAbstractContext
- Parameters:
contextClass
- the class of returned object (or superclass, or implemented interface).- Returns:
- this instance.
- Throws:
NullPointerException
- if contextClass is null.IllegalArgumentException
- if contextClass does not extends or implementsContext
interface.UnsupportedContextException
- if this instance does not implement or extend the required type.- See Also:
-
is
This implementation returns true if contextClass is not null and and contextClass.isAssignableFrom(thisInstance.getClass()). In other case, if this instance was created by the constructor with the specified set of allowed classes (SubContext(Context, Class[])
) and if the passed context class is not in the list of allowed contexts, passed to the constructor, this implementation returns false.If this instance was created by the constructor with the specified memory model (
SubContext(Context, MemoryModel)
), and if contextClass==ArrayMemoryContext.class, this implementation returns true.In all other cases, this implementation returns superContext.
is
(contextClass).- Specified by:
is
in interfaceContext
- Overrides:
is
in classAbstractContext
- Parameters:
contextClass
- the class or interface of a sub-context.- Returns:
- true if this context class can be processed by
as(Class)
method. - See Also:
-