All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.common.inject.multibindings.MapBinder Maven / Gradle / Ivy

There is a newer version: 8.13.2
Show newest version
/*
 * Copyright (C) 2008 Google Inc.
 *
 * 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 org.elasticsearch.common.inject.multibindings;

import org.elasticsearch.common.inject.Binder;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Key;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.inject.Provider;
import org.elasticsearch.common.inject.TypeLiteral;
import org.elasticsearch.common.inject.binder.LinkedBindingBuilder;
import org.elasticsearch.common.inject.multibindings.Multibinder.RealMultibinder;
import org.elasticsearch.common.inject.spi.Dependency;
import org.elasticsearch.common.inject.spi.ProviderWithDependencies;
import org.elasticsearch.common.inject.util.Types;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import static java.util.Collections.singleton;
import static org.elasticsearch.common.inject.util.Types.newParameterizedType;
import static org.elasticsearch.common.inject.util.Types.newParameterizedTypeWithOwner;

/**
 * An API to bind multiple map entries separately, only to later inject them as
 * a complete map. MapBinder is intended for use in your application's module:
 * 

 * public class SnacksModule extends AbstractModule {
 *   protected void configure() {
 *     MapBinder<String, Snack> mapbinder
 *         = MapBinder.newMapBinder(binder(), String.class, Snack.class);
 *     mapbinder.addBinding("twix").toInstance(new Twix());
 *     mapbinder.addBinding("snickers").toProvider(SnickersProvider.class);
 *     mapbinder.addBinding("skittles").to(Skittles.class);
 *   }
 * }
*

* With this binding, a {@link Map}{@code } can now be * injected: *


 * class SnackMachine {
 *   {@literal @}Inject
 *   public SnackMachine(Map<String, Snack> snacks) { ... }
 * }
*

* In addition to binding {@code Map}, a mapbinder will also bind * {@code Map>} for lazy value provision: *


 * class SnackMachine {
 *   {@literal @}Inject
 *   public SnackMachine(Map<String, Provider<Snack>> snackProviders) { ... }
 * }
*

* Creating mapbindings from different modules is supported. For example, it * is okay to have both {@code CandyModule} and {@code ChipsModule} both * create their own {@code MapBinder}, and to each contribute * bindings to the snacks map. When that map is injected, it will contain * entries from both modules. *

* Values are resolved at map injection time. If a value is bound to a * provider, that provider's get method will be called each time the map is * injected (unless the binding is also scoped, or a map of providers is injected). *

* Annotations are used to create different maps of the same key/value * type. Each distinct annotation gets its own independent map. *

* Keys must be distinct. If the same key is bound more than * once, map injection will fail. *

* Keys must be non-null. {@code addBinding(null)} will * throw an unchecked exception. *

* Values must be non-null to use map injection. If any * value is null, map injection will fail (although injecting a map of providers * will not). * * @author [email protected] (David P. Baker) */ public abstract class MapBinder { private MapBinder() { } /** * Returns a new mapbinder that collects entries of {@code keyType}/{@code valueType} in a * {@link Map} that is itself bound with no binding annotation. */ public static MapBinder newMapBinder(Binder binder, TypeLiteral keyType, TypeLiteral valueType) { binder = binder.skipSources(MapBinder.class, RealMapBinder.class); return newMapBinder(binder, valueType, Key.get(mapOf(keyType, valueType)), Key.get(mapOfProviderOf(keyType, valueType)), Multibinder.newSetBinder(binder, entryOfProviderOf(keyType, valueType))); } /** * Returns a new mapbinder that collects entries of {@code keyType}/{@code valueType} in a * {@link Map} that is itself bound with no binding annotation. */ public static MapBinder newMapBinder(Binder binder, Class keyType, Class valueType) { return newMapBinder(binder, TypeLiteral.get(keyType), TypeLiteral.get(valueType)); } /** * Returns a new mapbinder that collects entries of {@code keyType}/{@code valueType} in a * {@link Map} that is itself bound with {@code annotation}. */ public static MapBinder newMapBinder(Binder binder, TypeLiteral keyType, TypeLiteral valueType, Annotation annotation) { binder = binder.skipSources(MapBinder.class, RealMapBinder.class); return newMapBinder(binder, valueType, Key.get(mapOf(keyType, valueType), annotation), Key.get(mapOfProviderOf(keyType, valueType), annotation), Multibinder.newSetBinder(binder, entryOfProviderOf(keyType, valueType), annotation)); } /** * Returns a new mapbinder that collects entries of {@code keyType}/{@code valueType} in a * {@link Map} that is itself bound with {@code annotation}. */ public static MapBinder newMapBinder(Binder binder, Class keyType, Class valueType, Annotation annotation) { return newMapBinder(binder, TypeLiteral.get(keyType), TypeLiteral.get(valueType), annotation); } /** * Returns a new mapbinder that collects entries of {@code keyType}/{@code valueType} in a * {@link Map} that is itself bound with {@code annotationType}. */ public static MapBinder newMapBinder(Binder binder, TypeLiteral keyType, TypeLiteral valueType, Class annotationType) { binder = binder.skipSources(MapBinder.class, RealMapBinder.class); return newMapBinder(binder, valueType, Key.get(mapOf(keyType, valueType), annotationType), Key.get(mapOfProviderOf(keyType, valueType), annotationType), Multibinder.newSetBinder(binder, entryOfProviderOf(keyType, valueType), annotationType)); } /** * Returns a new mapbinder that collects entries of {@code keyType}/{@code valueType} in a * {@link Map} that is itself bound with {@code annotationType}. */ public static MapBinder newMapBinder(Binder binder, Class keyType, Class valueType, Class annotationType) { return newMapBinder( binder, TypeLiteral.get(keyType), TypeLiteral.get(valueType), annotationType); } @SuppressWarnings("unchecked") // a map of is safely a Map private static TypeLiteral> mapOf( TypeLiteral keyType, TypeLiteral valueType) { return (TypeLiteral>) TypeLiteral.get( Types.mapOf(keyType.getType(), valueType.getType())); } @SuppressWarnings("unchecked") // a provider map is safely a Map> private static TypeLiteral>> mapOfProviderOf( TypeLiteral keyType, TypeLiteral valueType) { return (TypeLiteral>>) TypeLiteral.get( Types.mapOf(keyType.getType(), newParameterizedType(Provider.class, valueType.getType()))); } @SuppressWarnings("unchecked") // a provider entry is safely a Map.Entry> private static TypeLiteral>> entryOfProviderOf( TypeLiteral keyType, TypeLiteral valueType) { return (TypeLiteral>>) TypeLiteral.get(newParameterizedTypeWithOwner( Map.class, Entry.class, keyType.getType(), Types.providerOf(valueType.getType()))); } private static MapBinder newMapBinder(Binder binder, TypeLiteral valueType, Key> mapKey, Key>> providerMapKey, Multibinder>> entrySetBinder) { RealMapBinder mapBinder = new RealMapBinder<>( binder, valueType, mapKey, providerMapKey, entrySetBinder); binder.install(mapBinder); return mapBinder; } /** * Returns a binding builder used to add a new entry in the map. Each * key must be distinct (and non-null). Bound providers will be evaluated each * time the map is injected. *

* It is an error to call this method without also calling one of the * {@code to} methods on the returned binding builder. *

* Scoping elements independently is supported. Use the {@code in} method * to specify a binding scope. */ public abstract LinkedBindingBuilder addBinding(K key); /** * The actual mapbinder plays several roles: *

* As a MapBinder, it acts as a factory for LinkedBindingBuilders for * each of the map's values. It delegates to a {@link Multibinder} of * entries (keys to value providers). *

* As a Module, it installs the binding to the map itself, as well as to * a corresponding map whose values are providers. It uses the entry set * multibinder to construct the map and the provider map. *

* As a module, this implements equals() and hashcode() in order to trick * Guice into executing its configure() method only once. That makes it so * that multiple mapbinders can be created for the same target map, but * only one is bound. Since the list of bindings is retrieved from the * injector itself (and not the mapbinder), each mapbinder has access to * all contributions from all equivalent mapbinders. *

* Rather than binding a single Map.Entry<K, V>, the map binder * binds keys and values independently. This allows the values to be properly * scoped. *

* We use a subclass to hide 'implements Module' from the public API. */ public static final class RealMapBinder extends MapBinder implements Module { private final TypeLiteral valueType; private final Key> mapKey; private final Key>> providerMapKey; private final RealMultibinder>> entrySetBinder; /* the target injector's binder. non-null until initialization, null afterwards */ private Binder binder; private RealMapBinder(Binder binder, TypeLiteral valueType, Key> mapKey, Key>> providerMapKey, Multibinder>> entrySetBinder) { this.valueType = valueType; this.mapKey = mapKey; this.providerMapKey = providerMapKey; this.entrySetBinder = (RealMultibinder>>) entrySetBinder; this.binder = binder; } /** * This creates two bindings. One for the {@code Map.Entry>} * and another for {@code V}. */ @Override public LinkedBindingBuilder addBinding(K key) { Multibinder.checkNotNull(key, "key"); Multibinder.checkConfiguration(!isInitialized(), "MapBinder was already initialized"); Key valueKey = Key.get(valueType, new RealElement(entrySetBinder.getSetName())); entrySetBinder.addBinding().toInstance(new MapEntry<>(key, binder.getProvider(valueKey))); return binder.bind(valueKey); } public static class MapBinderProviderWithDependencies implements ProviderWithDependencies>> { private Map> providerMap; @SuppressWarnings("rawtypes") // code is silly stupid with generics private final RealMapBinder binder; private final Set> dependencies; private final Provider>>> provider; @SuppressWarnings("rawtypes") // code is silly stupid with generics MapBinderProviderWithDependencies(RealMapBinder binder, Set> dependencies, Provider>>> provider) { this.binder = binder; this.dependencies = dependencies; this.provider = provider; } @SuppressWarnings({"unchecked", "unused"}) // code is silly stupid with generics @Inject public void initialize() { binder.binder = null; Map> providerMapMutable = new LinkedHashMap<>(); for (Entry> entry : provider.get()) { Multibinder.checkConfiguration(providerMapMutable.put(entry.getKey(), entry.getValue()) == null, "Map injection failed due to duplicated key \"%s\"", entry.getKey()); } providerMap = Collections.unmodifiableMap(providerMapMutable); } @Override public Map> get() { return providerMap; } @Override public Set> getDependencies() { return dependencies; } } @Override @SuppressWarnings({"rawtypes", "unchecked"}) // code is silly stupid with generics public void configure(Binder binder) { Multibinder.checkConfiguration(!isInitialized(), "MapBinder was already initialized"); final Set> dependencies = singleton(Dependency.get(entrySetBinder.getSetKey())); // binds a Map> from a collection of Map> final Provider>>> entrySetProvider = binder .getProvider(entrySetBinder.getSetKey()); binder.bind(providerMapKey) .toProvider(new MapBinderProviderWithDependencies(RealMapBinder.this, dependencies, entrySetProvider)); final Provider>> mapProvider = binder.getProvider(providerMapKey); binder.bind(mapKey).toProvider(new ProviderWithDependencies>() { @Override public Map get() { Map map = new LinkedHashMap<>(); for (Entry> entry : mapProvider.get().entrySet()) { V value = entry.getValue().get(); K key = entry.getKey(); Multibinder.checkConfiguration(value != null, "Map injection failed due to null value for key \"%s\"", key); map.put(key, value); } return Collections.unmodifiableMap(map); } @Override public Set> getDependencies() { return dependencies; } }); } private boolean isInitialized() { return binder == null; } @Override public boolean equals(Object o) { return o instanceof RealMapBinder && ((RealMapBinder) o).mapKey.equals(mapKey); } @Override public int hashCode() { return mapKey.hashCode(); } private static final class MapEntry implements Map.Entry { private final K key; private final V value; private MapEntry(K key, V value) { this.key = key; this.value = value; } @Override public K getKey() { return key; } @Override public V getValue() { return value; } @Override public V setValue(V value) { throw new UnsupportedOperationException(); } @Override public boolean equals(Object obj) { return obj instanceof Map.Entry && key.equals(((Map.Entry) obj).getKey()) && value.equals(((Map.Entry) obj).getValue()); } @Override public int hashCode() { return 127 * ("key".hashCode() ^ key.hashCode()) + 127 * ("value".hashCode() ^ value.hashCode()); } @Override public String toString() { return "MapEntry(" + key + ", " + value + ")"; } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy