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.binds; 18 19 import com.google.common.util.concurrent.MoreExecutors; 20 import dagger.Binds; 21 import dagger.Module; 22 import dagger.Provides; 23 import dagger.multibindings.ElementsIntoSet; 24 import dagger.multibindings.IntKey; 25 import dagger.multibindings.IntoMap; 26 import dagger.multibindings.IntoSet; 27 import dagger.producers.ProducerModule; 28 import dagger.producers.Produces; 29 import dagger.producers.Production; 30 import java.lang.annotation.Retention; 31 import java.lang.annotation.RetentionPolicy; 32 import java.util.Arrays; 33 import java.util.Collection; 34 import java.util.HashSet; 35 import java.util.Set; 36 import java.util.TreeSet; 37 import java.util.concurrent.Executor; 38 import javax.inject.Named; 39 import javax.inject.Qualifier; 40 41 @ProducerModule(includes = { 42 SimpleBindingModule.ExecutorModule.class, 43 SimpleBindingModule.ProvisionModuleForMap.class 44 }) 45 abstract class SimpleBindingModule { 46 @Binds bindObject(FooOfStrings impl)47 abstract Object bindObject(FooOfStrings impl); 48 49 @Binds bindFooOfStrings(FooOfStrings impl)50 abstract Foo<String> bindFooOfStrings(FooOfStrings impl); 51 52 @Binds bindFooOfNumbers(Foo<Integer> fooOfIntegers)53 abstract Foo<? extends Number> bindFooOfNumbers(Foo<Integer> fooOfIntegers); 54 55 @Binds 56 @SomeQualifier bindQualifiedFooOfStrings(FooOfStrings impl)57 abstract Foo<String> bindQualifiedFooOfStrings(FooOfStrings impl); 58 59 @Produces produceFooOfStrings()60 static FooOfStrings produceFooOfStrings() { 61 return new FooOfStrings(); 62 } 63 64 @Produces produceFooOfIntegers()65 static Foo<Integer> produceFooOfIntegers() { 66 return new Foo<Integer>() {}; 67 } 68 69 @Produces produceFooOfDoubles()70 static Foo<Double> produceFooOfDoubles() { 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 @Produces produceStringHashSet()91 static HashSet<String> produceStringHashSet() { 92 return new HashSet<>(Arrays.asList("hash-string1", "hash-string2")); 93 } 94 95 @Produces produceCharSequenceTreeSet()96 static TreeSet<CharSequence> produceCharSequenceTreeSet() { 97 return new TreeSet<CharSequence>(Arrays.asList("tree-charSequence1", "tree-charSequence2")); 98 } 99 100 @Produces produceCharSequenceCollection()101 static Collection<CharSequence> produceCharSequenceCollection() { 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 @Qualifier 118 @Retention(RetentionPolicy.RUNTIME) 119 @interface SomeQualifier {} 120 121 @Module 122 static final class ExecutorModule { 123 @Provides @Production provideExecutor()124 static Executor provideExecutor() { 125 return MoreExecutors.directExecutor(); 126 } 127 } 128 129 @Binds 130 @IntoMap 131 @IntKey(123) bind123ForMap(@amed"For-123") String string)132 abstract Object bind123ForMap(@Named("For-123") String string); 133 134 @Binds 135 @IntoMap 136 @IntKey(456) bind456ForMap(@amed"For-456") String string)137 abstract Object bind456ForMap(@Named("For-456") String string); 138 139 @Produces 140 @IntoMap 141 @IntKey(789) produce789ForMap()142 static Object produce789ForMap() { 143 return "789-string"; 144 } 145 146 @Module 147 abstract static class ProvisionModuleForMap { provideProvisionString()148 @Provides @Named("Provision string") static String provideProvisionString() { 149 return "provision-string"; 150 } 151 152 @Binds 153 @IntoMap 154 @IntKey(-1) bindNegative1ForMap(@amed"Provision string") String string)155 abstract Object bindNegative1ForMap(@Named("Provision string") String string); 156 } 157 158 @Binds 159 @IntoMap 160 @IntKey(123) 161 @SomeQualifier bindFooOfStringsIntoQualifiedMap(FooOfStrings fooOfStrings)162 abstract Object bindFooOfStringsIntoQualifiedMap(FooOfStrings fooOfStrings); 163 164 @Produces 165 @Named("For-123") produce123String()166 static String produce123String() { 167 return "123-string"; 168 } 169 170 @Produces 171 @Named("For-456") produce456String()172 static String produce456String() { 173 return "456-string"; 174 } 175 } 176