xref: /aosp_15_r20/external/dagger2/java/dagger/producers/internal/MapProducer.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2016 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.producers.internal;
18 
19 import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
20 import static dagger.internal.Providers.asDaggerProvider;
21 
22 import com.google.common.base.Function;
23 import com.google.common.collect.ImmutableMap;
24 import com.google.common.collect.Maps;
25 import com.google.common.util.concurrent.Futures;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import dagger.internal.Provider;
28 import dagger.producers.Producer;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 
34 /**
35  * A {@link Producer} implementation used to implement {@link Map} bindings. This producer returns a
36  * {@code Map<K, V>} which is populated by calls to the delegate {@link Producer#get} methods.
37  */
38 public final class MapProducer<K, V> extends AbstractMapProducer<K, V, V> {
MapProducer(ImmutableMap<K, Producer<V>> contributingMap)39   private MapProducer(ImmutableMap<K, Producer<V>> contributingMap) {
40     super(contributingMap);
41   }
42 
43   /** Returns a new {@link Builder}. */
builder(int size)44   public static <K, V> Builder<K, V> builder(int size) {
45     return new Builder<>(size);
46   }
47 
48   /** A builder for {@link MapProducer} */
49   public static final class Builder<K, V> extends AbstractMapProducer.Builder<K, V, V> {
Builder(int size)50     private Builder(int size) {
51       super(size);
52     }
53 
54     @Override
put(K key, Producer<V> producerOfValue)55     public Builder<K, V> put(K key, Producer<V> producerOfValue) {
56       super.put(key, producerOfValue);
57       return this;
58     }
59 
60     @Override
put(K key, Provider<V> providerOfValue)61     public Builder<K, V> put(K key, Provider<V> providerOfValue) {
62       super.put(key, providerOfValue);
63       return this;
64     }
65 
66     /**
67      * Legacy javax version of the method to support libraries compiled with an older version of
68      * Dagger. Do not use directly.
69      */
70     @Deprecated
put(K key, javax.inject.Provider<V> providerOfValue)71     public Builder<K, V> put(K key, javax.inject.Provider<V> providerOfValue) {
72       return put(key, asDaggerProvider(providerOfValue));
73     }
74 
75     @Override
putAll(Producer<Map<K, V>> mapProducer)76     public Builder<K, V> putAll(Producer<Map<K, V>> mapProducer) {
77       super.putAll(mapProducer);
78       return this;
79     }
80 
81     /** Returns a new {@link MapProducer}. */
build()82     public MapProducer<K, V> build() {
83       return new MapProducer<>(mapBuilder.build());
84     }
85   }
86 
87   @Override
compute()88   protected ListenableFuture<Map<K, V>> compute() {
89     final List<ListenableFuture<Map.Entry<K, V>>> listOfEntries = new ArrayList<>();
90     for (final Entry<K, Producer<V>> entry : contributingMap().entrySet()) {
91       listOfEntries.add(
92           Futures.transform(
93               entry.getValue().get(),
94               new Function<V, Entry<K, V>>() {
95                 @Override
96                 public Entry<K, V> apply(V computedValue) {
97                   return Maps.immutableEntry(entry.getKey(), computedValue);
98                 }
99               },
100               directExecutor()));
101     }
102 
103     return Futures.transform(
104         Futures.allAsList(listOfEntries),
105         new Function<List<Map.Entry<K, V>>, Map<K, V>>() {
106           @Override
107           public Map<K, V> apply(List<Map.Entry<K, V>> entries) {
108             return ImmutableMap.copyOf(entries);
109           }
110         },
111         directExecutor());
112   }
113 }
114