/* * Copyright (C) 2016 The Dagger Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package dagger.producers.internal; import static dagger.producers.internal.Producers.entryPointViewOf; import static dagger.producers.internal.Producers.nonCancellationPropagatingViewOf; import com.google.common.base.Function; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import dagger.producers.Producer; import java.util.Map; import javax.inject.Provider; /** * A {@link Producer} implementation used to implement {@link Map} bindings. This factory returns an * immediate future of {@code Map>} when calling {@link #get}. */ public final class MapOfProducerProducer extends AbstractMapProducer> { /** Returns a new {@link Builder}. */ public static Builder builder(int size) { return new Builder<>(size); } private MapOfProducerProducer(ImmutableMap> contributingMap) { super(contributingMap); } @Override public ListenableFuture>> compute() { return Futures.>>immediateFuture(contributingMap()); } /** A builder for {@link MapOfProducerProducer} */ public static final class Builder extends AbstractMapProducer.Builder> { private Builder(int size) { super(size); } @Override public Builder put(K key, Producer producerOfValue) { super.put(key, producerOfValue); return this; } @Override public Builder put(K key, Provider providerOfValue) { super.put(key, providerOfValue); return this; } @Override public Builder putAll(Producer>> mapOfProducerProducer) { super.putAll(mapOfProducerProducer); return this; } /** Returns a new {@link MapOfProducerProducer}. */ public MapOfProducerProducer build() { return new MapOfProducerProducer<>(mapBuilder.build()); } } @Override public Producer>> newDependencyView() { return newTransformedValuesView(MapOfProducerProducer.toDependencyView()); } @Override public Producer>> newEntryPointView( CancellationListener cancellationListener) { return newTransformedValuesView( MapOfProducerProducer.toEntryPointView(cancellationListener)); } private Producer>> newTransformedValuesView( Function, Producer> valueTransformationFunction) { return dagger.producers.Producers.>>immediateProducer( ImmutableMap.copyOf(Maps.transformValues(contributingMap(), valueTransformationFunction))); } @SuppressWarnings("unchecked") private static Function, Producer> toDependencyView() { return (Function) TO_DEPENDENCY_VIEW; } private static Function, Producer> toEntryPointView( final CancellationListener cancellationListener) { return new Function, Producer>() { @Override public Producer apply(Producer input) { return entryPointViewOf(input, cancellationListener); } }; } private static final Function, Producer> TO_DEPENDENCY_VIEW = new Function, Producer>() { @Override public Producer apply(Producer input) { return nonCancellationPropagatingViewOf(input); } }; }