Package page.codeberg.friedolyn.util
Class Task<ResultType>
java.lang.Object
page.codeberg.friedolyn.util.Task<ResultType>
- Type Parameters:
ResultType- The class of the end product that the action executed generates.
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
Client.FetchPersonalData,ContactController.OpenContactReplyDialogTask,ContactController.SubmitTask,FriedolinClient.FetchClearname,FriedolinClient.FetchEmailAddress,FriedolinClient.Login,KeePassXC.AssociateAndLoginTask
Represents an action that is executed asynchronously and that can only be run once. Use a
Task.TaskListener to
subscribe to status updates of the action's progress.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA subscriber exclusively interested in thecancellationof the action, but in no other information.static interfaceA subscriber exclusively interested in thefailure and the resultof the action, but in no other information.static interfaceA subscriber exclusively interested inprogress updatesof the action, but in no other information.static interfaceA subscriber exclusively interested in thesuccess and the resultof the action, but in no other information.static classA subscriber that is interested in status updates of the action's progress. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidcancel()Prematurely terminates the action.abstract @NonNull Optional<Result<ResultType>> abstract voidregisterListener(@NonNull Task.TaskListener<ResultType> taskListener) Adds a subscriber that is interested in status updates of the action's progress.abstract booleanremoveListener(@NonNull Task.TaskListener<ResultType> taskListener) Removes a subscriber that was previously added to receive status updates of the action's progress.abstract voidrun()Begins the execution of the action.voidsetOnCancelled(@NonNull Task.CancelListener cancelListener) AddsaTask.TaskListenerthat is exclusively interested in the prematureforceful terminationof the action.voidsetOnFailed(@NonNull Task.FailureListener<ResultType> failureListener) AddsaTask.TaskListenerthat is exclusively interested in the failure and the possible result of the action.voidsetOnProgressUpdate(@NonNull Task.ProgressListener progressListener) AddsaTask.TaskListenerthat is exclusively interested in progress updates of the action.voidsetOnSucceeded(@NonNull Task.SuccessListener<ResultType> successListener) AddsaTask.TaskListenerthat is exclusively interested in the success and the result of the action.
-
Constructor Details
-
Task
public Task()
-
-
Method Details
-
setOnProgressUpdate
AddsaTask.TaskListenerthat is exclusively interested in progress updates of the action. Does NOT remove or replace any existing listeners.- Parameters:
progressListener- What to do when progress has been made executing the task. Will be called byTask.ProgressListener.onProgressUpdate(int, int).- See Also:
-
setOnSucceeded
AddsaTask.TaskListenerthat is exclusively interested in the success and the result of the action. Does NOT remove or replace any existing listeners.- Parameters:
successListener- What to do when the action has succeeded. Will be called byTask.SuccessListener.onSucceeded(Object).- See Also:
-
setOnFailed
AddsaTask.TaskListenerthat is exclusively interested in the failure and the possible result of the action. Does NOT remove or replace any existing listeners.- Parameters:
failureListener- What to do when the action has failed. Will be called byTask.FailureListener.onFailed(Optional).- See Also:
-
setOnCancelled
AddsaTask.TaskListenerthat is exclusively interested in the prematureforceful terminationof the action. Does NOT remove or replace any existing listeners.- Parameters:
cancelListener- What to do when the action has been cancelled. Will be called byTask.CancelListener.onCancelled().- See Also:
-
registerListener
Adds a subscriber that is interested in status updates of the action's progress. Does NOT remove or replace any existing listeners.- Parameters:
taskListener- The subscriber that wants to be informed about the progress of the action.- See Also:
-
removeListener
public abstract boolean removeListener(@NonNull @NonNull Task.TaskListener<ResultType> taskListener) Removes a subscriber that was previously added to receive status updates of the action's progress.- Parameters:
taskListener- The subscriber that no longer wants to be informed about the progress of the action. Must be the exact same reference that was previously registered.- Returns:
trueif the subscriber was removed,falseif it hadn't been registered in the first place.- See Also:
-
run
Begins the execution of the action. The action will run in the same thread that it was envoked from. Usecancel()to prematurely terminate the action. The end product of the action can be retrieved usinggetResult().- Specified by:
runin interfaceRunnable- Throws:
IllegalStateException- If the action had been started before (each task can only be run once).
-
cancel
Prematurely terminates the action. The action will be stopped as soon as possible, but it may not be immediate, depending on the implementation.- Throws:
IllegalStateException- If the action had not been started before or if it has already finished.
-
getResult
- Returns:
- The end product generated by the action. Empty if the action has not finished yet.
The presence of
Result.getResult()does NOT indicate success. OnlyResult.getSuccess()can be used to determine success or failure.
-