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