Provide utility methods for Futures: getDone, checkSucceess
* `getDone` is an unchecked variant of Guava's `Futures#getDone`
* `checkSucceess` is equivalent to `getDone` but does not return value
and conveys intent better, if the caller is interested in just that
@@ -237,6 +237,26 @@ public final class MoreFutures
return Optional.empty();
}
+ /**
+ * Returns the result of the input {@link Future}, which must have already completed.
+ * <p>
+ * Similar to Futures{@link Futures#getDone(Future)}, but does not throw checked exceptions.
+ */
+ public static <T> T getDone(Future<T> future)
+ {
+ requireNonNull(future, "future is null");
+ checkArgument(future.isDone(), "future not done yet");
+ return getFutureValue(future);
+ }
+
+ /**
+ * Checks that the completed future completed successfully.
+ */
+ public static void checkSucceess(Future<?> future)
+ {
+ getDone(future);
+ }
+
/**
* Creates a future that completes when the first future completes either normally
* or exceptionally. Cancellation of the future propagates to the supplied futures.
