Class AbstractContext

java.lang.Object
net.algart.contexts.AbstractContext
All Implemented Interfaces:
Context
Direct Known Subclasses:
DefaultContext, SubContext

public abstract class AbstractContext extends Object implements Context

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

    Fields
    Modifier and Type
    Field
    Description
    protected final boolean
    The value of the corresponding argument of the constructor.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractContext(boolean useServiceLoader)
    Creates a new instance of this class.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T extends Context>
    T
    as(Class<T> contextClass)
    This implementation returns, when possible, the reference to this instance or to the service provider implementing the required class.
    boolean
    is(Class<? extends Context> contextClass)
    Returns true if this context class can be processed by as(Class) method.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • useServiceLoader

      protected final boolean useServiceLoader
      The value of the corresponding argument of the constructor.
  • Constructor Details

    • AbstractContext

      protected AbstractContext(boolean useServiceLoader)
      Creates a new instance of this class.
      Parameters:
      useServiceLoader - whether as(Class) and is(Class) methods should check all service providers for the Context interface to find a suitable provider, in a case when this instance does not implement the required interface (or extends the class) contextClass.
  • Method Details

    • as

      public <T extends Context> T as(Class<T> contextClass)
      This implementation returns, when possible, the reference to this instance or to the service provider implementing the required class. More precisely:
      1. 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;
      2. 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;
      3. 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 interface Context
      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 implements Context interface.
      UnsupportedContextException - if this context cannot serve the request.
      See Also:
    • is

      public boolean is(Class<? extends Context> contextClass)
      Returns true if this context class can be processed by as(Class) method. More precisely:
      1. if contextClass==null or contextClass is not an inheritor of Context interface, this method returns false;
      2. if this instance implements (extends) the required interface (class) contextClass, i.e. if contextClass.isAssignableFrom(thisInstance.getClass()), then this method returns true;
      3. 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;
      4. 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.

      Specified by:
      is in interface Context
      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: