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 childProgressReporterthat can be used to track sub-progress when tracked activity spans across concurrent processes.voidreportProgress(long progress) Accumulates the providedprogressand notifies.voidreset()Resets progress to zero and notifies.static ProgressReporterwithProgressListener(ProgressListener progressListener) Creates aProgressReporterthat notifiesProgressListener.
-
Method Details
-
withProgressListener
Creates aProgressReporterthat notifiesProgressListener.- Parameters:
progressListener- TheProgressListenerto be notified about progress. Must not be null.- Returns:
- The
ProgressReporterinstance. - Throws:
NullPointerException- IfprogressReceiveris null.
-
createChild
Creates childProgressReporterthat can be used to track sub-progress when tracked activity spans across concurrent processes. ChildProgressReporternotifies 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
ProgressReporterthen attachedProgressListeneris notified. Otherwise, already accumulated progress is subtracted from the parentProgressReporter's progress. -
reportProgress
public void reportProgress(long progress) Accumulates the providedprogressand notifies.If this is a root
ProgressReporterthen attachedProgressListeneris notified about accumulated progress. Otherwise, the providedprogressis reported to the parentProgressReporter.- Parameters:
progress- The number to be accumulated.
-