Class SubContext

All Implemented Interfaces:
Context

public class SubContext extends AbstractContext implements 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

    Constructors
    Modifier
    Constructor
    Description
    protected
    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 the base constructor with the only difference that as(ArrayMemoryContext.class) method will return a ArrayMemoryContext describing the passed memory model.
  • Method Summary

    Modifier and Type
    Method
    Description
    final <T extends Context>
    T
    as(Class<T> contextClass)
    This implementation returns the reference to this instance, if contextClass.isAssignableFrom(thisInstance.getClass()), or calls superContext.as(contextClass) in other case.
    final boolean
    is(Class<? extends Context> contextClass)
    This implementation returns true if contextClass is not null and and contextClass.isAssignableFrom(thisInstance.getClass()).

    Methods inherited from class java.lang.Object

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

    • SubContext

      protected SubContext(Context superContext)
      Creates new context on the base of the passed super-context.
      Parameters:
      superContext - super-context.
    • SubContext

      public SubContext(Context superContext, Class<?>... allowedClasses)
      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 a Context.
    • SubContext

      public SubContext(Context superContext, MemoryModel memoryModel)
      Creates new context alike the base constructor with the only difference that as(ArrayMemoryContext.class) method will return a ArrayMemoryContext describing the passed memory model. See comments to as(Class) and is(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

      public final <T extends Context> T as(Class<T> contextClass)
      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 throws UnsupportedContextException.

      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 of ArrayMemoryContext with getMemoryModel(), getMemoryModel(Class) and getMemoryModel(String) methods, returning the memory model specified in the constructor. The second getMemoryModel(Class) method will return SimpleMemoryModel.getInstance(), if the required element type is not supported by the memory model specified in the constructor.

      Specified by:
      as in interface Context
      Overrides:
      as in class AbstractContext
      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 implements Context interface.
      UnsupportedContextException - if this instance does not implement or extend the required type.
      See Also:
    • is

      public final boolean is(Class<? extends Context> contextClass)
      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 interface Context
      Overrides:
      is in class AbstractContext
      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: