001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.reflect; 016 017import com.google.common.annotations.Beta; 018import com.google.errorprone.annotations.CanIgnoreReturnValue; 019import java.util.Map; 020import javax.annotation.Nullable; 021 022/** 023 * A map, each entry of which maps a {@link TypeToken} to an instance of that type. In addition to 024 * implementing {@code Map}, the additional type-safe operations {@link #putInstance} and 025 * {@link #getInstance} are available. 026 * 027 * <p>Generally, implementations don't support {@link #put} and {@link #putAll} because there is no 028 * way to check an object at runtime to be an instance of a {@link TypeToken}. Instead, caller 029 * should use the type safe {@link #putInstance}. 030 * 031 * <p>Also, if caller suppresses unchecked warnings and passes in an {@code Iterable<String>} for 032 * type {@code Iterable<Integer>}, the map won't be able to detect and throw type error. 033 * 034 * <p>Like any other {@code Map<Class, Object>}, this map may contain entries for primitive types, 035 * and a primitive type and its corresponding wrapper type may map to different values. 036 * 037 * @param <B> the common supertype that all entries must share; often this is simply {@link Object} 038 * 039 * @author Ben Yu 040 * @since 13.0 041 */ 042@Beta 043public interface TypeToInstanceMap<B> extends Map<TypeToken<? extends B>, B> { 044 045 /** 046 * Returns the value the specified class is mapped to, or {@code null} if no entry for this class 047 * is present. This will only return a value that was bound to this specific class, not a value 048 * that may have been bound to a subtype. 049 * 050 * <p>{@code getInstance(Foo.class)} is equivalent to 051 * {@code getInstance(TypeToken.of(Foo.class))}. 052 */ 053 @Nullable 054 <T extends B> T getInstance(Class<T> type); 055 056 /** 057 * Maps the specified class to the specified value. Does <i>not</i> associate this value with any 058 * of the class's supertypes. 059 * 060 * <p>{@code putInstance(Foo.class, foo)} is equivalent to 061 * {@code putInstance(TypeToken.of(Foo.class), foo)}. 062 * 063 * @return the value previously associated with this class (possibly {@code null}), or 064 * {@code null} if there was no previous entry. 065 */ 066 @Nullable 067 @CanIgnoreReturnValue 068 <T extends B> T putInstance(Class<T> type, @Nullable T value); 069 070 /** 071 * Returns the value the specified type is mapped to, or {@code null} if no entry for this type is 072 * present. This will only return a value that was bound to this specific type, not a value that 073 * may have been bound to a subtype. 074 */ 075 @Nullable 076 <T extends B> T getInstance(TypeToken<T> type); 077 078 /** 079 * Maps the specified type to the specified value. Does <i>not</i> associate this value with any 080 * of the type's supertypes. 081 * 082 * @return the value previously associated with this type (possibly {@code null}), or {@code null} 083 * if there was no previous entry. 084 */ 085 @Nullable 086 @CanIgnoreReturnValue 087 <T extends B> T putInstance(TypeToken<T> type, @Nullable T value); 088}