Top Description Inners Fields Constructors Methods
org.jabref.logic.util

public abstract Class BackgroundTask<V>

extends Object
Class Inheritance
Known Direct Subclasses
org.jabref.gui.exporter.WriteMetadataToLinkedPdfsAction.WriteMetaDataTask, org.jabref.gui.externalfiles.UnlinkedFilesCrawler, org.jabref.gui.linkedfile.DownloadLinkedFileAction.FileDownloadTask, org.jabref.logic.ai.ingestion.GenerateEmbeddingsForSeveralTask, org.jabref.logic.ai.ingestion.GenerateEmbeddingsTask, org.jabref.logic.ai.ingestion.model.UpdateEmbeddingModelTask, org.jabref.logic.ai.summarization.GenerateSummaryForSeveralTask, org.jabref.logic.ai.summarization.GenerateSummaryTask
Type Parameters
<V>
type of the return value of the task
Imports
java.util.concurrent.Callable, .Future, .TimeUnit, java.util.function.Consumer, .Function, javafx.beans.property.BooleanProperty, .DoubleProperty, .ObjectProperty, .SimpleBooleanProperty, .SimpleDoubleProperty, .SimpleObjectProperty, .SimpleStringProperty, .StringProperty, com.tobiasdiez.easybind.EasyBind, org.slf4j.Logger, .LoggerFactory

This class is essentially a wrapper around javafx.concurrent.Task. We cannot use javafx.concurrent.Task directly since it runs certain update notifications on the JavaFX thread, and so makes testing harder. We take the opportunity and implement a fluid interface.

A task created here is to be submitted to TaskExecutor#execute(BackgroundTask) to submit. This class is injected at @Inject TaskExecutor

Example (for using the fluent interface)

BackgroundTask
    .wrap(() -> ...)
    .showToUser(true)
    .onRunning(() -> ...)
    .onSuccess(() -> ...)
    .onFailure(() -> ...)
    .executeWith(taskExecutor);
Background: The task executor one takes care to show it in the UI. See org.jabref.gui.StateManager#addBackgroundTask(BackgroundTask, Task) for details.

Todo

Think of migrating to RxJava; CompletableFuture do not seem to support everything. If this is not possible, add an @implNote why.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static record

Field Summary

Modifier and TypeField and Description
private final BooleanProperty
private static final Logger
private final StringProperty
private Consumer<Exception>
private Runnable
private Runnable
private Consumer<V>
private final ObjectProperty<BackgroundTask.BackgroundProgress>
private final BooleanProperty
private final StringProperty
private final BooleanProperty
private final DoubleProperty

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract V
call()

public void
private static <T> Consumer<T>
chain(Runnable first, Consumer<T> second)

public Future<V>
executeWith(TaskExecutor taskExecutor)

public Consumer<Exception>
public Runnable
public Consumer<V>
protected BackgroundTask.BackgroundProgress
public double
public boolean
public BooleanProperty
public StringProperty
public BackgroundTask<V>
onFailure(Consumer<Exception> onException)

Sets the Consumer that is invoked after the task has failed with an exception.

public BackgroundTask<V>
onFinished(Runnable onFinished)

Sets the Runnable that is invoked after the task is finished, irrespectively if it was successful or failed with an error.

public BackgroundTask<V>
onRunning(Runnable onRunning)

Sets the Runnable that is invoked after the task is started.

public BackgroundTask<V>
onSuccess(Consumer<V> onSuccess)

Sets the Consumer that is invoked after the task is successfully finished.

public ObjectProperty<BackgroundTask.BackgroundProgress>
public Future<?>
scheduleWith(TaskExecutor taskExecutor, long delay, TimeUnit unit)

public BackgroundTask<V>
public boolean
public BackgroundTask<V>
showToUser(boolean show)

public BooleanProperty
public <
type of the return value of the second task
T
>
BackgroundTask<T>
then(Function<V, BackgroundTask<T>>
the function that creates the new task
nextTaskFactory
)

Creates a BackgroundTask that first runs this task and based on the result runs a second task.

public <
type of the return value of the second task
T
>
BackgroundTask<T>
thenRun(Function<V, T>
the function that performs the next operation
nextOperation
)

Creates a BackgroundTask that first runs this task and based on the result runs a second task.

public BackgroundTask<Void>
thenRun(Consumer<V>
the function that performs the next operation
nextOperation
)

Creates a BackgroundTask that first runs this task and based on the result runs a second task.

public StringProperty
public void
updateMessage(String newMessage)

protected void
public void
updateProgress(double workDone, double max)

public boolean
public BackgroundTask<V>
willBeRecoveredAutomatically(boolean willBeRecoveredAutomatically)

public BackgroundTask<V>
public DoubleProperty
public static <V> BackgroundTask<V>
wrap(Callable<V> callable)

public static BackgroundTask<Void>
wrap(Runnable runnable)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

isCancelledback to summary
private final BooleanProperty isCancelled
LOGGERback to summary
private static final Logger LOGGER
messageback to summary
private final StringProperty message
onExceptionback to summary
private Consumer<Exception> onException
onFinishedback to summary
private Runnable onFinished
onRunningback to summary
private Runnable onRunning
onSuccessback to summary
private Consumer<V> onSuccess
progressback to summary
private final ObjectProperty<BackgroundTask.BackgroundProgress> progress
showToUserback to summary
private final BooleanProperty showToUser
titleback to summary
private final StringProperty title
willBeRecoveredAutomaticallyback to summary
private final BooleanProperty willBeRecoveredAutomatically
workDonePercentageback to summary
private final DoubleProperty workDonePercentage

