Class Work<T>

java.lang.Object
net.jalbum.util.Work<T>

public class Work<T> extends Object
Manager of background data processing on any collection of objects (array, Collection or Stream). Work intends to unload the writing of a lot of the common "plumbing" code needed for user friendly data processing, leaving only a minimal amount of code to be written. A code reduction of 80% or more is not uncommon. Work has a fluent readable API that supports the following:
  • Initial confirmation dialog
  • Result dialog
  • Abortable popup progress indicator if work takes more than 500ms to complete (configurable)
  • Error reporting
  • Grouped undo/redo (if underlying work is undoable)
  • Parallel processing through the Streams API

Operation will be aborted if any exception is thrown. The default error reporter will present the name of the failing object and the exception itself, including a stack trace. In case you wish to abort a running work, throw an OperationAbortedException.

Java example: Simply print words of a string array, one word at a time:

 Work.on("This message will be printed one word at a time".split(" "))
 .titled("Print one word at a time")
 .ask("Do you really want each word to be printed?")
 .forEach(s -> {
 	System.out.println(s);
 })
 ;
Groovy and jAlbum example: Convert all album objects and their descendants to lower case:

 Work.on(rootFolder.descendants)
 .titled("Rename to lower case")
 .inform("This script converts the filenames of this project's objects to lower case")
 .forEach(ao) -> {
 	if (!ao.folder) {
 		ao.name = ao.name.toLowerCase();
 	}
 }
 .showResult();
 

For more code examples, see jAlbum's Groovy based external tools, located in the "tools" folder

Sample data parameters:
  • rootFolder.getDescendants - feeds all objects of the current project
  • selectedObjects - feeds the selected objects
  • TreeCollection.of(selectedObjects) - feeds selected objects AND their descendants
Since:
22
  • Method Details

    • on

      public static <T> Work<T> on(Collection<T> data)
    • on

      public static <T> Work<T> on(T[] data)
    • on

      public static <T> Work<T> on(Stream<T> data)
    • titled

      public Work<T> titled(String title)
      Set a title for this work.
      Parameters:
      title - Title for progress indicator dialog and to label the final undo/redo action
    • owner

      public Work<T> owner(Component parentComponent)
      Set the progress dialog's owning component
      Parameters:
      parentComponent -
    • status

      public Work<T> status(Function<T,String> messageSupplier)
      Supply a custom progress indicator. The default progress indicator prints .toString() on the currently processed object to the progress dialog
      Parameters:
      messageSupplier - Expression returning string to display on progress
    • parallelism

      public Work<T> parallelism(int parallelism)
    • isCancelled

      public boolean isCancelled()
      Returns:
      true if user cancelled this work prior to its execution
    • filter

      public Work<T> filter(Predicate<T> filter)
      Convenience to filter stream on certain criteria
      Parameters:
      filter -
      Returns:
    • forEach

      public Work<T> forEach(Operation<T> op)
      Execute work for each item of passed data in the background. Wait until done or aborted
      Parameters:
      op - lambda expression to be executed for each object of the passed data collection
    • forEach

      public Work<T> forEach(BiOperation<T,Counter> op)
      Execute work for each item of passed data in the background.Wait until done or aborted
      Parameters:
      op - lambda expression to be executed for each object of the passed data collection. The second parameter is an object counter
      Returns:
    • onError

      public void onError(Consumer<WorkException> errorHandler)
      By default, the 1:st error (Exception thrown) generates an error dialog but this behavior can be overridden by this error handler In any case, errors also cause abortion of the queue.
      Parameters:
      errorHandler - Handler being passed the exception that causes the error wrapped in a WorkException
    • execute

      public Work execute(Consumer<Work> code)
      Execute arbitrary code at this point in the chain Allows for instance updating the UI prior to displaying the result
      Parameters:
      code -
      Returns:
      Since:
      29.2
    • onDialogOpening

      public Work onDialogOpening(Consumer<Dialog> onOpeningDialog)
    • inform

      public Work<T> inform(Object prompt)
      Present an ok/cancel confirmation dialog prior to working
      Parameters:
      prompt - Text or UI component to display
    • ask

      public Work<T> ask(Object question)
      Ask user a yes/no question. Yes continues with work
      Parameters:
      question - Text or UI component to display
      Returns:
    • showResult

      public Work<T> showResult()
      Report number of processed items and timing in a popup dialog
    • showResult

      public Work<T> showResult(String template)
      Report number of processed items in a popup dialog
      Parameters:
      template - Custom text. Use {0} to refer to # of processed items
    • printResult

      public Work<T> printResult()
      Report number of processed items and timing to console
    • printResult

      public Work<T> printResult(String template)
      Report number of processed items to console
      Parameters:
      template - Custom text. Use {0} to refer to # of processed items
    • getProcessed

      public long getProcessed()
      Returns:
      number of processed items/objects
    • getTiming

      public Stopwatch getTiming()
      Returns:
      Stopwatch object containing timing information
    • isAborted

      public boolean isAborted()
    • millisToPopup

      public Work<T> millisToPopup(int millisToPopup)
      Parameters:
      millisToPopup - milliseconds to show progress dialog. Defaults to 500ms