Class Task<V>
java.lang.Object
java.util.concurrent.CompletableFuture<V>
eu.cloudnetservice.common.concurrent.Task<V>
- Type Parameters:
V- the generic type of the value to complete.
- All Implemented Interfaces:
CompletionStage<V>,Future<V>
- Direct Known Subclasses:
CountingTask,ListenableTask
This task is a
Future which can be completed at any time in the future and provides some
useful shortcuts for completable futures.- Since:
- 4.0
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.CompletableFuture
CompletableFuture.AsynchronousCompletionTask -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncompletedTask(@Nullable Object result) Creates a new task that already has a defined result.This blocks the calling thread until the result of the task is available.This blocks the calling thread until the result of the task is available.This blocks the calling thread until the result of the task is available.Supplies and executes the given runnable in a cached thread pool and wraps it into a task.Supplies the given supplier into the cached thread pool.wrapFuture(@NonNull CompletableFuture<V> future) Wraps the given completable future into a cloudnet task.Methods inherited from class java.util.concurrent.CompletableFuture
acceptEither, acceptEitherAsync, acceptEitherAsync, allOf, anyOf, applyToEither, applyToEitherAsync, applyToEitherAsync, cancel, complete, completeAsync, completeAsync, completedFuture, completedStage, completeExceptionally, completeOnTimeout, copy, defaultExecutor, delayedExecutor, delayedExecutor, exceptionally, exceptionallyAsync, exceptionallyAsync, exceptionallyCompose, exceptionallyComposeAsync, exceptionallyComposeAsync, failedFuture, failedStage, get, get, getNow, getNumberOfDependents, handle, handleAsync, handleAsync, isCancelled, isCompletedExceptionally, isDone, join, minimalCompletionStage, newIncompleteFuture, obtrudeException, obtrudeValue, orTimeout, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, runAsync, runAsync, supplyAsync, supplyAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, toCompletableFuture, toString, whenComplete, whenCompleteAsync, whenCompleteAsync
-
Field Details
-
SERVICE
-
-
Constructor Details
-
Task
public Task()
-
-
Method Details
-
supply
Supplies and executes the given runnable in a cached thread pool and wraps it into a task.- Type Parameters:
V- the generic type of the task.- Parameters:
runnable- the runnable to run.- Returns:
- a new task containing the given runnable.
- Throws:
NullPointerException- if the given runnable is null.
-
supply
Supplies the given supplier into the cached thread pool. A new task is created and completed with the return value of the supplier. Thrown exceptions are caught and passed into the created task.- Type Parameters:
V- the generic type of the supplier and the task.- Parameters:
supplier- the supplier to execute in the thread pool.- Returns:
- the new task completing the value of the supplier.
- Throws:
NullPointerException- if the given supplier is null.
-
wrapFuture
@NonNull public static <V> @NonNull Task<V> wrapFuture(@NonNull @NonNull CompletableFuture<V> future) Wraps the given completable future into a cloudnet task. Exceptions thrown in the future are passed down to the task.- Type Parameters:
V- the generic type of the completable future and the new task.- Parameters:
future- the future to wrap as to a task.- Returns:
- the new task wrapping the completable future.
- Throws:
NullPointerException- if the given future is null.
-
completedTask
Creates a new task that already has a defined result. If the given result is a throwable the task is completed exceptionallyCompletableFuture.completeExceptionally(Throwable)instead of completing a normal result.- Type Parameters:
V- the generic type of the new task.- Parameters:
result- the result for the new task.- Returns:
- the new task completed with the given result.
-
getOrNull
This blocks the calling thread until the result of the task is available. If any exception occurs during the execution of the task or the task is cancelled null is returned as result. This method is equivalent totask.getDef(null).- Returns:
- the completed result of this task, null if any exception occurred.
-
getDef
This blocks the calling thread until the result of the task is available. If any exception occurs during the execution of the task or the task is cancelled the given default value is returned.- Parameters:
def- the default value returned on failure.- Returns:
- the completed result of this task or the default value if any exception occurred.
-
get
This blocks the calling thread until the result of the task is available. If any exception occurs during the execution of the task or the task is cancelled the given default value is returned. This will also return the default value if the task did not complete the result within the given time-out.- Parameters:
time- the time to wait for the completion of the result.timeUnit- the time unit of the time.def- the default value returned on failure or if the task did not complete within time-out.- Returns:
- the completed result of this task or the default value if the task did not complete.
- Throws:
NullPointerException- if the given time unit is null.
-