Class AbstractContext
- All Implemented Interfaces:
Context
- Direct Known Subclasses:
DefaultContext
,SubContext
A skeletal implementation of the Context
interface to minimize
the effort required to implement this interface.
The behavior, provided by this implementation, is described in the comments
to as(Class)
and is(Class)
methods below.
The full set of
service providers
for Context
interface, used by these methods, is loaded once
when they are needed for these methods for the first time.
Though a documented tool for loading service providers was added since Java 1.6,
this technique works in earlier Java versions also.
If some exception occurs while loading the set of service providers
(for example, if some of the service providers, listed in
META-INF/services/net.algart.contexts.Context file,
have no public constructors without arguments), this fact is logged via
standard java.util.logging tools
with the SEVERE level.
The following logger is used for this purpose:
Logger.getLogger("net.algart.contexts.AbstractContext
").
In this case, the execution of the called as(Class)
or is(Class)
method is not interrupted,
but some (or all) service providers may be ignored in this and futher requests.
- Author:
- Daniel Alievsky
-
Field Summary
Modifier and TypeFieldDescriptionprotected final boolean
The value of the corresponding argument of theconstructor
. -
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractContext
(boolean useServiceLoader) Creates a new instance of this class. -
Method Summary
-
Field Details
-
useServiceLoader
protected final boolean useServiceLoaderThe value of the corresponding argument of theconstructor
.
-
-
Constructor Details
-
AbstractContext
protected AbstractContext(boolean useServiceLoader) Creates a new instance of this class.
-
-
Method Details
-
as
This implementation returns, when possible, the reference to this instance or to the service provider implementing the required class. More precisely:- if this instance implements (extends) the required interface (class) contextClass, i.e. if contextClass.isAssignableFrom(thisInstance.getClass()), then this method returns the reference to this instance;
- else, if there is at least one
service provider
for the
Context
interface (not for the passed contextClass!), listed in META-INF/services/net.algart.contexts.Context file, which implements (extends) the required interface (class) contextClass, then an instance of this service provider is returned; - else
UnsupportedContextException
is thrown.
The 2nd check is performed only if the useServiceLoader argument, passed to the constructor, was true. If it was false, the service providers are not loaded and not checked.
- Specified by:
as
in interfaceContext
- Parameters:
contextClass
- the class of returned object (or superclass, or implemented interface).- Returns:
- this instance, if it is suitable, or some service provider of
Context
interface that implements (extends) required contextClass. - Throws:
NullPointerException
- if contextClass is null.IllegalArgumentException
- if contextClass does not extends or implementsContext
interface.UnsupportedContextException
- if this context cannot serve the request.- See Also:
-
is
Returns true if this context class can be processed byas(Class)
method. More precisely:- if contextClass==null or contextClass is not an inheritor
of
Context
interface, this method returns false; - if this instance implements (extends) the required interface (class) contextClass, i.e. if contextClass.isAssignableFrom(thisInstance.getClass()), then this method returns true;
- else, if there is at least one
service provider
for the
Context
interface (not for the passed contextClass!), listed in META-INF/services/net.algart.contexts.Context file, which implements (extends) the required interface (class) contextClass, then this method returns true; - else this method returns false.
The 3rd check is performed only if the useServiceLoader argument, passed to the constructor, was true. If it was false, the service providers are not loaded and not checked.
- if contextClass==null or contextClass is not an inheritor
of
-