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.multibindings; 18 19 import com.google.common.collect.ImmutableSet; 20 import com.google.common.util.concurrent.Futures; 21 import com.google.common.util.concurrent.ListenableFuture; 22 import dagger.functional.producers.multibindings.Qualifiers.EmptyButDeclaredInModuleAndProducerModule; 23 import dagger.functional.producers.multibindings.Qualifiers.ObjCount; 24 import dagger.functional.producers.multibindings.Qualifiers.PossiblyThrowingMap; 25 import dagger.functional.producers.multibindings.Qualifiers.PossiblyThrowingSet; 26 import dagger.multibindings.ElementsIntoSet; 27 import dagger.multibindings.IntKey; 28 import dagger.multibindings.IntoMap; 29 import dagger.multibindings.IntoSet; 30 import dagger.multibindings.LazyClassKey; 31 import dagger.multibindings.Multibinds; 32 import dagger.producers.Produced; 33 import dagger.producers.ProducerModule; 34 import dagger.producers.Produces; 35 import java.util.Map; 36 import java.util.Set; 37 38 @ProducerModule 39 abstract class MultibindingProducerModule { 40 static class Foo {} 41 42 @Produces 43 @IntoSet futureStr()44 static ListenableFuture<String> futureStr() { 45 return Futures.immediateFuture("foo"); 46 } 47 48 @Produces 49 @IntoSet str()50 static String str() { 51 return "bar"; 52 } 53 54 @Produces 55 @ElementsIntoSet futureStrs()56 static ListenableFuture<Set<String>> futureStrs() { 57 return Futures.<Set<String>>immediateFuture(ImmutableSet.of("foo1", "foo2")); 58 } 59 60 @Produces 61 @ElementsIntoSet strFutures()62 static Set<ListenableFuture<String>> strFutures() { 63 return ImmutableSet.of(Futures.immediateFuture("baz1"), Futures.immediateFuture("baz2")); 64 } 65 66 @Produces 67 @ElementsIntoSet strs()68 static Set<String> strs() { 69 return ImmutableSet.of("bar1", "bar2"); 70 } 71 72 @Produces strCount(Set<String> strs)73 static int strCount(Set<String> strs) { 74 return strs.size(); 75 } 76 77 @Produces 78 @IntoSet 79 @PossiblyThrowingSet successfulStringForSet()80 static String successfulStringForSet() { 81 return "singleton"; 82 } 83 84 @Produces 85 @ElementsIntoSet 86 @PossiblyThrowingSet successfulStringsForSet()87 static Set<String> successfulStringsForSet() { 88 return ImmutableSet.of("double", "ton"); 89 } 90 91 @Produces 92 @IntoSet 93 @PossiblyThrowingSet throwingStringForSet()94 static String throwingStringForSet() { 95 throw new RuntimeException("monkey"); 96 } 97 98 @Produces 99 @IntoMap 100 @IntKey(42) futureFor42()101 static ListenableFuture<String> futureFor42() { 102 return Futures.immediateFuture("forty two"); 103 } 104 105 @Produces 106 @IntoMap 107 @IntKey(15) valueFor15()108 static String valueFor15() { 109 return "fifteen"; 110 } 111 112 @Produces 113 @IntoMap 114 @PossiblyThrowingMap 115 @IntKey(42) successfulFutureFor42()116 static ListenableFuture<String> successfulFutureFor42() { 117 return Futures.immediateFuture("forty two"); 118 } 119 120 @Produces 121 @IntoMap 122 @PossiblyThrowingMap 123 @IntKey(15) throwingValueFor15()124 static String throwingValueFor15() { 125 throw new RuntimeException("monkey"); 126 } 127 128 @Produces 129 @IntoMap 130 @LazyClassKey(Foo.class) produceName()131 static String produceName() { 132 return "foo"; 133 } 134 135 @Multibinds objs()136 abstract Set<Object> objs(); 137 138 @Multibinds objMap()139 abstract Map<Object, Object> objMap(); 140 141 @Produces 142 @ObjCount objCount(Set<Produced<Object>> objs, Map<Object, Produced<Object>> objMap)143 static int objCount(Set<Produced<Object>> objs, Map<Object, Produced<Object>> objMap) { 144 return objs.size() + objMap.size(); 145 } 146 147 @Multibinds 148 @EmptyButDeclaredInModuleAndProducerModule emptyButDeclaredInModuleAndProducerModule()149 abstract Map<String, Object> emptyButDeclaredInModuleAndProducerModule(); 150 } 151