• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.multibindings;
18 
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import dagger.functional.producers.multibindings.Qualifiers.EmptyButDeclaredInModuleAndProducerModule;
23 import dagger.functional.producers.multibindings.Qualifiers.ObjCount;
24 import dagger.functional.producers.multibindings.Qualifiers.PossiblyThrowingMap;
25 import dagger.functional.producers.multibindings.Qualifiers.PossiblyThrowingSet;
26 import dagger.multibindings.ElementsIntoSet;
27 import dagger.multibindings.IntKey;
28 import dagger.multibindings.IntoMap;
29 import dagger.multibindings.IntoSet;
30 import dagger.multibindings.Multibinds;
31 import dagger.producers.Produced;
32 import dagger.producers.ProducerModule;
33 import dagger.producers.Produces;
34 import java.util.Map;
35 import java.util.Set;
36 
37 @ProducerModule
38 abstract class MultibindingProducerModule {
39   @Produces
40   @IntoSet
futureStr()41   static ListenableFuture<String> futureStr() {
42     return Futures.immediateFuture("foo");
43   }
44 
45   @Produces
46   @IntoSet
str()47   static String str() {
48     return "bar";
49   }
50 
51   @Produces
52   @ElementsIntoSet
futureStrs()53   static ListenableFuture<Set<String>> futureStrs() {
54     return Futures.<Set<String>>immediateFuture(ImmutableSet.of("foo1", "foo2"));
55   }
56 
57   @Produces
58   @ElementsIntoSet
strFutures()59   static Set<ListenableFuture<String>> strFutures() {
60     return ImmutableSet.of(Futures.immediateFuture("baz1"), Futures.immediateFuture("baz2"));
61   }
62 
63   @Produces
64   @ElementsIntoSet
strs()65   static Set<String> strs() {
66     return ImmutableSet.of("bar1", "bar2");
67   }
68 
69   @Produces
strCount(Set<String> strs)70   static int strCount(Set<String> strs) {
71     return strs.size();
72   }
73 
74   @Produces
75   @IntoSet
76   @PossiblyThrowingSet
successfulStringForSet()77   static String successfulStringForSet() {
78     return "singleton";
79   }
80 
81   @Produces
82   @ElementsIntoSet
83   @PossiblyThrowingSet
successfulStringsForSet()84   static Set<String> successfulStringsForSet() {
85     return ImmutableSet.of("double", "ton");
86   }
87 
88   @Produces
89   @IntoSet
90   @PossiblyThrowingSet
throwingStringForSet()91   static String throwingStringForSet() {
92     throw new RuntimeException("monkey");
93   }
94 
95   @Produces
96   @IntoMap
97   @IntKey(42)
futureFor42()98   static ListenableFuture<String> futureFor42() {
99     return Futures.immediateFuture("forty two");
100   }
101 
102   @Produces
103   @IntoMap
104   @IntKey(15)
valueFor15()105   static String valueFor15() {
106     return "fifteen";
107   }
108 
109   @Produces
110   @IntoMap
111   @PossiblyThrowingMap
112   @IntKey(42)
successfulFutureFor42()113   static ListenableFuture<String> successfulFutureFor42() {
114     return Futures.immediateFuture("forty two");
115   }
116 
117   @Produces
118   @IntoMap
119   @PossiblyThrowingMap
120   @IntKey(15)
throwingValueFor15()121   static String throwingValueFor15() {
122     throw new RuntimeException("monkey");
123   }
124 
125   @Multibinds
objs()126   abstract Set<Object> objs();
127 
128   @Multibinds
objMap()129   abstract Map<Object, Object> objMap();
130 
131   @Produces
132   @ObjCount
objCount(Set<Produced<Object>> objs, Map<Object, Produced<Object>> objMap)133   static int objCount(Set<Produced<Object>> objs, Map<Object, Produced<Object>> objMap) {
134     return objs.size() + objMap.size();
135   }
136 
137   @Multibinds
138   @EmptyButDeclaredInModuleAndProducerModule
emptyButDeclaredInModuleAndProducerModule()139   abstract Map<String, Object> emptyButDeclaredInModuleAndProducerModule();
140 }
141