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

public class Task<V> extends CompletableFuture<V>
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
  • Field Details

  • Constructor Details

    • Task

      public Task()
  • Method Details

    • supply

      @NonNull public static <V> @NonNull Task<V> supply(@NonNull @NonNull Runnable runnable)
      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

      @NonNull public static <V> @NonNull Task<V> supply(@NonNull @NonNull Callable<V> supplier)
      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

      @NonNull public static <V> @NonNull Task<V> completedTask(@Nullable @Nullable Object result)
      Creates a new task that already has a defined result. If the given result is a throwable the task is completed exceptionally CompletableFuture.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

      public @UnknownNullability V 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 to task.getDef(null).
      Returns:
      the completed result of this task, null if any exception occurred.
    • getDef

      public @UnknownNullability V getDef(@Nullable V def)
      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

      public @UnknownNullability V get(long time, @NonNull @NonNull TimeUnit timeUnit, @Nullable V def)
      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.