1*f585d8a3SJacky Wang /* 2*f585d8a3SJacky Wang * Copyright (C) 2014 The Dagger Authors. 3*f585d8a3SJacky Wang * 4*f585d8a3SJacky Wang * Licensed under the Apache License, Version 2.0 (the "License"); 5*f585d8a3SJacky Wang * you may not use this file except in compliance with the License. 6*f585d8a3SJacky Wang * You may obtain a copy of the License at 7*f585d8a3SJacky Wang * 8*f585d8a3SJacky Wang * http://www.apache.org/licenses/LICENSE-2.0 9*f585d8a3SJacky Wang * 10*f585d8a3SJacky Wang * Unless required by applicable law or agreed to in writing, software 11*f585d8a3SJacky Wang * distributed under the License is distributed on an "AS IS" BASIS, 12*f585d8a3SJacky Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*f585d8a3SJacky Wang * See the License for the specific language governing permissions and 14*f585d8a3SJacky Wang * limitations under the License. 15*f585d8a3SJacky Wang */ 16*f585d8a3SJacky Wang 17*f585d8a3SJacky Wang package dagger.internal; 18*f585d8a3SJacky Wang 19*f585d8a3SJacky Wang import static dagger.internal.DaggerCollections.newLinkedHashMapWithExpectedSize; 20*f585d8a3SJacky Wang import static dagger.internal.Preconditions.checkNotNull; 21*f585d8a3SJacky Wang import static java.util.Collections.unmodifiableMap; 22*f585d8a3SJacky Wang 23*f585d8a3SJacky Wang import java.util.LinkedHashMap; 24*f585d8a3SJacky Wang import java.util.Map; 25*f585d8a3SJacky Wang 26*f585d8a3SJacky Wang /** 27*f585d8a3SJacky Wang * An {@code abstract} {@link Factory} implementation used to implement {@link Map} bindings. 28*f585d8a3SJacky Wang * 29*f585d8a3SJacky Wang * @param <K> the key type of the map that this provides 30*f585d8a3SJacky Wang * @param <V> the type that each contributing factory 31*f585d8a3SJacky Wang * @param <V2> the value type of the map that this provides 32*f585d8a3SJacky Wang */ 33*f585d8a3SJacky Wang abstract class AbstractMapFactory<K, V, V2> implements Factory<Map<K, V2>> { 34*f585d8a3SJacky Wang private final Map<K, Provider<V>> contributingMap; 35*f585d8a3SJacky Wang AbstractMapFactory(Map<K, Provider<V>> map)36*f585d8a3SJacky Wang AbstractMapFactory(Map<K, Provider<V>> map) { 37*f585d8a3SJacky Wang this.contributingMap = unmodifiableMap(map); 38*f585d8a3SJacky Wang } 39*f585d8a3SJacky Wang 40*f585d8a3SJacky Wang /** The map of {@link Provider}s that contribute to this map binding. */ contributingMap()41*f585d8a3SJacky Wang final Map<K, Provider<V>> contributingMap() { 42*f585d8a3SJacky Wang return contributingMap; 43*f585d8a3SJacky Wang } 44*f585d8a3SJacky Wang 45*f585d8a3SJacky Wang /** A builder for {@link AbstractMapFactory}. */ 46*f585d8a3SJacky Wang public abstract static class Builder<K, V, V2> { 47*f585d8a3SJacky Wang final LinkedHashMap<K, Provider<V>> map; 48*f585d8a3SJacky Wang Builder(int size)49*f585d8a3SJacky Wang Builder(int size) { 50*f585d8a3SJacky Wang this.map = newLinkedHashMapWithExpectedSize(size); 51*f585d8a3SJacky Wang } 52*f585d8a3SJacky Wang 53*f585d8a3SJacky Wang // Unfortunately, we cannot return a self-type here because a raw Provider type passed to one of 54*f585d8a3SJacky Wang // these methods affects the returned type of the method. The first put*() call erases the self 55*f585d8a3SJacky Wang // type to the "raw" self type, and the second erases the type to the upper bound 56*f585d8a3SJacky Wang // (AbstractMapFactory.Builder), which doesn't have a build() method. 57*f585d8a3SJacky Wang // 58*f585d8a3SJacky Wang // The methods are therefore not declared public so that each subtype will redeclare them and 59*f585d8a3SJacky Wang // expand their accessibility 60*f585d8a3SJacky Wang 61*f585d8a3SJacky Wang /** Associates {@code key} with {@code providerOfValue}. */ put(K key, Provider<V> providerOfValue)62*f585d8a3SJacky Wang Builder<K, V, V2> put(K key, Provider<V> providerOfValue) { 63*f585d8a3SJacky Wang map.put(checkNotNull(key, "key"), checkNotNull(providerOfValue, "provider")); 64*f585d8a3SJacky Wang return this; 65*f585d8a3SJacky Wang } 66*f585d8a3SJacky Wang putAll(Provider<Map<K, V2>> mapOfProviders)67*f585d8a3SJacky Wang Builder<K, V, V2> putAll(Provider<Map<K, V2>> mapOfProviders) { 68*f585d8a3SJacky Wang if (mapOfProviders instanceof DelegateFactory) { 69*f585d8a3SJacky Wang @SuppressWarnings("unchecked") 70*f585d8a3SJacky Wang DelegateFactory<Map<K, V2>> asDelegateFactory = (DelegateFactory) mapOfProviders; 71*f585d8a3SJacky Wang return putAll(asDelegateFactory.getDelegate()); 72*f585d8a3SJacky Wang } 73*f585d8a3SJacky Wang @SuppressWarnings("unchecked") 74*f585d8a3SJacky Wang AbstractMapFactory<K, V, ?> asAbstractMapFactory = 75*f585d8a3SJacky Wang ((AbstractMapFactory<K, V, ?>) (Provider) mapOfProviders); 76*f585d8a3SJacky Wang map.putAll(asAbstractMapFactory.contributingMap); 77*f585d8a3SJacky Wang return this; 78*f585d8a3SJacky Wang } 79*f585d8a3SJacky Wang } 80*f585d8a3SJacky Wang } 81