Class SubtaskContext
- All Implemented Interfaces:
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 thatas
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 implementsProgressUpdater
). Then sc.as
(ProgressUpdater.class) returns an instance spu of some internal class, implementingProgressUpdater
, 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).
ProgressUpdater
(parent.as(ProgressUpdater
.class) throwsUnsupportedContextException
), then this context sc does not supportProgressUpdater
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
ConstructorDescriptionSubtaskContext
(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 TypeMethodDescriptionfinal <T extends Context>
TSee the the detailed specification in the comments to this class.final boolean
Fully equivalent to parentContext.is(contextClass), where parentContext is the constructor argument.toString()
-
Constructor Details
-
SubtaskContext
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
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
excepting the case from=to=total=0, when it is equivalent toSubtaskContext
(parentContext, (double)from/(double)total, to==total ? 1.0: (double)to/(double)total)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
-
as
See the the detailed specification in the comments to this class.- Specified by:
as
in interfaceContext
- Parameters:
contextClass
- the class of returned object (or superclass, or implemented interface).- Returns:
- the required context.
- Throws:
NullPointerException
- if contextClass is null.IllegalArgumentException
- if contextClass does not extends or implementsContext
interface.UnsupportedContextException
- if the parent context cannot serve the request.- See Also:
-
is
Fully equivalent to parentContext.is(contextClass), where parentContext is the constructor argument. -
toString
-