Class SubtaskContext

java.lang.Object
net.algart.contexts.SubtaskContext
All Implemented Interfaces:
Context

public class SubtaskContext extends Object implements Context

A wrapper around the parent context, allowing to describe a subtask of some long-working task.

This wrapper delegates all calls of as and is methods to the parent context with the only exception, concerning the ProgressUpdater contexts. Namely, when we request ProgressUpdater from this wrapper via as(ProgressUpdater.class) call, the returned progress updater will transform the execution percents (passed to its updateProgress(readyPart,force) method) by the following formula:

fromPart + readyPart * (toPart - fromPart)

where fromPart and toPart are some real values in 0.0..1.0 range, passed to the constructor of this class. So, if we shall pass this wrapper to some method, that uses a context for showing its execution percents from 0% to 100%, then the source (parent) context will show the change of execution percents from fromPart*100% to toPart*100%. It can be very convenient, if the called method solves only a part of the full task, approximately fromPart*100%..toPart*100%.

More precisely, let sc is the instance of this class and parent is its parent context, and let fromPart and toPart are the constructor arguments. Then:

  • sc.is(contextClass) call is fully equivalent to parent.is(contextClass);
     
  • if contextClass!=ProgressUpdater.class, then sc.as(contextClass) call is equivalent to parent.as(contextClass), with the only difference that as method in the returned instance also "remebmers" about the subtask and works according the rules, specified here;
     
  • however, sc.as(ProgressUpdater.class) differs from parent.as(ProgressUpdater.class). Namely, let pu=parent.as(ProgressUpdater.class) (so, pu implements ProgressUpdater). Then sc.as(ProgressUpdater.class) returns an instance spu of some internal class, implementing ProgressUpdater, with the following behavior:
    • spu.is(contextClass) and spu.as(contextClass) are equivalent to sc.is(contextClass) and sc.as(contextClass);
    • spu.updateProgress(readyPart,force) calls pu.updateProgress(fromPart+readyPart*(toPart-fromPart),force).
    Note: if the parent context does not support ProgressUpdater (parent.as(ProgressUpdater.class) throws UnsupportedContextException), then this context sc does not support ProgressUpdater also (throws the same exception).

Warning: in an instance c, returned by as method of this class, the reference this inside its methods may differ from c reference! It is because as method of this class returns a proxy class, wrapping all method of the parent context.

Author:
Daniel Alievsky
  • Constructor Summary

    Constructors
    Constructor
    Description
    SubtaskContext(Context parentContext, double fromPart, double toPart)
    Creates new context on the base of the passed parentContext and fromPart / toPart values.
    SubtaskContext(Context parentContext, long from, long to, long total)
    Creates new context on the base of the passed parentContext and the range from/total*100%..to/total*100%, specified by integer values relatively some "total" number of operations.
  • Method Summary

    Modifier and Type
    Method
    Description
    final <T extends Context>
    T
    as(Class<T> contextClass)
    See the the detailed specification in the comments to this class.
    final boolean
    is(Class<? extends Context> contextClass)
    Fully equivalent to parentContext.is(contextClass), where parentContext is the constructor argument.
     

    Methods inherited from class java.lang.Object

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

    • SubtaskContext

      public SubtaskContext(Context parentContext, double fromPart, double toPart)
      Creates new context on the base of the passed parentContext and fromPart / toPart values.
      Parameters:
      parentContext - the parent context that will serve all methods of this class.
      fromPart - the estimated ready part of the total algorithm at the start of the subtask; must be in 0.0..1.0 range.
      toPart - the estimated ready part of the total algorithm at the finish of the subtask; must be in fromPart..1.0 range.
      Throws:
      NullPointerException - if the parentContext argument is null.
      IllegalArgumentException - if fromPart or toPart is not in 0.0..1.0 range or if fromPart>toPart.
      See Also:
    • SubtaskContext

      public SubtaskContext(Context parentContext, long from, long to, long total)
      Creates new context on the base of the passed parentContext and the range from/total*100%..to/total*100%, specified by integer values relatively some "total" number of operations. More precisely, equivalent to the following call:
       new SubtaskContext(parentContext,
           (double)from/(double)total,
           to==total ? 1.0: (double)to/(double)total)
       
      excepting the case from=to=total=0, when it is equivalent to
       new SubtaskContext(0.0, 1.0)
       
      Parameters:
      parentContext - the parent context that will serve all methods of this class.
      from - the estimated ready part, from 0 to total, of the total algorithm at the start of the subtask.
      to - the estimated ready part, from 0.0 to total, of the total algorithm at the finish of the subtask.
      total - the number of some operation in the full task.
      Throws:
      IllegalArgumentException - if from or to is not in 0..total range, or if from>to, or if total<0.
      See Also:
  • Method Details