• 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 dagger.Lazy;
20 import java.util.Map;
21 import javax.inject.Provider;
22 
23 /**
24  * A {@link Factory} implementation used to implement {@link Map} bindings. This factory returns a
25  * {@code Map<K, Provider<V>>} when calling {@link #get} (as specified by {@link Factory}).
26  */
27 public final class MapProviderFactory<K, V> extends AbstractMapFactory<K, V, Provider<V>>
28     implements Lazy<Map<K, Provider<V>>> {
29 
30   /** Returns a new {@link Builder} */
builder(int size)31   public static <K, V> Builder<K, V> builder(int size) {
32     return new Builder<>(size);
33   }
34 
MapProviderFactory(Map<K, Provider<V>> contributingMap)35   private MapProviderFactory(Map<K, Provider<V>> contributingMap) {
36     super(contributingMap);
37   }
38 
39   /**
40    * Returns a {@code Map<K, Provider<V>>} whose iteration order is that of the elements given by
41    * each of the providers, which are invoked in the order given at creation.
42    */
43   @Override
get()44   public Map<K, Provider<V>> get() {
45     return contributingMap();
46   }
47 
48   /** A builder for {@link MapProviderFactory}. */
49   public static final class Builder<K, V> extends AbstractMapFactory.Builder<K, V, Provider<V>> {
Builder(int size)50     private Builder(int size) {
51       super(size);
52     }
53 
54     @Override
put(K key, Provider<V> providerOfValue)55     public Builder<K, V> put(K key, Provider<V> providerOfValue) {
56       super.put(key, providerOfValue);
57       return this;
58     }
59 
60     @Override
putAll(Provider<Map<K, Provider<V>>> mapProviderFactory)61     public Builder<K, V> putAll(Provider<Map<K, Provider<V>>> mapProviderFactory) {
62       super.putAll(mapProviderFactory);
63       return this;
64     }
65 
66     /** Returns a new {@link MapProviderFactory}. */
build()67     public MapProviderFactory<K, V> build() {
68       return new MapProviderFactory<>(map);
69     }
70   }
71 }
72