Interface ProgressWorker.Builder<T,V>

Type Parameters:
T - the worker result type
V - the intermediate result type
Enclosing class:
ProgressWorker<T,V>

public static sealed interface ProgressWorker.Builder<T,V>

Builds a ProgressWorker instance.

Each handler method can be called multiple times to add multiple handlers. All handlers are called in the order they were added. When using a task that implements a handler interface (e.g. ProgressWorker.TaskHandler), those handler methods are added first, and any handlers added via this builder are called after.

  • Method Details

    • onStarted

      ProgressWorker.Builder<T,V> onStarted(Runnable onStarted)
      Adds a handler called using the Dispatcher before background processing is started.
      Parameters:
      onStarted - the handler to add
      Returns:
      this builder instance
    • onDone

      Adds a handler called using the Dispatcher when the task is done running, successfully or not, or as soon as it is cancelled, before the result is processed.
      Parameters:
      onDone - the handler to add
      Returns:
      this builder instance
    • onWorking

      ProgressWorker.Builder<T,V> onWorking(Consumer<Boolean> onWorking)
      Adds a handler called using the Dispatcher with true before any onStarted(Runnable) handlers and with false after any onDone(Runnable) handlers, that is, before the result, exception or cancellation is handled, convenient for a state indicating that work is being performed: onWorking(working::set). Only called with false in case it was called with true, which is not the case for a task cancelled before it started.

      Note that this represents a single worker, a state shared by workers which may overlap is reset when the first one is done, the others still working.

      Parameters:
      onWorking - the handler to add
      Returns:
      this builder instance
    • onSuccess

      ProgressWorker.Builder<T,V> onSuccess(Runnable onSuccess)
      Adds a handler called using the Dispatcher after a successful task run, before any onResult(Consumer) handlers.
      Parameters:
      onSuccess - the handler to add
      Returns:
      this builder instance
    • onResult

      ProgressWorker.Builder<T,V> onResult(Consumer<T> onResult)
      Adds a handler called using the Dispatcher when the result of a successful run is available, after any onSuccess(Runnable) handlers.
      Parameters:
      onResult - the handler to add
      Returns:
      this builder instance
    • onProgress

      ProgressWorker.Builder<T,V> onProgress(Consumer<Integer> onProgress)
      Adds a handler called using the Dispatcher when progress is reported, unless the task has been cancelled.
      Parameters:
      onProgress - the handler to add
      Returns:
      this builder instance
    • onPublish

      ProgressWorker.Builder<T,V> onPublish(Consumer<List<V>> onPublish)
      Adds a handler called using the Dispatcher when chunks are available for publishing, unless the task has been cancelled.
      Parameters:
      onPublish - the handler to add
      Returns:
      this builder instance
    • onException

      ProgressWorker.Builder<T,V> onException(Consumer<Exception> onException)
      Adds a handler called using the Dispatcher if an exception occurred.

      An exception without any onException handler is rethrown on the dispatch thread.

      Parameters:
      onException - the handler to add
      Returns:
      this builder instance
    • onCancelled

      ProgressWorker.Builder<T,V> onCancelled(Runnable onCancelled)
      Adds a handler called using the Dispatcher if the background task is cancelled via ProgressWorker.cancel(boolean) or if it throws a CancelException or an InterruptedException.
      Parameters:
      onCancelled - the handler to add
      Returns:
      this builder instance
    • dispatcher

      ProgressWorker.Builder<T,V> dispatcher(Dispatcher dispatcher)
      Overrides the Dispatcher used to run the handlers, Dispatcher.instance() by default.

      Provided primarily for testing, Dispatcher.SYNCHRONOUS runs the handlers synchronously on the calling thread.

      Parameters:
      dispatcher - the dispatcher to use
      Returns:
      this builder instance
    • execute

      ProgressWorker<T,V> execute()
      Builds and executes a new ProgressWorker based on this builder
      Returns:
      a ProgressWorker based on this builder
    • build

      ProgressWorker<T,V> build()
      Returns:
      a ProgressWorker based on this builder