Class MoreFutures
- java.lang.Object
-
- org.apache.beam.sdk.util.MoreFutures
-
public class MoreFutures extends java.lang.Object
Utilities to do future programming with Java 8.Standards for these utilities:
- Always allow thrown exceptions, and they should cause futures to complete exceptionally.
- Always return
CompletionStage
as a future value. - Return
CompletableFuture
only to the producer of a future value.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MoreFutures.ExceptionOrResult<T>
An object that represents either a result or an exceptional termination.
-
Constructor Summary
Constructors Constructor Description MoreFutures()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.util.concurrent.CompletionStage<java.util.List<T>>
allAsList(java.util.Collection<? extends java.util.concurrent.CompletionStage<? extends T>> futures)
LikeCompletableFuture.allOf(java.util.concurrent.CompletableFuture<?>...)
but returning the result of constituent futures.static <T> java.util.concurrent.CompletionStage<java.util.List<MoreFutures.ExceptionOrResult<T>>>
allAsListWithExceptions(java.util.Collection<? extends java.util.concurrent.CompletionStage<? extends T>> futures)
LikeallAsList(java.util.Collection<? extends java.util.concurrent.CompletionStage<? extends T>>)
but return a list ofMoreFutures.ExceptionOrResult
of constituent futures.static <T> T
get(java.util.concurrent.CompletionStage<T> future)
Gets the result of the given future.static <T> T
get(java.util.concurrent.CompletionStage<T> future, long duration, java.util.concurrent.TimeUnit unit)
Gets the result of the given future.static boolean
isCancelled(java.util.concurrent.CompletionStage<?> future)
Indicates whether the future is cancelled.static boolean
isDone(java.util.concurrent.CompletionStage<?> future)
Indicates whether the future is done.static java.util.concurrent.CompletionStage<java.lang.Void>
runAsync(ThrowingRunnable runnable)
Shorthand forrunAsync(ThrowingRunnable, ExecutorService)
usingForkJoinPool.commonPool()
.static java.util.concurrent.CompletionStage<java.lang.Void>
runAsync(ThrowingRunnable runnable, java.util.concurrent.ExecutorService executorService)
LikeCompletableFuture.runAsync(java.lang.Runnable)
but forThrowingRunnable
.static <T> java.util.concurrent.CompletionStage<T>
supplyAsync(ThrowingSupplier<T> supplier)
Shorthand forsupplyAsync(ThrowingSupplier, ExecutorService)
usingForkJoinPool.commonPool()
.static <T> java.util.concurrent.CompletionStage<T>
supplyAsync(ThrowingSupplier<T> supplier, java.util.concurrent.ExecutorService executorService)
LikeCompletableFuture.supplyAsync(Supplier)
but forThrowingSupplier
.
-
-
-
Method Detail
-
get
public static <T> T get(java.util.concurrent.CompletionStage<T> future) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
Gets the result of the given future.This utility is provided so consumers of futures need not even convert to
CompletableFuture
, an interface that is only suitable for producers of futures.- Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
-
get
public static <T> T get(java.util.concurrent.CompletionStage<T> future, long duration, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
Gets the result of the given future.This utility is provided so consumers of futures need not even convert to
CompletableFuture
, an interface that is only suitable for producers of futures.- Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
-
isDone
public static boolean isDone(java.util.concurrent.CompletionStage<?> future)
Indicates whether the future is done.This utility is provided so consumers of futures need not even convert to
CompletableFuture
, an interface that is only suitable for producers of futures.
-
isCancelled
public static boolean isCancelled(java.util.concurrent.CompletionStage<?> future)
Indicates whether the future is cancelled.This utility is provided so consumers of futures need not even convert to
CompletableFuture
, an interface that is only suitable for producers of futures.
-
supplyAsync
public static <T> java.util.concurrent.CompletionStage<T> supplyAsync(ThrowingSupplier<T> supplier, java.util.concurrent.ExecutorService executorService)
LikeCompletableFuture.supplyAsync(Supplier)
but forThrowingSupplier
.If the
ThrowingSupplier
throws an exception, the future completes exceptionally.
-
supplyAsync
public static <T> java.util.concurrent.CompletionStage<T> supplyAsync(ThrowingSupplier<T> supplier)
Shorthand forsupplyAsync(ThrowingSupplier, ExecutorService)
usingForkJoinPool.commonPool()
.
-
runAsync
public static java.util.concurrent.CompletionStage<java.lang.Void> runAsync(ThrowingRunnable runnable, java.util.concurrent.ExecutorService executorService)
LikeCompletableFuture.runAsync(java.lang.Runnable)
but forThrowingRunnable
.If the
ThrowingRunnable
throws an exception, the future completes exceptionally.
-
runAsync
public static java.util.concurrent.CompletionStage<java.lang.Void> runAsync(ThrowingRunnable runnable)
Shorthand forrunAsync(ThrowingRunnable, ExecutorService)
usingForkJoinPool.commonPool()
.
-
allAsList
public static <T> java.util.concurrent.CompletionStage<java.util.List<T>> allAsList(java.util.Collection<? extends java.util.concurrent.CompletionStage<? extends T>> futures)
LikeCompletableFuture.allOf(java.util.concurrent.CompletableFuture<?>...)
but returning the result of constituent futures.
-
allAsListWithExceptions
public static <T> java.util.concurrent.CompletionStage<java.util.List<MoreFutures.ExceptionOrResult<T>>> allAsListWithExceptions(java.util.Collection<? extends java.util.concurrent.CompletionStage<? extends T>> futures)
LikeallAsList(java.util.Collection<? extends java.util.concurrent.CompletionStage<? extends T>>)
but return a list ofMoreFutures.ExceptionOrResult
of constituent futures.
-
-