Class ProgressTracker
- All Implemented Interfaces:
AutoCloseable
Progress is quantified as a floating-point number in the
unit interval, i.e., between 0.0
and 1.0
,
both inclusive.
Progress is supposed, but not enforced, to be non-decreasing over time.
The progress of a task can be updated in either of two ways:
- Directly by setting to a specific value, if the task is conceptually atomic, or or
- indirectly by subdividing the task into a sequence of subtasks and assigning weights to those. Every subtask is issued with a local tracker, such that local progress values always use the same scale.
The progress of a whole task can be monitored actively by
repeatedly calling getProgress()
, or passively by passing a
hook function object that is called back on any change.
-
Constructor Summary
ConstructorDescriptionProgressTracker
(double resolution, @Opt DoubleConsumer hook) Creates a new tracker for a whole task. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Report that the tracked task is completed.double
Returns the current progress value.void
report
(double progress) Report that the tracked task has reached a specific progress value.void
reset()
subdivide
(double... weights) Creates an array of trackers for subtasks of arbitrary weights.subdivideEqually
(int n) Creates an array of trackers for subtasks of equal weight.
-
Constructor Details
-
ProgressTracker
Creates a new tracker for a whole task.A desired resolution can be specified, in order that there are not too many change events for highly (especially recursively) subdivided tasks: If the effective contribution of a subtask is less than or equal to the resolution, then its partial progress will not be propagated to its supertasks, only its completion. Set the resolution to
0.0
for maximum detail.- Parameters:
resolution
- the desired resolution of the tracking processhook
- the hook operation to execute whenever progress changes, ornull
if not applicable- Throws:
IllegalArgumentException
- ifresolution
is outside the unit interval
-
-
Method Details
-
subdivideEqually
Creates an array of trackers for subtasks of equal weight.It is assumed that the subtasks are performed in sequence: A report for the (i+1)-th subtracker, even with progress value
0.0
, implies that the i-th subtask has already been completed.- Parameters:
n
- the number of subtasks- Throws:
IllegalArgumentException
- ifn
is nonpositive
-
subdivide
Creates an array of trackers for subtasks of arbitrary weights.It is assumed that the subtasks are performed in sequence.
- Parameters:
weights
- the weights of all but the last subtask- Throws:
IllegalArgumentException
- if any element ofweights
is outside the unit interval, or if the sum ofweights
is greater than1.0
-
reset
public void reset() -
close
public void close()Report that the tracked task is completed.This method has the same effect as
report(double)
with an argument of1.0
.For consistency of monitoring, it is good practice to call this method eventually for any tracker that has been created, even if the accumulated progress of its subtasks may already sum to
1.0
.- Specified by:
close
in interfaceAutoCloseable
-
report
public void report(double progress) Report that the tracked task has reached a specific progress value.Progress will be propagated to the parent task, if there is one. Partial progress, i.e., values less than
1.0
are not propagated if below the resolution threshold.- Parameters:
progress
- the achieved progress value- Throws:
IllegalArgumentException
- ifprogress
is outside the unit interval
-
getProgress
public double getProgress()Returns the current progress value.
-