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 import javax.inject.Singleton; 41 42 @ProducerModule(includes = { 43 SimpleBindingModule.ExecutorModule.class, 44 SimpleBindingModule.ProvisionModuleForMap.class 45 }) 46 abstract class SimpleBindingModule { 47 @Binds bindObject(FooOfStrings impl)48 abstract Object bindObject(FooOfStrings impl); 49 50 @Binds bindFooOfStrings(FooOfStrings impl)51 abstract Foo<String> bindFooOfStrings(FooOfStrings impl); 52 53 @Binds bindFooOfNumbers(Foo<Integer> fooOfIntegers)54 abstract Foo<? extends Number> bindFooOfNumbers(Foo<Integer> fooOfIntegers); 55 56 @Binds 57 @Singleton 58 @SomeQualifier bindQualifiedFooOfStrings(FooOfStrings impl)59 abstract Foo<String> bindQualifiedFooOfStrings(FooOfStrings impl); 60 61 @Produces produceFooOfStrings()62 static FooOfStrings produceFooOfStrings() { 63 return new FooOfStrings(); 64 } 65 66 @Produces produceFooOfIntegers()67 static Foo<Integer> produceFooOfIntegers() { 68 return new Foo<Integer>() {}; 69 } 70 71 @Produces produceFooOfDoubles()72 static Foo<Double> produceFooOfDoubles() { 73 return new Foo<Double>() {}; 74 } 75 76 @Binds 77 @IntoSet bindFooOfIntegersIntoSet(Foo<Integer> fooOfIntegers)78 abstract Foo<? extends Number> bindFooOfIntegersIntoSet(Foo<Integer> fooOfIntegers); 79 80 @Binds 81 @IntoSet bindFooExtendsNumberIntoSet(Foo<Double> fooOfDoubles)82 abstract Foo<? extends Number> bindFooExtendsNumberIntoSet(Foo<Double> fooOfDoubles); 83 84 @Binds 85 @ElementsIntoSet bindSetOfFooNumbersToObjects(Set<Foo<? extends Number>> setOfFooNumbers)86 abstract Set<Object> bindSetOfFooNumbersToObjects(Set<Foo<? extends Number>> setOfFooNumbers); 87 88 @Binds 89 @IntoSet bindFooOfStringsIntoSetOfObjects(FooOfStrings impl)90 abstract Object bindFooOfStringsIntoSetOfObjects(FooOfStrings impl); 91 92 @Produces produceStringHashSet()93 static HashSet<String> produceStringHashSet() { 94 return new HashSet<>(Arrays.asList("hash-string1", "hash-string2")); 95 } 96 97 @Produces produceCharSequenceTreeSet()98 static TreeSet<CharSequence> produceCharSequenceTreeSet() { 99 return new TreeSet<CharSequence>(Arrays.asList("tree-charSequence1", "tree-charSequence2")); 100 } 101 102 @Produces produceCharSequenceCollection()103 static Collection<CharSequence> produceCharSequenceCollection() { 104 return Arrays.<CharSequence>asList("list-charSequence"); 105 } 106 107 @Binds 108 @ElementsIntoSet bindHashSetOfStrings(HashSet<String> set)109 abstract Set<CharSequence> bindHashSetOfStrings(HashSet<String> set); 110 111 @Binds 112 @ElementsIntoSet bindTreeSetOfCharSequences(TreeSet<CharSequence> set)113 abstract Set<CharSequence> bindTreeSetOfCharSequences(TreeSet<CharSequence> set); 114 115 @Binds 116 @ElementsIntoSet bindCollectionOfCharSequences(Collection<CharSequence> collection)117 abstract Set<CharSequence> bindCollectionOfCharSequences(Collection<CharSequence> collection); 118 119 @Qualifier 120 @Retention(RetentionPolicy.RUNTIME) 121 @interface SomeQualifier {} 122 123 @Module 124 static final class ExecutorModule { 125 @Provides @Production provideExecutor()126 static Executor provideExecutor() { 127 return MoreExecutors.directExecutor(); 128 } 129 } 130 131 @Binds 132 @IntoMap 133 @IntKey(123) bind123ForMap(@amed"For-123") String string)134 abstract Object bind123ForMap(@Named("For-123") String string); 135 136 @Binds 137 @IntoMap 138 @IntKey(456) bind456ForMap(@amed"For-456") String string)139 abstract Object bind456ForMap(@Named("For-456") String string); 140 141 @Produces 142 @IntoMap 143 @IntKey(789) produce789ForMap()144 static Object produce789ForMap() { 145 return "789-string"; 146 } 147 148 @Module 149 abstract static class ProvisionModuleForMap { provideProvisionString()150 @Provides @Named("Provision string") static String provideProvisionString() { 151 return "provision-string"; 152 } 153 154 @Binds 155 @IntoMap 156 @IntKey(-1) bindNegative1ForMap(@amed"Provision string") String string)157 abstract Object bindNegative1ForMap(@Named("Provision string") String string); 158 } 159 160 @Binds 161 @IntoMap 162 @IntKey(123) 163 @SomeQualifier bindFooOfStringsIntoQualifiedMap(FooOfStrings fooOfStrings)164 abstract Object bindFooOfStringsIntoQualifiedMap(FooOfStrings fooOfStrings); 165 166 @Produces 167 @Named("For-123") produce123String()168 static String produce123String() { 169 return "123-string"; 170 } 171 172 @Produces 173 @Named("For-456") produce456String()174 static String produce456String() { 175 return "456-string"; 176 } 177 } 178