xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/binds/SimpleBindingModule.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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.binds;
18 
19 import dagger.Binds;
20 import dagger.Module;
21 import dagger.Provides;
22 import dagger.Reusable;
23 import dagger.multibindings.ElementsIntoSet;
24 import dagger.multibindings.IntKey;
25 import dagger.multibindings.IntoMap;
26 import dagger.multibindings.IntoSet;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.HashSet;
30 import java.util.Set;
31 import java.util.TreeSet;
32 import javax.inject.Named;
33 import javax.inject.Singleton;
34 
35 @Module(includes = InterfaceModule.class)
36 abstract class SimpleBindingModule {
37 
38   // Regression test for b/161853413 that binds an implementation that extends a generated class
39   // that is processed in the same build unit as the @Binds method.
40   @Binds
bindFooFactory(NeedsFactory.SomethingFactoryImpl impl)41   abstract NeedsFactory.SomethingFactory bindFooFactory(NeedsFactory.SomethingFactoryImpl impl);
42 
43   @Binds
bindObject(FooOfStrings impl)44   abstract Object bindObject(FooOfStrings impl);
45 
46   @Binds
47   @Reusable
48   @SomeQualifier
bindReusableObject(FooOfStrings impl)49   abstract Object bindReusableObject(FooOfStrings impl);
50 
51   @Binds
bindFooOfStrings(FooOfStrings impl)52   abstract Foo<String> bindFooOfStrings(FooOfStrings impl);
53 
54   @Binds
bindFooOfNumbers(Foo<Integer> fooOfIntegers)55   abstract Foo<? extends Number> bindFooOfNumbers(Foo<Integer> fooOfIntegers);
56 
57   @Binds
58   @Singleton
59   @SomeQualifier
bindQualifiedFooOfStrings(FooOfStrings impl)60   abstract Foo<String> bindQualifiedFooOfStrings(FooOfStrings impl);
61 
62   @Provides
provideFooOfIntegers()63   static Foo<Integer> provideFooOfIntegers() {
64     return new Foo<Integer>() {};
65   }
66 
67   @Provides
provideFooOfDoubles()68   static Foo<Double> provideFooOfDoubles() {
69     return new Foo<Double>() {};
70   }
71 
72   @Binds
73   @IntoSet
bindFooOfIntegersIntoSet(Foo<Integer> fooOfIntegers)74   abstract Foo<? extends Number> bindFooOfIntegersIntoSet(Foo<Integer> fooOfIntegers);
75 
76   @Binds
77   @IntoSet
bindFooExtendsNumberIntoSet(Foo<Double> fooOfDoubles)78   abstract Foo<? extends Number> bindFooExtendsNumberIntoSet(Foo<Double> fooOfDoubles);
79 
80   @Binds
81   @ElementsIntoSet
bindSetOfFooNumbersToObjects(Set<Foo<? extends Number>> setOfFooNumbers)82   abstract Set<Object> bindSetOfFooNumbersToObjects(Set<Foo<? extends Number>> setOfFooNumbers);
83 
84   @Binds
85   @IntoSet
bindFooOfStringsIntoSetOfObjects(FooOfStrings impl)86   abstract Object bindFooOfStringsIntoSetOfObjects(FooOfStrings impl);
87 
88   @Provides
provideStringHashSet()89   static HashSet<String> provideStringHashSet() {
90     return new HashSet<>(Arrays.asList("hash-string1", "hash-string2"));
91   }
92 
93   @Provides
provideCharSequenceTreeSet()94   static TreeSet<CharSequence> provideCharSequenceTreeSet() {
95     return new TreeSet<CharSequence>(Arrays.asList("tree-charSequence1", "tree-charSequence2"));
96   }
97 
98   @Provides
provideCharSequenceCollection()99   static Collection<CharSequence> provideCharSequenceCollection() {
100     return Arrays.<CharSequence>asList("list-charSequence");
101   }
102 
103   @Binds
104   @ElementsIntoSet
bindHashSetOfStrings(HashSet<String> set)105   abstract Set<CharSequence> bindHashSetOfStrings(HashSet<String> set);
106 
107   @Binds
108   @ElementsIntoSet
bindTreeSetOfCharSequences(TreeSet<CharSequence> set)109   abstract Set<CharSequence> bindTreeSetOfCharSequences(TreeSet<CharSequence> set);
110 
111   @Binds
112   @ElementsIntoSet
bindCollectionOfCharSequences(Collection<CharSequence> collection)113   abstract Set<CharSequence> bindCollectionOfCharSequences(Collection<CharSequence> collection);
114 
115   @Binds
116   @IntoMap
117   @IntKey(123)
bind123ForMap(@amed"For-123") String string)118   abstract Object bind123ForMap(@Named("For-123") String string);
119 
120   @Binds
121   @IntoMap
122   @IntKey(456)
bind456ForMap(@amed"For-456") String string)123   abstract Object bind456ForMap(@Named("For-456") String string);
124 
125   @Provides
126   @IntoMap
127   @IntKey(789)
provide789ForMap()128   static Object provide789ForMap() {
129     return "789-string";
130   }
131 
132   @Binds
133   @SomeQualifier
primitiveToPrimitive(int intValue)134   abstract int primitiveToPrimitive(int intValue);
135 
136   @Binds
137   @IntoSet
intValueIntoSet(int intValue)138   abstract int intValueIntoSet(int intValue);
139 
140   @Binds
141   @IntoMap
142   @IntKey(10)
intValueIntoMap(int intValue)143   abstract int intValueIntoMap(int intValue);
144 
145   @Provides
intValue()146   static int intValue() {
147     return 100;
148   }
149 
150   @Binds
151   @IntoMap
152   @IntKey(123)
153   @SomeQualifier
bindFooOfStringsIntoQualifiedMap(FooOfStrings fooOfStrings)154   abstract Object bindFooOfStringsIntoQualifiedMap(FooOfStrings fooOfStrings);
155 
156   @Provides
157   @Named("For-123")
provide123String()158   static String provide123String() {
159     return "123-string";
160   }
161 
162   @Provides
163   @Named("For-456")
provide456String()164   static String provide456String() {
165     return "456-string";
166   }
167 }
168