Class ClosingFuture.Combiner
- java.lang.Object
-
- com.google.common.util.concurrent.ClosingFuture.Combiner
-
- Direct Known Subclasses:
ClosingFuture.Combiner2
,ClosingFuture.Combiner3
,ClosingFuture.Combiner4
,ClosingFuture.Combiner5
- Enclosing class:
- ClosingFuture<V extends @Nullable java.lang.Object>
@DoNotMock("Use ClosingFuture.whenAllSucceed() or .whenAllComplete() instead.") public static class ClosingFuture.Combiner extends java.lang.Object
A builder of aClosingFuture
step that is derived from more than one input step.See
ClosingFuture.whenAllComplete(Iterable)
andClosingFuture.whenAllSucceed(Iterable)
for how to instantiate this class.Example:
final ClosingFuture<BufferedReader> file1ReaderFuture = ...; final ClosingFuture<BufferedReader> file2ReaderFuture = ...; ListenableFuture<Integer> numberOfDifferentLines = ClosingFuture.whenAllSucceed(file1ReaderFuture, file2ReaderFuture) .call( (closer, peeker) -> { BufferedReader file1Reader = peeker.getDone(file1ReaderFuture); BufferedReader file2Reader = peeker.getDone(file2ReaderFuture); return countDifferentLines(file1Reader, file2Reader); }, executor) .closing(executor);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ClosingFuture.Combiner.AsyncCombiningCallable<V extends @Nullable java.lang.Object>
An operation that returns aClosingFuture
result and may throw an exception.static interface
ClosingFuture.Combiner.CombiningCallable<V extends @Nullable java.lang.Object>
An operation that returns a result and may throw an exception.
-
Field Summary
Fields Modifier and Type Field Description protected ImmutableList<ClosingFuture<?>>
inputs
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <V extends @Nullable java.lang.Object>
ClosingFuture<V>call(ClosingFuture.Combiner.CombiningCallable<V> combiningCallable, java.util.concurrent.Executor executor)
Returns a newClosingFuture
pipeline step derived from the inputs by applying a combining function to their values.<V extends @Nullable java.lang.Object>
ClosingFuture<V>callAsync(ClosingFuture.Combiner.AsyncCombiningCallable<V> combiningCallable, java.util.concurrent.Executor executor)
Returns a newClosingFuture
pipeline step derived from the inputs by applying aClosingFuture
-returning function to their values.
-
-
-
Field Detail
-
inputs
protected final ImmutableList<ClosingFuture<?>> inputs
-
-
Method Detail
-
call
public <V extends @Nullable java.lang.Object> ClosingFuture<V> call(ClosingFuture.Combiner.CombiningCallable<V> combiningCallable, java.util.concurrent.Executor executor)
Returns a newClosingFuture
pipeline step derived from the inputs by applying a combining function to their values. The function can use aClosingFuture.DeferredCloser
to capture objects to be closed when the pipeline is done.If this combiner was returned by a
ClosingFuture.whenAllSucceed(java.lang.Iterable<? extends com.google.common.util.concurrent.ClosingFuture<?>>)
method and any of the inputs fail, so will the returned step.If the combiningCallable throws a
CancellationException
, the pipeline will be cancelled.If the combiningCallable throws an
ExecutionException
, the cause of the thrownExecutionException
will be extracted and used as the failure of the derived step.
-
callAsync
public <V extends @Nullable java.lang.Object> ClosingFuture<V> callAsync(ClosingFuture.Combiner.AsyncCombiningCallable<V> combiningCallable, java.util.concurrent.Executor executor)
Returns a newClosingFuture
pipeline step derived from the inputs by applying aClosingFuture
-returning function to their values. The function can use aClosingFuture.DeferredCloser
to capture objects to be closed when the pipeline is done (other than those captured by the returnedClosingFuture
).If this combiner was returned by a
ClosingFuture.whenAllSucceed(java.lang.Iterable<? extends com.google.common.util.concurrent.ClosingFuture<?>>)
method and any of the inputs fail, so will the returned step.If the combiningCallable throws a
CancellationException
, the pipeline will be cancelled.If the combiningCallable throws an
ExecutionException
, the cause of the thrownExecutionException
will be extracted and used as the failure of the derived step.If the combiningCallable throws any other exception, it will be used as the failure of the derived step.
If an exception is thrown after the combiningCallable creates a
ClosingFuture
, then none of the closeable objects in thatClosingFuture
will be closed.Usage guidelines for this method:
- Use this method only when calling an API that returns a
ListenableFuture
or aClosingFuture
. If possible, prefer callingcall(CombiningCallable, Executor)
instead, with a function that returns the next value directly. - Call
closer.eventuallyClose()
for every closeable object this step creates in order to capture it for later closing. - Return a
ClosingFuture
. To turn aListenableFuture
into aClosingFuture
callClosingFuture.from(ListenableFuture)
.
The same warnings about doing heavyweight operations within
ClosingFuture.transformAsync(AsyncClosingFunction, Executor)
apply here. - Use this method only when calling an API that returns a
-
-