If you want some type safety, you can combine the result of two different independent tasks using the EventBus from the Guava sister com.google.common.eventbus package
As an example, let's assume that one of you Futures returns Integer , and the other Double .
First, create a drive class (another name builder, collector, etc.) that you register as an event receiver with EventBus. As you can see, this is really a POJO, which will be an Integer and Double event
class Accumulator { Integer intResult; Double doubleResult; @Subscribe
Here is an implementation of a method that takes 2 futures and combines them into a battery.
final ListenableFuture< Integer > future1 = ...; final ListenableFuture< Double > future2 = ...; final ImmutableList< ListenableFuture< ? extends Object> > futures = ImmutableList.< ListenableFuture<? extends Object> >of( future1, future2 ); final ListenableFuture< Accumulator > resultFuture = Futures.transform(
Alexander Pogrebnyak
source share