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 interface
A subscriber exclusively interested in thecancellation
of the action, but in no other information.static interface
A subscriber exclusively interested in thefailure and the result
of the action, but in no other information.static interface
A subscriber exclusively interested inprogress updates
of the action, but in no other information.static interface
A subscriber exclusively interested in thesuccess and the result
of the action, but in no other information.static class
A subscriber that is interested in status updates of the action's progress. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract void
cancel()
Prematurely terminates the action.abstract @NonNull Optional
<Result<ResultType>> abstract void
registerListener
(@NonNull Task.TaskListener<ResultType> taskListener) Adds a subscriber that is interested in status updates of the action's progress.abstract boolean
removeListener
(@NonNull Task.TaskListener<ResultType> taskListener) Removes a subscriber that was previously added to receive status updates of the action's progress.abstract void
run()
Begins the execution of the action.void
setOnCancelled
(@NonNull Task.CancelListener cancelListener) Adds
aTask.TaskListener
that is exclusively interested in the prematureforceful termination
of the action.void
setOnFailed
(@NonNull Task.FailureListener<ResultType> failureListener) Adds
aTask.TaskListener
that is exclusively interested in the failure and the possible result of the action.void
setOnProgressUpdate
(@NonNull Task.ProgressListener progressListener) Adds
aTask.TaskListener
that is exclusively interested in progress updates of the action.void
setOnSucceeded
(@NonNull Task.SuccessListener<ResultType> successListener) Adds
aTask.TaskListener
that is exclusively interested in the success and the result of the action.
-
Constructor Details
-
Task
public Task()
-
-
Method Details
-
setOnProgressUpdate
Adds
aTask.TaskListener
that 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
Adds
aTask.TaskListener
that 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
Adds
aTask.TaskListener
that 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
Adds
aTask.TaskListener
that is exclusively interested in the prematureforceful termination
of 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:
true
if the subscriber was removed,false
if 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:
run
in 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.
-