1 /*
2  * Copyright (C) 2023 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.kotlinsrc.multibindings
18 
19 import dagger.Module
20 import dagger.Provides
21 import dagger.functional.kotlinsrc.multibindings.NestedAnnotationContainer.NestedWrappedKey
22 import dagger.multibindings.ClassKey
23 import dagger.multibindings.ElementsIntoSet
24 import dagger.multibindings.IntKey
25 import dagger.multibindings.IntoMap
26 import dagger.multibindings.IntoSet
27 import dagger.multibindings.LongKey
28 import dagger.multibindings.StringKey
29 import java.math.BigDecimal
30 import java.math.BigInteger
31 import javax.inject.Named
32 import javax.inject.Provider
33 
34 @Module
35 internal object MultibindingModule {
36   @Provides
37   @IntoMap
38   @StringKey("foo")
provideFooKeynull39   fun provideFooKey(@Suppress("UNUSED_PARAMETER") doubleDependency: Double): String = "foo value"
40 
41   @Provides @IntoMap @StringKey("bar") fun provideBarKey(): String = "bar value"
42 
43   @Provides
44   @IntoMap
45   @StringKey("foo")
46   fun provideFooArrayValue(
47     @Suppress("UNUSED_PARAMETER") doubleDependency: Double
48   ): Array<String> = arrayOf("foo1", "foo2")
49 
50   @Provides
51   @IntoMap
52   @StringKey("bar")
53   fun provideBarArrayValue(): Array<String> = arrayOf("bar1", "bar2")
54 
55   @Provides @IntoSet fun provideFiveToSet(): Int = 5
56 
57   @Provides @IntoSet fun provideSixToSet(): Int = 6
58 
59   @Provides @ElementsIntoSet fun provideElementsIntoSet(): Set<Int> = setOf(-101, -102)
60 
61   @Provides
62   fun provideMapKeys(map: Map<String, @JvmSuppressWildcards Provider<String>>): Set<String> =
63     map.keys
64 
65   @Provides fun provideMapValues(map: Map<String, String>): Collection<String> = map.values
66 
67   @Provides
68   @IntoMap
69   @NestedWrappedKey(java.lang.Integer::class)
70   fun valueForInteger(): String = "integer"
71 
72   @Provides @IntoMap @NestedWrappedKey(java.lang.Long::class) fun valueForLong(): String = "long"
73 
74   @Provides
75   @IntoMap
76   @ClassKey(java.lang.Integer::class)
77   fun valueForClassInteger(): String = "integer"
78 
79   @Provides @IntoMap @ClassKey(java.lang.Long::class) fun valueForClassLong(): String = "long"
80 
81   @Provides
82   @IntoMap
83   @NumberClassKey(BigDecimal::class)
84   fun valueForNumberClassBigDecimal(): String = "bigdecimal"
85 
86   @Provides
87   @IntoMap
88   @NumberClassKey(BigInteger::class)
89   fun valueForNumberClassBigInteger(): String = "biginteger"
90 
91   @Provides @IntoMap @LongKey(100) fun valueFor100Long(): String = "100 long"
92 
93   @Provides @IntoMap @IntKey(100) fun valueFor100Int(): String = "100 int"
94 
95   @Provides @IntoMap @ShortKey(100) fun valueFor100Short(): String = "100 short"
96 
97   @Provides @IntoMap @ByteKey(100) fun valueFor100Byte(): String = "100 byte"
98 
99   @Provides @IntoMap @BooleanKey(true) fun valueForTrue(): String = "true"
100 
101   @Provides @IntoMap @CharKey('a') fun valueForA(): String = "a char"
102 
103   @Provides @IntoMap @CharKey('\n') fun valueForNewline(): String = "newline char"
104 
105   @Provides
106   @IntoMap
107   @UnwrappedAnnotationKey(StringKey("foo\n"))
108   fun valueForUnwrappedAnnotationKeyFoo(): String = "foo annotation"
109 
110   @Provides
111   @IntoMap
112   @WrappedAnnotationKey(
113     value = StringKey("foo"),
114     integers = [1, 2, 3],
115     annotations = [],
116     classes = [java.lang.Long::class, java.lang.Integer::class]
117   )
118   fun valueForWrappedAnnotationKeyFoo(): String = "wrapped foo annotation"
119 
120   @Provides @IntoSet @Named("complexQualifier") fun valueForComplexQualifierSet(): String = "foo"
121 
122   @Provides @IntoSet fun setContribution(): CharSequence = "foo"
123 
124   @Provides
125   @IntoSet
126   @Named("complexQualifier")
127   fun qualifiedSetContribution(): CharSequence = "qualified foo"
128 
129   @Provides @IntoMap @StringKey("key") fun mapContribution(): CharSequence = "foo value"
130 
131   @Provides
132   @IntoMap
133   @Named("complexQualifier")
134   @StringKey("key")
135   fun qualifiedMapContribution(): CharSequence = "qualified foo value"
136 }
137