Class ProgressReporter
ProgressReporter
offers a convenient way to add progress tracking to I/O operations.
The ProgressReporter
can be used to track a single operation as well as the progress of
complex operations that involve multiple sub-operations. In the latter case ProgressReporter
forms a tree where child nodes track the progress of sub-operations and report to the parent which in turn
aggregates the total progress. The reporting tree can have arbitrary level of nesting.
Code samples
/** * A simple operation that simulates I/O activity. * @param progressReporter The {@link ProgressReporter}. */ public static void simpleOperation(ProgressReporter progressReporter) { for (long i = 0; i < 100; i++) { // Simulate 100 I/Os with 10 progress. progressReporter.reportProgress(10); } } /** * A complex operation that simulates I/O activity by invoking multiple {@link #simpleOperation(ProgressReporter)}. * @param progressReporter The {@link ProgressReporter}. */ public static void complexOperation(ProgressReporter progressReporter) { simpleOperation(progressReporter.createChild()); simpleOperation(progressReporter.createChild()); simpleOperation(progressReporter.createChild()); } /** * The main method. * @param args Program arguments. */ public static void main(String[] args) { // Execute simpleOperation ProgressReporter simpleOperationProgressReporter = ProgressReporter .withProgressListener(progress -> System.out.println("Simple operation progress " + progress)); simpleOperation(simpleOperationProgressReporter); // Execute complexOperation ProgressReporter complexOperationProgressReporter = ProgressReporter .withProgressListener(progress -> System.out.println("Complex operation progress " + progress)); complexOperation(complexOperationProgressReporter); }
-
Method Summary
Modifier and TypeMethodDescriptionCreates childProgressReporter
that can be used to track sub-progress when tracked activity spans across concurrent processes.void
reportProgress
(long progress) Accumulates the providedprogress
and notifies.void
reset()
Resets progress to zero and notifies.static ProgressReporter
withProgressListener
(ProgressListener progressListener) Creates aProgressReporter
that notifiesProgressListener
.
-
Method Details
-
withProgressListener
Creates aProgressReporter
that notifiesProgressListener
.- Parameters:
progressListener
- TheProgressListener
to be notified about progress. Must not be null.- Returns:
- The
ProgressReporter
instance. - Throws:
NullPointerException
- IfprogressReceiver
is null.
-
createChild
Creates childProgressReporter
that can be used to track sub-progress when tracked activity spans across concurrent processes. ChildProgressReporter
notifies parent about progress and parent notifiesProgressListener
.- Returns:
- The child
ProgressReporter
.
-
reset
public void reset()Resets progress to zero and notifies.If this is a root
ProgressReporter
then attachedProgressListener
is notified. Otherwise, already accumulated progress is subtracted from the parentProgressReporter
's progress. -
reportProgress
public void reportProgress(long progress) Accumulates the providedprogress
and notifies.If this is a root
ProgressReporter
then attachedProgressListener
is notified about accumulated progress. Otherwise, the providedprogress
is reported to the parentProgressReporter
.- Parameters:
progress
- The number to be accumulated.
-