001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import com.google.common.annotations.GwtCompatible; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.util.Map; 022import org.checkerframework.checker.nullness.qual.Nullable; 023 024/** 025 * A map, each entry of which maps a Java <a href="http://tinyurl.com/2cmwkz">raw type</a> to an 026 * instance of that type. In addition to implementing {@code Map}, the additional type-safe 027 * operations {@link #putInstance} and {@link #getInstance} are available. 028 * 029 * <p>Like any other {@code Map<Class, Object>}, this map may contain entries for primitive types, 030 * and a primitive type and its corresponding wrapper type may map to different values. 031 * 032 * <p>See the Guava User Guide article on <a href= 033 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#classtoinstancemap"> {@code 034 * ClassToInstanceMap}</a>. 035 * 036 * <p>To map a generic type to an instance of that type, use {@link 037 * com.google.common.reflect.TypeToInstanceMap} instead. 038 * 039 * @param <B> the common supertype that all entries must share; often this is simply {@link Object} 040 * @author Kevin Bourrillion 041 * @since 2.0 042 */ 043@GwtCompatible 044public interface ClassToInstanceMap<B> extends Map<Class<? extends B>, B> { 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 <T extends B> T getInstance(Class<T> type); 051 052 /** 053 * Maps the specified class to the specified value. Does <i>not</i> associate this value with any 054 * of the class's supertypes. 055 * 056 * @return the value previously associated with this class (possibly {@code null}), or {@code 057 * null} if there was no previous entry. 058 */ 059 @CanIgnoreReturnValue 060 <T extends B> T putInstance(Class<T> type, @Nullable T value); 061}