Constructor Detail

BackgroundTaskback to summary
public BackgroundTask()

Method Detail

callback to summary
public abstract V call() throws Exception
cancelback to summary
public void cancel()
chainback to summary
private static <T> Consumer<T> chain(Runnable first, Consumer<T> second)
executeWithback to summary
public Future<V> executeWith(TaskExecutor taskExecutor)
getOnExceptionback to summary
public Consumer<Exception> getOnException()
getOnRunningback to summary
public Runnable getOnRunning()
getOnSuccessback to summary
public Consumer<V> getOnSuccess()
getProgressback to summary
protected BackgroundTask.BackgroundProgress getProgress()
getWorkDonePercentageback to summary
public double getWorkDonePercentage()
isCancelledback to summary
public boolean isCancelled()
isCancelledPropertyback to summary
public BooleanProperty isCancelledProperty()
messagePropertyback to summary
public StringProperty messageProperty()
onFailureback to summary
public BackgroundTask<V> onFailure(Consumer<Exception> onException)

Sets the Consumer that is invoked after the task has failed with an exception. The consumer always runs on the JavaFX thread.

onFinishedback to summary
public BackgroundTask<V> onFinished(Runnable onFinished)

Sets the Runnable that is invoked after the task is finished, irrespectively if it was successful or failed with an error.

onRunningback to summary
public BackgroundTask<V> onRunning(Runnable onRunning)

Sets the Runnable that is invoked after the task is started.

onSuccessback to summary
public BackgroundTask<V> onSuccess(Consumer<V> onSuccess)

Sets the Consumer that is invoked after the task is successfully finished. The consumer always runs on the JavaFX thread.

progressPropertyback to summary
public ObjectProperty<BackgroundTask.BackgroundProgress> progressProperty()
scheduleWithback to summary
public Future<?> scheduleWith(TaskExecutor taskExecutor, long delay, TimeUnit unit)
setTitleback to summary
public BackgroundTask<V> setTitle(String title)
showToUserback to summary
public boolean showToUser()
showToUserback to summary
public BackgroundTask<V> showToUser(boolean show)
showToUserPropertyback to summary
public BooleanProperty showToUserProperty()
thenback to summary
public <T> BackgroundTask<T> then(Function<V, BackgroundTask<T>> nextTaskFactory)

Creates a BackgroundTask that first runs this task and based on the result runs a second task.

Parameters
<T>
type of the return value of the second task
nextTaskFactory:Function<V, BackgroundTask<T>>

the function that creates the new task

thenRunback to summary
public <T> BackgroundTask<T> thenRun(Function<V, T> nextOperation)

Creates a BackgroundTask that first runs this task and based on the result runs a second task.

Parameters
<T>
type of the return value of the second task
nextOperation:Function<V, T>

the function that performs the next operation

thenRunback to summary
public BackgroundTask<Void> thenRun(Consumer<V> nextOperation)

Creates a BackgroundTask that first runs this task and based on the result runs a second task.

Parameters
nextOperation:Consumer<V>

the function that performs the next operation

titlePropertyback to summary
public StringProperty titleProperty()
updateMessageback to summary
public void updateMessage(String newMessage)
updateProgressback to summary
protected void updateProgress(BackgroundTask.BackgroundProgress newProgress)
updateProgressback to summary
public void updateProgress(double workDone, double max)
willBeRecoveredAutomaticallyback to summary
public boolean willBeRecoveredAutomatically()
willBeRecoveredAutomaticallyback to summary
public BackgroundTask<V> willBeRecoveredAutomatically(boolean willBeRecoveredAutomatically)
withInitialMessageback to summary
public BackgroundTask<V> withInitialMessage(String message)
workDonePercentagePropertyback to summary
public DoubleProperty workDonePercentageProperty()
wrapback to summary
public static <V> BackgroundTask<V> wrap(Callable<V> callable)
wrapback to summary
public static BackgroundTask<Void> wrap(Runnable runnable)
org.jabref.logic.util back to summary

public final Record BackgroundTask.BackgroundProgress

extends Record
Class Inheritance
Record Components
workDone:double 
max:double 

Field Summary

Modifier and TypeField and Description
private final double
max

Record Component accessed by max().

private final double
workDone

Record Component accessed by workDone().

Constructor Summary

AccessConstructor and Description
public
BackgroundProgress(double workDone, double max)

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object
the reference object with which to compare.
o
)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public double
public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public double
max()

Record Component getter of max.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

public double
workDone()

Record Component getter of workDone.

Field Detail

maxback to summary
private final double max

Record Component accessed by max().

workDoneback to summary
private final double workDone

Record Component accessed by workDone().

Constructor Detail

BackgroundProgressback to summary
public BackgroundProgress(double workDone, double max)

Method Detail

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this record is equal to the argument; false otherwise.

getWorkDonePercentageback to summary
public double getWorkDonePercentage()
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

Returns:int

a hash code value for this record.

maxback to summary
public double max()

Record Component getter of max.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

Returns:String

a string representation of the object.

workDoneback to summary
public double workDone()

Record Component getter of workDone.