Class Work<T>

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

public class Work<T>
extends java.lang.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 Summary

    Modifier and Type Method Description
    Work<T> ask​(java.lang.Object question)
    Ask user a yes/no question.
    Work<T> forEach​(Operation<T> op)
    Execute work for each item of passed data in the background.
    long getProcessed()  
    Work<T> inform​(java.lang.Object prompt)
    Present an ok/cancel confirmation dialog prior to working
    boolean isAborted()  
    boolean isCancelled()  
    Work<T> millisToPopup​(int millisToPopup)  
    static <T> Work<T> on​(java.util.Collection<T> data)  
    static <T> Work<T> on​(java.util.stream.Stream<T> data)  
    static <T> Work<T> on​(T[] data)  
    void onError​(java.util.function.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.
    Work<T> owner​(java.awt.Component parentComponent)
    Set the progress dialog's owning component
    Work<T> showResult()
    Report number of processed items in a popup dialog
    Work<T> showResult​(java.lang.String template)
    Report number of processed items in a popup dialog
    Work<T> status​(java.util.function.Function<T,​java.lang.String> messageSupplier)
    Supply a custom progress indicator.
    Work<T> titled​(java.lang.String title)
    Set a title for this work.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • on

      public static <T> Work<T> on​(java.util.Collection<T> data)
    • on

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

      public static <T> Work<T> on​(java.util.stream.Stream<T> data)
    • titled

      public Work<T> titled​(java.lang.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​(java.awt.Component parentComponent)
      Set the progress dialog's owning component
      Parameters:
      parentComponent -
    • status

      public Work<T> status​(java.util.function.Function<T,​java.lang.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
    • isCancelled

      public boolean isCancelled()
      Returns:
      true if user cancelled this work prior to its execution
    • 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
    • onError

      public void onError​(java.util.function.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
    • inform

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

      public Work<T> ask​(java.lang.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 in a popup dialog
    • showResult

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

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

      public boolean isAborted()
    • millisToPopup

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