Package org.kiwiproject.beta.concurrent
Class KiwiFutures
java.lang.Object
org.kiwiproject.beta.concurrent.KiwiFutures
Utilities related to
Future.-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic <V> ThrowableexceptionNow(Future<V> future) Returns the exception thrown by the task, without waiting.static <V> booleanCheck whether aFutureis done.static <V> VReturns the computed result, without waiting.static <V> KiwiFutures.FutureStateReturns the state of aFuture.
-
Method Details
-
resultNow
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 nullIllegalStateException- if the task has not completed, was interrupted, or did not complete with a result- API Note:
- JDK 19 adds
resultNowas an instance method inFuture, 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 thewhileloop 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 callsgeton 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
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 nullIllegalStateException- if the task has not completed, was interrupted, completed normally, or was canceled- API Note:
- JDK 19 adds
exceptionNowas an instance method inFuture, 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 19Future#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
Returns the state of aFuture.- Type Parameters:
V- the result type of the future- Parameters:
future- the Future to check- Returns:
- the
KiwiFutures.FutureStaterepresenting the Future's state - Throws:
IllegalArgumentException- if the future is nullIllegalStateException- if the task was interrupted- API Note:
- JDK 19 adds
stateas an instance method inFuture, 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 19Future#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
Check whether aFutureis 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 aisNotDone()method to Future, this is this best we can do in Java. And while Lombok hasExtensionMethod, 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())
-