xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/multibindings/MultibindingModule.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2015 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.multibindings;
18 
19 import dagger.Module;
20 import dagger.Provides;
21 import dagger.multibindings.ClassKey;
22 import dagger.multibindings.ElementsIntoSet;
23 import dagger.multibindings.IntKey;
24 import dagger.multibindings.IntoMap;
25 import dagger.multibindings.IntoSet;
26 import dagger.multibindings.LongKey;
27 import dagger.multibindings.StringKey;
28 import java.math.BigDecimal;
29 import java.math.BigInteger;
30 import java.util.Collection;
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.Set;
34 import javax.inject.Named;
35 import javax.inject.Provider;
36 
37 @Module
38 class MultibindingModule {
39   @Provides
40   @IntoMap
41   @StringKey("foo")
provideFooKey(@uppressWarnings"unused") double doubleDependency)42   static String provideFooKey(@SuppressWarnings("unused") double doubleDependency) {
43     return "foo value";
44   }
45 
46   @Provides
47   @IntoMap
48   @StringKey("bar")
provideBarKey()49   static String provideBarKey() {
50     return "bar value";
51   }
52 
53   @Provides
54   @IntoMap
55   @StringKey("foo")
provideFooArrayValue(@uppressWarnings"unused") double doubleDependency)56   static String[] provideFooArrayValue(@SuppressWarnings("unused") double doubleDependency) {
57     return new String[] {"foo1", "foo2"};
58   }
59 
60   @Provides
61   @IntoMap
62   @StringKey("bar")
provideBarArrayValue()63   static String[] provideBarArrayValue() {
64     return new String[] {"bar1", "bar2"};
65   }
66 
67   @Provides
68   @IntoSet
provideFiveToSet()69   static int provideFiveToSet() {
70     return 5;
71   }
72 
73   @Provides
74   @IntoSet
provideSixToSet()75   static int provideSixToSet() {
76     return 6;
77   }
78 
79   @Provides
80   @ElementsIntoSet
provideElementsIntoSet()81   static Set<Integer> provideElementsIntoSet() {
82     Set<Integer> set = new HashSet<>();
83     set.add(-101);
84     set.add(-102);
85     return set;
86   }
87 
88   @Provides
provideMapKeys(Map<String, Provider<String>> map)89   static Set<String> provideMapKeys(Map<String, Provider<String>> map) {
90     return map.keySet();
91   }
92 
93   @Provides
provideMapValues(Map<String, String> map)94   static Collection<String> provideMapValues(Map<String, String> map) {
95     return map.values();
96   }
97 
98   @Provides
99   @IntoMap
100   @NestedAnnotationContainer.NestedWrappedKey(Integer.class)
valueForInteger()101   static String valueForInteger() {
102     return "integer";
103   }
104 
105   @Provides
106   @IntoMap
107   @NestedAnnotationContainer.NestedWrappedKey(Long.class)
valueForLong()108   static String valueForLong() {
109     return "long";
110   }
111 
112   @Provides
113   @IntoMap
114   @ClassKey(Integer.class)
valueForClassInteger()115   static String valueForClassInteger() {
116     return "integer";
117   }
118 
119   @Provides
120   @IntoMap
121   @ClassKey(Long.class)
valueForClassLong()122   static String valueForClassLong() {
123     return "long";
124   }
125 
126   @Provides
127   @IntoMap
128   @NumberClassKey(BigDecimal.class)
valueForNumberClassBigDecimal()129   static String valueForNumberClassBigDecimal() {
130     return "bigdecimal";
131   }
132 
133   @Provides
134   @IntoMap
135   @NumberClassKey(BigInteger.class)
valueForNumberClassBigInteger()136   static String valueForNumberClassBigInteger() {
137     return "biginteger";
138   }
139 
140   @Provides
141   @IntoMap
142   @LongKey(100)
valueFor100Long()143   static String valueFor100Long() {
144     return "100 long";
145   }
146 
147   @Provides
148   @IntoMap
149   @IntKey(100)
valueFor100Int()150   static String valueFor100Int() {
151     return "100 int";
152   }
153 
154   @Provides
155   @IntoMap
156   @ShortKey(100)
valueFor100Short()157   static String valueFor100Short() {
158     return "100 short";
159   }
160 
161   @Provides
162   @IntoMap
163   @ByteKey(100)
valueFor100Byte()164   static String valueFor100Byte() {
165     return "100 byte";
166   }
167 
168   @Provides
169   @IntoMap
170   @BooleanKey(true)
valueForTrue()171   static String valueForTrue() {
172     return "true";
173   }
174 
175   @Provides
176   @IntoMap
177   @CharKey('a')
valueForA()178   static String valueForA() {
179     return "a char";
180   }
181 
182   @Provides
183   @IntoMap
184   @CharKey('\n')
valueForNewline()185   static String valueForNewline() {
186     return "newline char";
187   }
188 
189   @Provides
190   @IntoMap
191   @UnwrappedAnnotationKey(@StringKey("foo\n"))
valueForUnwrappedAnnotationKeyFoo()192   static String valueForUnwrappedAnnotationKeyFoo() {
193     return "foo annotation";
194   }
195 
196   @Provides
197   @IntoMap
198   @WrappedAnnotationKey(
199     value = @StringKey("foo"),
200     integers = {1, 2, 3},
201     annotations = {},
202     classes = {Long.class, Integer.class}
203   )
valueForWrappedAnnotationKeyFoo()204   static String valueForWrappedAnnotationKeyFoo() {
205     return "wrapped foo annotation";
206   }
207 
208   @Provides
209   @IntoSet
210   @Named("complexQualifier")
valueForComplexQualifierSet()211   static String valueForComplexQualifierSet() {
212     return "foo";
213   }
214 
215   @Provides
216   @IntoSet
setContribution()217   static CharSequence setContribution() {
218     return "foo";
219   }
220 
221   @Provides
222   @IntoSet
223   @Named("complexQualifier")
qualifiedSetContribution()224   static CharSequence qualifiedSetContribution() {
225     return "qualified foo";
226   }
227 
228   @Provides
229   @IntoMap
230   @StringKey("key")
mapContribution()231   static CharSequence mapContribution() {
232     return "foo value";
233   }
234 
235   @Provides
236   @IntoMap
237   @Named("complexQualifier")
238   @StringKey("key")
qualifiedMapContribution()239   static CharSequence qualifiedMapContribution() {
240     return "qualified foo value";
241   }
242 }
243