Class AggregateFuture<InputT,OutputT>
- java.lang.Object
-
- com.google.common.util.concurrent.internal.InternalFutureFailureAccess
-
- com.google.common.util.concurrent.AbstractFuture<V>
-
- com.google.common.util.concurrent.AbstractFuture.TrustedFuture<OutputT>
-
- com.google.common.util.concurrent.AggregateFutureState<OutputT>
-
- com.google.common.util.concurrent.AggregateFuture<InputT,OutputT>
-
- Type Parameters:
InputT- the type of the individual inputsOutputT- the type of the output (i.e. this) future
- All Implemented Interfaces:
AbstractFuture.Trusted<OutputT>,ListenableFuture<OutputT>,java.util.concurrent.Future<OutputT>
- Direct Known Subclasses:
CollectionFuture,CombinedFuture
abstract class AggregateFuture<InputT,OutputT> extends AggregateFutureState<OutputT>
A future whose value is derived from a collection of input futures.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classAggregateFuture.ReleaseResourcesReason-
Nested classes/interfaces inherited from class com.google.common.util.concurrent.AbstractFuture
AbstractFuture.Trusted<V>, AbstractFuture.TrustedFuture<V>
-
-
Field Summary
Fields Modifier and Type Field Description private booleanallMustSucceedprivate booleancollectsValuesprivate ImmutableCollection<? extends ListenableFuture<? extends InputT>>futuresThe input futures.private static java.util.logging.Loggerlogger
-
Constructor Summary
Constructors Constructor Description AggregateFuture(ImmutableCollection<? extends ListenableFuture<? extends InputT>> futures, boolean allMustSucceed, boolean collectsValues)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private static booleanaddCausalChain(java.util.Set<java.lang.Throwable> seen, java.lang.Throwable t)Adds the chain to the seen set, and returns whether all the chain was new to us.(package private) voidaddInitialException(java.util.Set<java.lang.Throwable> seen)Populatesseenwith the exception that was passed tosetException.protected voidafterDone()Callback method that is called exactly once after the future is completed.(package private) abstract voidcollectOneValue(int index, InputT returnValue)IfallMustSucceedis true, called as each future completes; otherwise, ifcollectsValuesis true, called for each future when all futures complete.private voidcollectValueFromNonCancelledFuture(int index, java.util.concurrent.Future<? extends InputT> future)Collects the result (success or failure) of one input future.private voiddecrementCountAndMaybeComplete(ImmutableCollection<? extends java.util.concurrent.Future<? extends InputT>> futuresIfNeedToCollectAtCompletion)(package private) abstract voidhandleAllCompleted()private voidhandleException(java.lang.Throwable throwable)Fails this future with the given Throwable ifallMustSucceedis true.(package private) voidinit()Must be called at the end of each subclass's constructor.private static voidlog(java.lang.Throwable throwable)protected java.lang.StringpendingToString()Provide a human-readable explanation of why this future has not yet completed.private voidprocessCompleted(ImmutableCollection<? extends java.util.concurrent.Future<? extends InputT>> futuresIfNeedToCollectAtCompletion)(package private) voidreleaseResources(AggregateFuture.ReleaseResourcesReason reason)Clears fields that are no longer needed after this future has completed -- or at least all its inputs have completed (more precisely, afterhandleAllCompleted()has been called).-
Methods inherited from class com.google.common.util.concurrent.AggregateFutureState
clearSeenExceptions, decrementRemainingAndGet, getOrInitSeenExceptions
-
Methods inherited from class com.google.common.util.concurrent.AbstractFuture.TrustedFuture
addListener, cancel, get, get, isCancelled, isDone
-
Methods inherited from class com.google.common.util.concurrent.AbstractFuture
interruptTask, maybePropagateCancellationTo, set, setException, setFuture, toString, tryInternalFastPathGetFailure, wasInterrupted
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
futures
private ImmutableCollection<? extends ListenableFuture<? extends InputT>> futures
The input futures. Afterinit(), this field is read only byafterDone()(to propagate cancellation) andAbstractFuture.toString(). To access the futures' values,AggregateFutureattaches listeners that hold references to one or more inputs. And in the case ofCombinedFuture, the user-supplied callback usually has its own references to inputs.
-
allMustSucceed
private final boolean allMustSucceed
-
collectsValues
private final boolean collectsValues
-
-
Constructor Detail
-
AggregateFuture
AggregateFuture(ImmutableCollection<? extends ListenableFuture<? extends InputT>> futures, boolean allMustSucceed, boolean collectsValues)
-
-
Method Detail
-
afterDone
protected final void afterDone()
Description copied from class:AbstractFutureCallback method that is called exactly once after the future is completed.If
AbstractFuture.interruptTask()is also run during completion,AbstractFuture.afterDone()runs after it.The default implementation of this method in
AbstractFuturedoes nothing. This is intended for very lightweight cleanup work, for example, timing statistics or clearing fields. If your task does anything heavier consider, just using a listener with an executor.- Overrides:
afterDonein classAbstractFuture<OutputT>
-
pendingToString
protected final java.lang.String pendingToString()
Description copied from class:AbstractFutureProvide a human-readable explanation of why this future has not yet completed.- Overrides:
pendingToStringin classAbstractFuture<OutputT>- Returns:
- null if an explanation cannot be provided (e.g. because the future is done).
-
init
final void init()
Must be called at the end of each subclass's constructor. This method performs the "real" initialization; we can't put this in the constructor because, in the case where futures are already complete, we would not initialize the subclass before callingcollectValueFromNonCancelledFuture(int, java.util.concurrent.Future<? extends InputT>). As this is called after the subclass is constructed, we're guaranteed to have properly initialized the subclass.
-
handleException
private void handleException(java.lang.Throwable throwable)
Fails this future with the given Throwable ifallMustSucceedis true. Also, logs the throwable if it is anErroror ifallMustSucceedistrue, the throwable did not cause this future to fail, and it is the first time we've seen that particular Throwable.
-
log
private static void log(java.lang.Throwable throwable)
-
addInitialException
final void addInitialException(java.util.Set<java.lang.Throwable> seen)
Description copied from class:AggregateFutureStatePopulatesseenwith the exception that was passed tosetException.- Specified by:
addInitialExceptionin classAggregateFutureState<OutputT>
-
collectValueFromNonCancelledFuture
private void collectValueFromNonCancelledFuture(int index, java.util.concurrent.Future<? extends InputT> future)Collects the result (success or failure) of one input future. The input must not have been cancelled. For details on when this is called, seecollectOneValue(int, InputT).
-
decrementCountAndMaybeComplete
private void decrementCountAndMaybeComplete(ImmutableCollection<? extends java.util.concurrent.Future<? extends InputT>> futuresIfNeedToCollectAtCompletion)
-
processCompleted
private void processCompleted(ImmutableCollection<? extends java.util.concurrent.Future<? extends InputT>> futuresIfNeedToCollectAtCompletion)
-
releaseResources
void releaseResources(AggregateFuture.ReleaseResourcesReason reason)
Clears fields that are no longer needed after this future has completed -- or at least all its inputs have completed (more precisely, afterhandleAllCompleted()has been called). Often called multiple times (that is, both when the inputs complete and when the output completes).This is similar to our proposed
afterCommitmethod but not quite the same. See the description of CL 265462958.
-
collectOneValue
abstract void collectOneValue(int index, InputT returnValue)IfallMustSucceedis true, called as each future completes; otherwise, ifcollectsValuesis true, called for each future when all futures complete.
-
handleAllCompleted
abstract void handleAllCompleted()
-
addCausalChain
private static boolean addCausalChain(java.util.Set<java.lang.Throwable> seen, java.lang.Throwable t)Adds the chain to the seen set, and returns whether all the chain was new to us.
-
-