1 /* 2 * Copyright (C) 2010 Google Inc. 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 com.google.inject.multibindings; 18 19 import com.google.inject.Binding; 20 import com.google.inject.Key; 21 import com.google.inject.TypeLiteral; 22 import com.google.inject.spi.Element; 23 import com.google.inject.spi.Elements; 24 import java.util.List; 25 26 /** 27 * A binding for a Multibinder. 28 * 29 * @param <T> The fully qualified type of the set, including Set. For example: <code> 30 * MultibinderBinding<Set<Boolean>></code> 31 * @since 3.0 32 * @author sameb@google.com (Sam Berlin) 33 */ 34 public interface MultibinderBinding<T> { 35 36 /** Returns the key for the set. */ getSetKey()37 Key<T> getSetKey(); 38 39 /** 40 * Returns the TypeLiteral that describes the type of elements in the set. 41 * 42 * <p>The elements will always match the type Set's generic type. For example, if getSetKey 43 * returns a key of <code>Set<String></code>, then this will always return a <code> 44 * TypeLiteral<String></code>. 45 */ getElementTypeLiteral()46 TypeLiteral<?> getElementTypeLiteral(); 47 48 /** 49 * Returns all bindings that make up the set. This is only supported on bindings returned from an 50 * injector. This will throw {@link UnsupportedOperationException} if it is called on an element 51 * retrieved from {@link Elements#getElements}. 52 * 53 * <p>The elements will always match the type Set's generic type. For example, if getSetKey 54 * returns a key of <code>Set<String></code>, then this will always return a list of type 55 * <code>List<Binding<String>></code>. 56 */ getElements()57 List<Binding<?>> getElements(); 58 59 /** 60 * Returns true if the multibinder permits duplicates. This is only supported on bindings returned 61 * from an injector. This will throw {@link UnsupportedOperationException} if it is called on a 62 * MultibinderBinding retrieved from {@link Elements#getElements}. 63 */ permitsDuplicates()64 boolean permitsDuplicates(); 65 66 /** 67 * Returns true if this Multibinder uses the given Element. This will be true for bindings that 68 * derive the elements of the set and other bindings that Multibinder uses internally. This will 69 * work for MultibinderBindings retrieved from an injector and {@link Elements#getElements}. 70 * Usually this is only necessary if you are working with elements retrieved from modules (without 71 * an Injector), otherwise {@link #getElements} and {@link #permitsDuplicates} are better options. 72 * 73 * <p>If you need to introspect the details of the set, such as the values or if it permits 74 * duplicates, it is necessary to pass the elements through an Injector and use {@link 75 * #getElements()} and {@link #permitsDuplicates()}. 76 */ containsElement(Element element)77 boolean containsElement(Element element); 78 } 79