Partager via


ProgressReporter Class

  • java.lang.Object
    • com.azure.core.util.ProgressReporter

public final 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 Type Method and Description
ProgressReporter createChild()

Creates child ProgressReporter that can be used to track sub-progress when tracked activity spans across concurrent processes.

void reportProgress(long progress)

Accumulates the provided progress and notifies.

void reset()

Resets progress to zero and notifies.

static ProgressReporter withProgressListener(ProgressListener progressListener)

Creates a ProgressReporter that notifies ProgressListener.

Methods inherited from java.lang.Object

Method Details

createChild

public ProgressReporter createChild()

Creates child ProgressReporter that can be used to track sub-progress when tracked activity spans across concurrent processes. Child ProgressReporter notifies parent about progress and parent notifies ProgressListener.

Returns:

The child ProgressReporter.

reportProgress

public void reportProgress(long progress)

Accumulates the provided progress and notifies.

If this is a root ProgressReporter then attached ProgressListener is notified about accumulated progress. Otherwise, the provided progress is reported to the parent ProgressReporter.

Parameters:

progress - The number to be accumulated.

reset

public void reset()

Resets progress to zero and notifies.

If this is a root ProgressReporter then attached ProgressListener is notified. Otherwise, already accumulated progress is subtracted from the parent ProgressReporter's progress.

withProgressListener

public static ProgressReporter withProgressListener(ProgressListener progressListener)

Creates a ProgressReporter that notifies ProgressListener.

Parameters:

progressListener - The ProgressListener to be notified about progress. Must not be null.

Returns:

The ProgressReporter instance.

Applies to