1 /* 2 * Copyright (C) 2024 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 21 import com.google.common.base.Function; 22 import com.google.common.util.concurrent.Futures; 23 import com.google.common.util.concurrent.ListenableFuture; 24 import dagger.internal.LazyClassKeyMap; 25 import dagger.producers.Produced; 26 import java.util.Map; 27 28 /** 29 * Wrapper around {@link MapOfProducedProducer} to be compatible with @LazyClassKey annotated map. 30 */ 31 public final class LazyMapOfProducedProducer<V> 32 extends AbstractProducer<Map<Class<?>, Produced<V>>> { 33 AbstractProducer<Map<String, Produced<V>>> delegate; 34 of( AbstractProducer<Map<String, Produced<V>>> delegate)35 public static <V> LazyMapOfProducedProducer<V> of( 36 AbstractProducer<Map<String, Produced<V>>> delegate) { 37 return new LazyMapOfProducedProducer<V>(delegate); 38 } 39 LazyMapOfProducedProducer(AbstractProducer<Map<String, Produced<V>>> delegate)40 private LazyMapOfProducedProducer(AbstractProducer<Map<String, Produced<V>>> delegate) { 41 this.delegate = delegate; 42 } 43 44 @Override compute()45 public ListenableFuture<Map<Class<?>, Produced<V>>> compute() { 46 return Futures.transform( 47 delegate.compute(), 48 new Function<Map<String, Produced<V>>, Map<Class<?>, Produced<V>>>() { 49 @Override 50 public Map<Class<?>, Produced<V>> apply(Map<String, Produced<V>> classMap) { 51 return LazyClassKeyMap.of(classMap); 52 } 53 }, 54 directExecutor()); 55 } 56 } 57