Class DynamicTask<T,P,R>
- java.lang.Object
-
- com.pranavpandey.android.dynamic.util.concurrent.DynamicRunnable<T,P,R>
-
- com.pranavpandey.android.dynamic.util.concurrent.DynamicTask<T,P,R>
-
- Type Parameters:
T- The type of the params.P- The type of the progress.R- The type of the result.
- All Implemented Interfaces:
java.lang.Runnable
- Direct Known Subclasses:
ContextTask,FileDeleteTask
public abstract class DynamicTask<T,P,R> extends DynamicRunnable<T,P,R>
Base class to representRunnableaccording to theDynamicRunnable.
-
-
Constructor Summary
Constructors Constructor Description DynamicTask()Constructor to initialize an object of this class.DynamicTask(android.os.Looper looper)Constructor to initialize an object of this class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancancel(boolean mayInterruptIfRunning)Attempts to cancel execution of this task.DynamicTask<T,P,R>execute()Executes the task with the default executor andnullparameters.DynamicTask<T,P,R>execute(T params)Executes the task with the supplied parameters.DynamicTask<T,P,R>executeOnExecutor(java.util.concurrent.Executor executor)Executes the task on the supplied executor.DynamicTask<T,P,R>executeOnExecutor(java.util.concurrent.Executor executor, T params)Executes the task on the supplied executor.voidfinish(DynamicResult<R> result)This method will be called to handle the result returned by the task.DynamicResult<R>get()Waits if necessary for the computation to complete, and then retrieves its result.DynamicResult<R>get(long timeout, java.util.concurrent.TimeUnit unit)Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.booleangetBooleanResult(DynamicResult<R> result)Returns the unboxedbooleanvalue from theBooleanresult.DynamicHandler<P,R>getHandler()Returns the thread handler used by this task.DynamicStatusgetStatus()Returns the current status of this task.booleanisCancelled()Returnstrueif this task was cancelled before it completed normally.DynamicResult<P>publishProgress(DynamicResult<P> progress)This method can be invoked fromDynamicRunnable.doInBackground(T)to publish updates on the UI thread while the background computation is still running.voidrun()-
Methods inherited from class com.pranavpandey.android.dynamic.util.concurrent.DynamicRunnable
doInBackground, onCancelled, onCancelled, onPostExecute, onPreExecute, onProgressUpdate
-
-
-
-
Method Detail
-
getHandler
@NonNull public DynamicHandler<P,R> getHandler()
Returns the thread handler used by this task.- Returns:
- The thread handler used by this task.
-
getBooleanResult
public boolean getBooleanResult(@Nullable DynamicResult<R> result)Returns the unboxedbooleanvalue from theBooleanresult.- Parameters:
result- TheBooleanresult to be unboxed.- Returns:
- The unboxed
booleanvalue from theBooleanresult.
-
getStatus
@NonNull public final DynamicStatus getStatus()
Returns the current status of this task.- Returns:
- The current status of this task.
-
get
@Nullable public final DynamicResult<R> get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
Waits if necessary for the computation to complete, and then retrieves its result.- Returns:
- The computed result.
- Throws:
java.util.concurrent.CancellationException- If the computation was cancelled.java.util.concurrent.ExecutionException- If the computation threw an exception.java.lang.InterruptedException- If the current thread was interrupted while waiting.
-
get
@Nullable public final DynamicResult<R> get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.- Parameters:
timeout- Time to wait before cancelling the operation.unit- The time unit for the timeout.- Returns:
- The computed result.
- Throws:
java.util.concurrent.CancellationException- If the computation was cancelled.java.util.concurrent.ExecutionException- If the computation threw an exception.java.lang.InterruptedException- If the current thread was interrupted while waiting.java.util.concurrent.TimeoutException- If the wait timed out.
-
run
public void run()
-
publishProgress
@Nullable public DynamicResult<P> publishProgress(@Nullable DynamicResult<P> progress)
Description copied from class:DynamicRunnableThis method can be invoked fromDynamicRunnable.doInBackground(T)to publish updates on the UI thread while the background computation is still running. Each call to this method will trigger the execution ofDynamicRunnable.onProgressUpdate(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<P>)on the UI thread.DynamicRunnable.onProgressUpdate(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<P>)will not be called if the task has been canceled.- Specified by:
publishProgressin classDynamicRunnable<T,P,R>- Parameters:
progress- The progress result to update the UI with.- Returns:
- The published result.
- See Also:
DynamicRunnable.onProgressUpdate(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<P>),DynamicRunnable.doInBackground(T)
-
finish
public void finish(@Nullable DynamicResult<R> result)Description copied from class:DynamicRunnableThis method will be called to handle the result returned by the task.- Specified by:
finishin classDynamicRunnable<T,P,R>- Parameters:
result- The result returned by the work.
-
isCancelled
public final boolean isCancelled()
Description copied from class:DynamicRunnableReturnstrueif this task was cancelled before it completed normally. If you are callingDynamicRunnable.cancel(boolean)on the task, the value returned by this method should be checked periodically fromDynamicRunnable.doInBackground(Object)to end the task as soon as possible.- Specified by:
isCancelledin classDynamicRunnable<T,P,R>- Returns:
trueif task was cancelled before it completed- See Also:
DynamicRunnable.cancel(boolean)
-
cancel
public final boolean cancel(boolean mayInterruptIfRunning)
Description copied from class:DynamicRunnableAttempts to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started whencancelis called, this task should never run. If the task has already started, then themayInterruptIfRunningparameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.Calling this method will result in
DynamicRunnable.onCancelled(DynamicResult)being invoked on the UI thread afterDynamicRunnable.doInBackground(Object)returns. Calling this method guarantees that onPostExecute(Object) is never subsequently invoked, even ifcancelreturns false, butDynamicRunnable.onPostExecute(com.pranavpandey.android.dynamic.util.concurrent.DynamicResult<R>)has not yet run. To finish the task as early as possible, checkDynamicRunnable.isCancelled()periodically fromDynamicRunnable.doInBackground(Object).This only requests cancellation. It never waits for a running background task to terminate, even if
mayInterruptIfRunningis true.- Specified by:
cancelin classDynamicRunnable<T,P,R>- Parameters:
mayInterruptIfRunning-trueif the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete.- Returns:
falseif the task could not be cancelled, typically because it has already completed normally;trueotherwise- See Also:
DynamicRunnable.isCancelled(),DynamicRunnable.onCancelled(DynamicResult)
-
executeOnExecutor
@MainThread @NonNull public final DynamicTask<T,P,R> executeOnExecutor(@NonNull java.util.concurrent.Executor executor, @Nullable T params)
Executes the task on the supplied executor.- Parameters:
executor- The executor to execute the task.params- The optional parameters for the task.- Returns:
- The instance of the executed task.
-
executeOnExecutor
@MainThread @NonNull public final DynamicTask<T,P,R> executeOnExecutor(@NonNull java.util.concurrent.Executor executor)
Executes the task on the supplied executor.- Parameters:
executor- The executor to execute the task.- Returns:
- The instance of the executed task.
-
execute
@MainThread @NonNull public final DynamicTask<T,P,R> execute(@Nullable T params)
Executes the task with the supplied parameters.- Parameters:
params- The parameters for the task.- Returns:
- The instance of the executed task.
- See Also:
executeOnExecutor(Executor, Object),DynamicConcurrent.getThreadPoolExecutor()
-
execute
@MainThread @NonNull public final DynamicTask<T,P,R> execute()
Executes the task with the default executor andnullparameters.- Returns:
- The instance of the executed task.
- See Also:
execute(Object),DynamicConcurrent.getThreadPoolExecutor()
-
-