Class KiwiFutures

java.lang.Object
org.kiwiproject.beta.concurrent.KiwiFutures

@Beta public final class KiwiFutures extends Object
Utilities related to Future.
  • Method Details

    • resultNow

      public static <V> V resultNow(Future<V> future)
      Returns the computed result, without waiting.
      Type Parameters:
      V - the result type of the future
      Parameters:
      future - the Future that should have completed with a result
      Returns:
      the result of the task
      Throws:
      IllegalArgumentException - if the future is null
      IllegalStateException - if the task has not completed, was interrupted, or did not complete with a result
      API Note:
      JDK 19 adds resultNow as an instance method in Future, at which point this method will no longer be needed. Code written for JDKs before 19 can use this method as a stand-in.
      Implementation Note:
      This implementation is mostly based on the JDK 19 code from Future#resultNow. The main difference is that this implementation removes the while loop and handles the InterruptedException by catching it, re-interrupting the current thread, and throwing an IllegalStateException. These changes were made because it seems that the JDK 19 implementation has an infinite loop, since it uses a "while (true)" loop which, when it catches InterruptedException, sets a boolean flag variable, but then allows execution to proceed. The code will then return to the top of the loop, and unless I am missing something, will encounter another InterruptedException when it calls get on the Future, and repeat forever (or until the program is actually shut down). I suspect it is likely I am missing something as to why that loop exists in the JDK 19 implementation, but since I cannot explain it, that logic was removed from this implementation. See Future#resultNow for the JDK 19 implementation. If anyone happens upon this code and knows the reason why JDK 19 contains the loop, please create an issue or discussion in kiwi-beta.
    • exceptionNow

      public static <V> Throwable exceptionNow(Future<V> future)
      Returns the exception thrown by the task, without waiting.
      Type Parameters:
      V - the result type of the future
      Parameters:
      future - the Future that should have completed with an Exception
      Returns:
      the exception thrown by the task
      Throws:
      IllegalArgumentException - if the future is null
      IllegalStateException - if the task has not completed, was interrupted, completed normally, or was canceled
      API Note:
      JDK 19 adds exceptionNow as an instance method in Future, at which point this method will no longer be needed. Code written for JDKs before 19 can use this method as a stand-in.
      Implementation Note:
      See the implementation note in resultNow(Future). The same loop-based implementation exists in the JDK 19 Future#exceptionNow, so the same reasoning applies to this method (in terms of why this method does not contain the same loop logic). See Future#exceptionNow for the JDK 19 implementation.
    • state

      public static <V> KiwiFutures.FutureState state(Future<V> future)
      Returns the state of a Future.
      Type Parameters:
      V - the result type of the future
      Parameters:
      future - the Future to check
      Returns:
      the KiwiFutures.FutureState representing the Future's state
      Throws:
      IllegalArgumentException - if the future is null
      IllegalStateException - if the task was interrupted
      API Note:
      JDK 19 adds state as an instance method in Future, at which point this method will no longer be needed. Code written for JDKs before 19 can use this method as a stand-in.
      Implementation Note:
      See the implementation note in resultNow(Future). The same loop-based implementation exists in the JDK 19 Future#state, so the same reasoning applies to this method (in terms of why this method does not contain the same loop logic). See Future#state for the JDK 19 implementation.
    • isNotDone

      public static <V> boolean isNotDone(Future<V> future)
      Check whether a Future is done.

      This is a convenience method for those who prefer (like me) to read code like:

       var future = ...;
       if (isNotDone(future)) {
           // ...
       }
       
      instead of code like:
       if (!future.isDone()) {
           // ...
       }
       
      And since we can't add a isNotDone() method to Future, this is this best we can do in Java. And while Lombok has ExtensionMethod, it is experimental so best to avoid.
      Type Parameters:
      V - the result type of the future
      Parameters:
      future - the Future to check
      Returns:
      true if the future is not done, and false if it is done (inverse of Future.isDone())