/* * Copyright (C) 2018 The Dagger Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package dagger.functional.producers.cancellation; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.truth.Truth.assertThat; import com.google.common.util.concurrent.ListenableFuture; import dagger.Provides; import dagger.producers.Producer; import dagger.producers.ProducerModule; import dagger.producers.Produces; import javax.inject.Named; @SuppressWarnings("unused") // not actually using dependencies @ProducerModule(subcomponents = CancellationSubcomponent.class) final class CancellationModule { private final ProducerTester tester; CancellationModule(ProducerTester tester) { this.tester = checkNotNull(tester); } @Produces @Named("leaf1") ListenableFuture produceLeaf1() { return tester.start("leaf1"); } @Produces @Named("leaf2") ListenableFuture produceLeaf2() { return tester.start("leaf2"); } @Produces @Named("leaf3") ListenableFuture produceLeaf3() { return tester.start("leaf3"); } @Produces @Named("foo") ListenableFuture produceFoo(@Named("leaf1") String leaf1, @Named("leaf2") String leaf2) { return tester.start("foo"); } @Produces @Named("bar") ListenableFuture produceBar(@Named("leaf2") String leaf2, @Named("leaf3") String leaf3) { return tester.start("bar"); } @Produces @Named("baz") ListenableFuture produceBaz( @Named("foo") Producer foo, @Named("bar") String bar) { ListenableFuture fooFuture = foo.get(); if (!fooFuture.isDone()) { assertThat(fooFuture.cancel(true)).isTrue(); assertThat(fooFuture.isCancelled()).isTrue(); } return tester.start("baz"); } @Provides @Named("providesDep") static String provideProvidesDep() { return "providesDep"; } @Produces @Named("qux") ListenableFuture produceQux( @Named("baz") String baz, @Named("providesDep") String providesDep) { return tester.start("qux"); } @Produces @Named("ep1") ListenableFuture produceEntryPoint1(@Named("qux") String qux) { return tester.start("entryPoint1"); } @Produces @Named("ep2") ListenableFuture produceEntryPoint2(@Named("bar") String bar, String dependency) { return tester.start("entryPoint2"); } @Produces @Named("ep3") static ListenableFuture produceEntryPoint3(Producer dependencyProducer) { ListenableFuture dependencyFuture = dependencyProducer.get(); assertThat(dependencyFuture.isDone()).isFalse(); assertThat(dependencyFuture.cancel(true)).isTrue(); assertThat(dependencyFuture.isCancelled()).isTrue(); return dependencyFuture; } }