Package net.jalbum.util
Class Work<T>
java.lang.Object
net.jalbum.util.Work<T>
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 TypeMethodDescriptionAsk user a yes/no question.Execute arbitrary code at this point in the chain Allows for instance updating the UI prior to displaying the resultConvenience to filter stream on certain criteriaforEach
(BiOperation<T, Counter> op) Execute work for each item of passed data in the background.Wait until done or abortedExecute work for each item of passed data in the background.long
Present an ok/cancel confirmation dialog prior to workingboolean
boolean
millisToPopup
(int millisToPopup) static <T> Work<T>
on
(Collection<T> data) static <T> Work<T>
static <T> Work<T>
on
(T[] data) onDialogOpening
(Consumer<Dialog> onOpeningDialog) 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.Set the progress dialog's owning componentparallelism
(int parallelism) Report number of processed items and timing to consoleprintResult
(String template) Report number of processed items to consoleReport number of processed items and timing in a popup dialogshowResult
(String template) Report number of processed items in a popup dialogSupply a custom progress indicator.Set a title for this work.
-
Method Details
-
on
-
on
-
on
-
titled
Set a title for this work.- Parameters:
title
- Title for progress indicator dialog and to label the final undo/redo action
-
owner
Set the progress dialog's owning component- Parameters:
parentComponent
-
-
status
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
-
isCancelled
public boolean isCancelled()- Returns:
- true if user cancelled this work prior to its execution
-
filter
Convenience to filter stream on certain criteria- Parameters:
filter
-- Returns:
-
forEach
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
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
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
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
-
inform
Present an ok/cancel confirmation dialog prior to working- Parameters:
prompt
- Text or UI component to display
-
ask
Ask user a yes/no question. Yes continues with work- Parameters:
question
- Text or UI component to display- Returns:
-
showResult
Report number of processed items and timing in a popup dialog -
showResult
Report number of processed items in a popup dialog- Parameters:
template
- Custom text. Use {0} to refer to # of processed items
-
printResult
Report number of processed items and timing to console -
printResult
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
- Returns:
- Stopwatch object containing timing information
-
isAborted
public boolean isAborted() -
millisToPopup
- Parameters:
millisToPopup
- milliseconds to show progress dialog. Defaults to 500ms
-