1 /* 2 * Copyright (C) 2015 Google, Inc. 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 package test; 17 18 import dagger.Module; 19 import dagger.Provides; 20 import dagger.mapkeys.ClassKey; 21 import dagger.mapkeys.IntKey; 22 import dagger.mapkeys.LongKey; 23 import dagger.mapkeys.StringKey; 24 import java.math.BigDecimal; 25 import java.math.BigInteger; 26 import java.util.Collection; 27 import java.util.Map; 28 import java.util.Set; 29 import javax.inject.Named; 30 import javax.inject.Provider; 31 32 import static dagger.Provides.Type.MAP; 33 import static dagger.Provides.Type.SET; 34 35 @Module 36 class MultibindingModule { 37 @Provides(type = MAP) 38 @StringKey("foo") 39 static String provideFooKey(double doubleDependency) { 40 return "foo value"; 41 } 42 43 @Provides(type = MAP) 44 @StringKey("bar") 45 static String provideBarKey() { 46 return "bar value"; 47 } 48 49 @Provides(type = MAP) 50 @StringKey("foo") 51 static String[] provideFooArrayValue(double doubleDependency) { 52 return new String[] {"foo1", "foo2"}; 53 } 54 55 @Provides(type = MAP) 56 @StringKey("bar") 57 static String[] provideBarArrayValue() { 58 return new String[] {"bar1", "bar2"}; 59 } 60 61 @Provides(type = SET) 62 static int provideFiveToSet() { 63 return 5; 64 } 65 66 @Provides(type = SET) 67 static int provideSixToSet() { 68 return 6; 69 } 70 71 @Provides 72 static Set<String> provideMapKeys(Map<String, Provider<String>> map) { 73 return map.keySet(); 74 } 75 76 @Provides 77 static Collection<String> provideMapValues(Map<String, String> map) { 78 return map.values(); 79 } 80 81 @Provides(type = MAP) 82 @NestedAnnotationContainer.NestedWrappedKey(Integer.class) 83 static String valueForInteger() { 84 return "integer"; 85 } 86 87 @Provides(type = MAP) 88 @NestedAnnotationContainer.NestedWrappedKey(Long.class) 89 static String valueForLong() { 90 return "long"; 91 } 92 93 @Provides(type = MAP) 94 @ClassKey(Integer.class) 95 static String valueForClassInteger() { 96 return "integer"; 97 } 98 99 @Provides(type = MAP) 100 @ClassKey(Long.class) 101 static String valueForClassLong() { 102 return "long"; 103 } 104 105 @Provides(type = MAP) 106 @NumberClassKey(BigDecimal.class) 107 static String valueForNumberClassBigDecimal() { 108 return "bigdecimal"; 109 } 110 111 @Provides(type = MAP) 112 @NumberClassKey(BigInteger.class) 113 static String valueForNumberClassBigInteger() { 114 return "biginteger"; 115 } 116 117 @Provides(type = MAP) 118 @LongKey(100) 119 static String valueFor100Long() { 120 return "100 long"; 121 } 122 123 @Provides(type = MAP) 124 @IntKey(100) 125 static String valueFor100Int() { 126 return "100 int"; 127 } 128 129 @Provides(type = MAP) 130 @ShortKey(100) 131 static String valueFor100Short() { 132 return "100 short"; 133 } 134 135 @Provides(type = MAP) 136 @ByteKey(100) 137 static String valueFor100Byte() { 138 return "100 byte"; 139 } 140 141 @Provides(type = MAP) 142 @BooleanKey(true) 143 static String valueForTrue() { 144 return "true"; 145 } 146 147 @Provides(type = MAP) 148 @CharKey('a') 149 static String valueForA() { 150 return "a char"; 151 } 152 153 @Provides(type = MAP) 154 @CharKey('\n') 155 static String valueForNewline() { 156 return "newline char"; 157 } 158 159 @Provides(type = MAP) 160 @UnwrappedAnnotationKey(@StringKey("foo\n")) 161 static String valueForUnwrappedAnnotationKeyFoo() { 162 return "foo annotation"; 163 } 164 165 @Provides(type = MAP) 166 @WrappedAnnotationKey( 167 value = @StringKey("foo"), 168 integers = {1, 2, 3}, 169 annotations = {}, 170 classes = {Long.class, Integer.class} 171 ) 172 static String valueForWrappedAnnotationKeyFoo() { 173 return "wrapped foo annotation"; 174 } 175 176 @Provides(type = SET) 177 @Named("complexQualifier") 178 static String valueForComplexQualifierSet() { 179 return "foo"; 180 } 181 } 182