xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/kotlinsrc/multibindings/LazyMaps.kt (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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.Component
20 import dagger.Lazy
21 import dagger.Module
22 import dagger.Provides
23 import dagger.multibindings.IntoMap
24 import dagger.multibindings.StringKey
25 import java.util.concurrent.atomic.AtomicInteger
26 import javax.inject.Provider
27 import javax.inject.Singleton
28 
29 /**
30  * Bindings that use `Lazy<T>` as the value in a multibound map. A regression was uncovered when
31  * using `MapType.valuesAreFrameworkType()`, which treats [Lazy] as a framework type and incorrectly
32  * suggested [dagger.internal.MapProviderFactory] for a `Map<K, Lazy<V>>` instead of a plain
33  * [dagger.internal.MapFactory]. See b/65084589.
34  */
35 class LazyMaps {
36   @Module
37   internal object TestModule {
provideAtomicIntegernull38     @Provides @Singleton fun provideAtomicInteger(): AtomicInteger = AtomicInteger()
39 
40     @Provides
41     fun provideString(atomicInteger: AtomicInteger): String =
42       "value-${atomicInteger.incrementAndGet()}"
43 
44     /* TODO(b/65118638) Replace once @Binds @IntoMap Lazy<T> methods work properly.
45     @Binds
46     @IntoMap
47     @StringKey("binds-key")
48     abstract Lazy<String> mapContributionAsBinds(Lazy<String> lazy);
49     */
50     @Provides
51     @IntoMap
52     @StringKey("key")
53     fun mapContribution(lazy: Lazy<String>): Lazy<String> = lazy
54   }
55 
56   @Singleton
57   @Component(modules = [TestModule::class])
58   interface TestComponent {
59     fun mapOfLazy(): Map<String, Lazy<String>>
60     fun mapOfProviderOfLazy(): Map<String, Provider<Lazy<String>>>
61     fun providerForMapOfLazy(): Provider<Map<String, Lazy<String>>>
62     fun providerForMapOfProviderOfLazy(): Provider<Map<String, Provider<Lazy<String>>>>
63   }
64 }
65