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