1 /*
2  * Copyright (C) 2017 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;
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.Map;
26 import java.util.concurrent.atomic.AtomicInteger;
27 import javax.inject.Provider;
28 import javax.inject.Singleton;
29 
30 /**
31  * Bindings that use {@code Lazy<T>} as the value in a multibound map. A regression was uncovered
32  * when using {@code MapType.valuesAreFrameworkType()}, which treats {@link Lazy} as a framework
33  * type and incorrectly suggested {@link dagger.internal.MapProviderFactory} for a {@code Map<K,
34  * Lazy<V>>} instead of a plain {@link dagger.internal.MapFactory}. See b/65084589.
35  */
36 class LazyMaps {
37   @Module
38   abstract static class TestModule {
39     @Provides
40     @Singleton
provideAtomicInteger()41     static AtomicInteger provideAtomicInteger() {
42       return new AtomicInteger();
43     }
44 
45     @Provides
provideString(AtomicInteger atomicInteger)46     static String provideString(AtomicInteger atomicInteger) {
47       return "value-" + atomicInteger.incrementAndGet();
48     }
49 
50     @Provides
51     @IntoMap
52     @StringKey("key")
mapContribution(Lazy<String> lazy)53     static Lazy<String> mapContribution(Lazy<String> lazy) {
54       return lazy;
55     }
56 
57     /* TODO(b/65118638) Replace once @Binds @IntoMap Lazy<T> methods work properly.
58     @Binds
59     @IntoMap
60     @StringKey("binds-key")
61     abstract Lazy<String> mapContributionAsBinds(Lazy<String> lazy);
62     */
63   }
64 
65   @Singleton
66   @Component(modules = TestModule.class)
67   interface TestComponent {
mapOfLazy()68     Map<String, Lazy<String>> mapOfLazy();
69 
mapOfProviderOfLazy()70     Map<String, Provider<Lazy<String>>> mapOfProviderOfLazy();
71 
providerForMapOfLazy()72     Provider<Map<String, Lazy<String>>> providerForMapOfLazy();
73 
providerForMapOfProviderOfLazy()74     Provider<Map<String, Provider<Lazy<String>>>> providerForMapOfProviderOfLazy();
75   }
76 }
77