• 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 java.util.Collections.unmodifiableMap;
21 
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import javax.inject.Provider;
26 
27 /**
28  * A {@link Factory} implementation used to implement {@link Map} bindings. This factory returns a
29  * {@code Map<K, V>} when calling {@link #get} (as specified by {@link Factory}).
30  */
31 public final class MapFactory<K, V> extends AbstractMapFactory<K, V, V> {
32   private static final Provider<Map<Object, Object>> EMPTY =
33       InstanceFactory.create(Collections.emptyMap());
34 
35   /** Returns a new {@link Builder} */
builder(int size)36   public static <K, V> Builder<K, V> builder(int size) {
37     return new Builder<>(size);
38   }
39 
40   /** Returns a factory of an empty map. */
41   @SuppressWarnings("unchecked") // safe contravariant cast
emptyMapProvider()42   public static <K, V> Provider<Map<K, V>> emptyMapProvider() {
43     return (Provider<Map<K, V>>) (Provider) EMPTY;
44   }
45 
MapFactory(Map<K, Provider<V>> map)46   private MapFactory(Map<K, Provider<V>> map) {
47     super(map);
48   }
49 
50   /**
51    * Returns a {@code Map<K, V>} whose iteration order is that of the elements given by each of the
52    * providers, which are invoked in the order given at creation.
53    */
54   @Override
get()55   public Map<K, V> get() {
56     Map<K, V> result = newLinkedHashMapWithExpectedSize(contributingMap().size());
57     for (Entry<K, Provider<V>> entry : contributingMap().entrySet()) {
58       result.put(entry.getKey(), entry.getValue().get());
59     }
60     return unmodifiableMap(result);
61   }
62 
63   /** A builder for {@link MapFactory}. */
64   public static final class Builder<K, V> extends AbstractMapFactory.Builder<K, V, V> {
Builder(int size)65     private Builder(int size) {
66       super(size);
67     }
68 
69     @Override
put(K key, Provider<V> providerOfValue)70     public Builder<K, V> put(K key, Provider<V> providerOfValue) {
71       super.put(key, providerOfValue);
72       return this;
73     }
74 
75     @Override
putAll(Provider<Map<K, V>> mapFactory)76     public Builder<K, V> putAll(Provider<Map<K, V>> mapFactory) {
77       super.putAll(mapFactory);
78       return this;
79     }
80 
81     /** Returns a new {@link MapProviderFactory}. */
build()82     public MapFactory<K, V> build() {
83       return new MapFactory<>(map);
84     }
85   }
86 }
87