Futures.transform not compatible with the chain, like RxJava, but you can still use it to configure Future , which are dependent on each other. Here is a concrete example:
final ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); final ListenableFuture<FileClass> fileFuture = service.submit(() -> fileDownloader.download()) final ListenableFuture<UnzippedFileClass> unzippedFileFuture = Futures.transform(fileFuture, //need to cast this lambda (Function<FileClass, UnzippedFileClass>) file -> fileUnzipper.unzip(file)); final ListenableFuture<Void> deletedFileFuture = Futures.transform(unzippedFileFuture, (Function<UnzippedFileClass, Void>) unzippedFile -> fileDeleter.delete(unzippedFile)); deletedFileFuture.get(); //or however you want to wait for the result
This example assumes fileDownloader.download() returns an instance of FileClass , fileUpzipper.unzip() returns UnzippedFileClass , etc. If fileDownloader.download() instead returns ListenableFuture<FileClass> , use AsyncFunction instead of Function .
This example also uses Java 8 lambdas for short. If you are not using Java 8, go to anonymous implementations of Function or AsyncFunction:
Futures.transform(fileFuture, new AsyncFunction<FileClass, UpzippedFileClass>() { @Override public ListenableFuture<UnzippedFileClass> apply(final FileClass input) throws Exception { return fileUnzipper.unzip(); } });
More information about transform here: http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/Futures.html#transform (scroll or search "transform" - deep binding is currently taking place)
source share