Producers { public static <T> ListenableFuture<Produced<T>> createFutureProduced(ListenableFuture<T> future) { return Futures.withFallback( Futures.transform(future, new Function<T, Produced<T>>() { @Override public Produced<T> apply(final T value) { return new Produced<T>() { @Override public T get() { return value; } }; } }), Producers.<T>futureFallbackForProduced()); } private  Producers(); static ListenableFuture<Produced<T>> createFutureProduced(ListenableFuture<T> future); static ListenableFuture<Set<T>> createFutureSingletonSet(ListenableFuture<T> future); static ListenableFuture<T> submitToExecutor(Callable<T> callable, Executor executor); static Producer<T> producerFromProvider(final Provider<T> provider);  }
@Test public void createFutureProduced_success() throws Exception { ListenableFuture<String> future = Futures.immediateFuture("monkey"); ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); assertThat(producedFuture.isDone()).isTrue(); assertThat(producedFuture.get().get()).isEqualTo("monkey"); }
@Test public void createFutureProduced_failure() throws Exception { ListenableFuture<String> future = Futures.immediateFailedFuture(new RuntimeException("monkey")); ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); assertThat(producedFuture.isDone()).isTrue(); assertThat(getProducedException(producedFuture.get()).getCause()).hasMessage("monkey"); }
@Test public void createFutureProduced_cancelPropagatesBackwards() throws Exception { ListenableFuture<String> future = SettableFuture.create(); ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); assertThat(producedFuture.isDone()).isFalse(); producedFuture.cancel(false); assertThat(future.isCancelled()).isTrue(); }
@Test public void createFutureProduced_cancelDoesNotPropagateForwards() throws Exception { ListenableFuture<String> future = SettableFuture.create(); ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); assertThat(producedFuture.isDone()).isFalse(); future.cancel(false); assertThat(producedFuture.isCancelled()).isFalse(); assertThat(getProducedException(producedFuture.get()).getCause()) .isInstanceOf(CancellationException.class); }