xref: /aosp_15_r20/external/dagger2/java/dagger/internal/AbstractMapFactory.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2014 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.internal;
18 
19 import static dagger.internal.DaggerCollections.newLinkedHashMapWithExpectedSize;
20 import static dagger.internal.Preconditions.checkNotNull;
21 import static java.util.Collections.unmodifiableMap;
22 
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25 
26 /**
27  * An {@code abstract} {@link Factory} implementation used to implement {@link Map} bindings.
28  *
29  * @param <K> the key type of the map that this provides
30  * @param <V> the type that each contributing factory
31  * @param <V2> the value type of the map that this provides
32  */
33 abstract class AbstractMapFactory<K, V, V2> implements Factory<Map<K, V2>> {
34   private final Map<K, Provider<V>> contributingMap;
35 
AbstractMapFactory(Map<K, Provider<V>> map)36   AbstractMapFactory(Map<K, Provider<V>> map) {
37     this.contributingMap = unmodifiableMap(map);
38   }
39 
40   /** The map of {@link Provider}s that contribute to this map binding. */
contributingMap()41   final Map<K, Provider<V>> contributingMap() {
42     return contributingMap;
43   }
44 
45   /** A builder for {@link AbstractMapFactory}. */
46   public abstract static class Builder<K, V, V2> {
47     final LinkedHashMap<K, Provider<V>> map;
48 
Builder(int size)49     Builder(int size) {
50       this.map = newLinkedHashMapWithExpectedSize(size);
51     }
52 
53     // Unfortunately, we cannot return a self-type here because a raw Provider type passed to one of
54     // these methods affects the returned type of the method. The first put*() call erases the self
55     // type to the "raw" self type, and the second erases the type to the upper bound
56     // (AbstractMapFactory.Builder), which doesn't have a build() method.
57     //
58     // The methods are therefore not declared public so that each subtype will redeclare them and
59     // expand their accessibility
60 
61     /** Associates {@code key} with {@code providerOfValue}. */
put(K key, Provider<V> providerOfValue)62     Builder<K, V, V2> put(K key, Provider<V> providerOfValue) {
63       map.put(checkNotNull(key, "key"), checkNotNull(providerOfValue, "provider"));
64       return this;
65     }
66 
putAll(Provider<Map<K, V2>> mapOfProviders)67     Builder<K, V, V2> putAll(Provider<Map<K, V2>> mapOfProviders) {
68       if (mapOfProviders instanceof DelegateFactory) {
69         @SuppressWarnings("unchecked")
70         DelegateFactory<Map<K, V2>> asDelegateFactory = (DelegateFactory) mapOfProviders;
71         return putAll(asDelegateFactory.getDelegate());
72       }
73       @SuppressWarnings("unchecked")
74       AbstractMapFactory<K, V, ?> asAbstractMapFactory =
75           ((AbstractMapFactory<K, V, ?>) (Provider) mapOfProviders);
76       map.putAll(asAbstractMapFactory.contributingMap);
77       return this;
78     }
79   }
80 }
81