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 com.google.errorprone.annotations.DoNotMock; 022import java.util.Map; 023import javax.annotation.CheckForNull; 024import org.checkerframework.checker.nullness.qual.NonNull; 025import org.checkerframework.checker.nullness.qual.Nullable; 026 027/** 028 * A map, each entry of which maps a Java <a href="http://tinyurl.com/2cmwkz">raw type</a> to an 029 * instance of that type. In addition to implementing {@code Map}, the additional type-safe 030 * operations {@link #putInstance} and {@link #getInstance} are available. 031 * 032 * <p>Like any other {@code Map<Class, Object>}, this map may contain entries for primitive types, 033 * and a primitive type and its corresponding wrapper type may map to different values. 034 * 035 * <h3>Implementations</h3> 036 * 037 * <ul> 038 * <li>{@link ImmutableClassToInstanceMap} 039 * <li>{@link MutableClassToInstanceMap} 040 * </ul> 041 * 042 * <p>To map a generic type to an instance of that type, use {@link 043 * com.google.common.reflect.TypeToInstanceMap} instead. 044 * 045 * <p>See the Guava User Guide article on <a href= 046 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#classtoinstancemap">{@code 047 * ClassToInstanceMap}</a>. 048 * 049 * @param <B> the common supertype that all values will share. When in doubt, just use {@link 050 * Object}, or use {@code @Nullable Object} to allow null values. 051 * @since 2.0 052 */ 053@DoNotMock("Use ImmutableClassToInstanceMap or MutableClassToInstanceMap") 054@GwtCompatible 055@ElementTypesAreNonnullByDefault 056public interface ClassToInstanceMap<B extends @Nullable Object> 057 extends Map<Class<? extends @NonNull B>, B> { 058 /** 059 * Returns the value the specified class is mapped to, or {@code null} if no entry for this class 060 * is present. This will only return a value that was bound to this specific class, not a value 061 * that may have been bound to a subtype. 062 */ 063 @CheckForNull 064 <T extends @NonNull B> T getInstance(Class<T> type); 065 066 /** 067 * Maps the specified class to the specified value. Does <i>not</i> associate this value with any 068 * of the class's supertypes. 069 * 070 * @return the value previously associated with this class (possibly {@code null}), or {@code 071 * null} if there was no previous entry. 072 */ 073 @CanIgnoreReturnValue 074 @CheckForNull 075 <T extends B> T putInstance(Class<@NonNull T> type, @ParametricNullness T value); 076}