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