• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.functional.producers.fluentfuture;
18 
19 import static com.google.common.util.concurrent.Futures.immediateFuture;
20 
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.util.concurrent.FluentFuture;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import dagger.BindsInstance;
25 import dagger.multibindings.ElementsIntoSet;
26 import dagger.multibindings.IntoSet;
27 import dagger.producers.ProducerModule;
28 import dagger.producers.Produces;
29 import dagger.producers.Production;
30 import dagger.producers.ProductionComponent;
31 import java.util.Set;
32 import java.util.concurrent.Executor;
33 
34 final class FluentFutures {
35   interface Dependency {
floatFuture()36     FluentFuture<Float> floatFuture();
37   }
38 
39   @ProducerModule
40   static final class Module {
41     @Produces
intFuture()42     static FluentFuture<Integer> intFuture() {
43       return FluentFuture.from(immediateFuture(5));
44     }
45 
46     @Produces
stringFuture(int i)47     static FluentFuture<String> stringFuture(int i) {
48       return FluentFuture.from(immediateFuture("hello"));
49     }
50 
51     @Produces
52     @IntoSet
doubleFuture(int i)53     static FluentFuture<Double> doubleFuture(int i) {
54       return FluentFuture.from(immediateFuture((double) i));
55     }
56 
57     @Produces
58     @IntoSet
dependencyInput(float f)59     static double dependencyInput(float f) {
60       return (double) f;
61     }
62 
63     @Produces
64     @ElementsIntoSet
setOfDoubleFutures(int i)65     static Set<FluentFuture<Double>> setOfDoubleFutures(int i) {
66       return ImmutableSet.of(
67           FluentFuture.from(immediateFuture((double) i + 1)),
68           FluentFuture.from(immediateFuture((double) i + 2)));
69     }
70   }
71 
72   @ProductionComponent(modules = Module.class, dependencies = Dependency.class)
73   interface Component {
string()74     ListenableFuture<String> string();
75 
setOfDouble()76     ListenableFuture<Set<Double>> setOfDouble();
77 
78     @ProductionComponent.Builder
79     interface Builder {
dependency(Dependency dependency)80       Builder dependency(Dependency dependency);
81 
82       @BindsInstance
executor(@roduction Executor executor)83       Builder executor(@Production Executor executor);
84 
build()85       Component build();
86     }
87   }
88 }
89