1 /* 2 * Copyright (C) 2012 The Guava 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 com.google.common.reflect; 18 19 import com.google.common.annotations.Beta; 20 21 import java.util.Map; 22 23 import javax.annotation.Nullable; 24 25 /** 26 * A map, each entry of which maps a {@link TypeToken} to an instance of that type. 27 * In addition to implementing {@code Map}, the additional type-safe operations 28 * {@link #putInstance} and {@link #getInstance} are available. 29 * 30 * <p>Generally, implementations don't support {@link #put} and {@link #putAll} 31 * because there is no way to check an object at runtime to be an instance of a 32 * {@link TypeToken}. Instead, caller should use the type safe {@link #putInstance}. 33 * 34 * <p>Also, if caller suppresses unchecked warnings and passes in an {@code Iterable<String>} 35 * for type {@code Iterable<Integer>}, the map won't be able to detect and throw type error. 36 * 37 * <p>Like any other {@code Map<Class, Object>}, this map may contain entries 38 * for primitive types, and a primitive type and its corresponding wrapper type 39 * may map to different values. 40 * 41 * @param <B> the common supertype that all entries must share; often this is 42 * simply {@link Object} 43 * 44 * @author Ben Yu 45 * @since 13.0 46 */ 47 @Beta 48 public interface TypeToInstanceMap<B> extends Map<TypeToken<? extends B>, B> { 49 50 /** 51 * Returns the value the specified class is mapped to, or {@code null} if no 52 * entry for this class is present. This will only return a value that was 53 * bound to this specific class, not a value that may have been bound to a 54 * subtype. 55 * 56 * <p>{@code getInstance(Foo.class)} is equivalent to 57 * {@code getInstance(TypeToken.of(Foo.class))}. 58 */ 59 @Nullable 60 <T extends B> T getInstance(Class<T> type); 61 62 /** 63 * Maps the specified class to the specified value. Does <i>not</i> associate 64 * this value with any of the class's supertypes. 65 * 66 * <p>{@code putInstance(Foo.class, foo)} is equivalent to 67 * {@code putInstance(TypeToken.of(Foo.class), foo)}. 68 * 69 * @return the value previously associated with this class (possibly {@code null}), 70 * or {@code null} if there was no previous entry. 71 */ 72 @Nullable 73 <T extends B> T putInstance(Class<T> type, @Nullable T value); 74 75 /** 76 * Returns the value the specified type is mapped to, or {@code null} if no 77 * entry for this type is present. This will only return a value that was 78 * bound to this specific type, not a value that may have been bound to a subtype. 79 */ 80 @Nullable 81 <T extends B> T getInstance(TypeToken<T> type); 82 83 /** 84 * Maps the specified type to the specified value. Does <i>not</i> associate 85 * this value with any of the type's supertypes. 86 * 87 * @return the value previously associated with this type (possibly {@code null}), 88 * or {@code null} if there was no previous entry. 89 */ 90 @Nullable 91 <T extends B> T putInstance(TypeToken<T> type, @Nullable T value); 92 }