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;
024
025/**
026 * A map, each entry of which maps a Java <a href="http://tinyurl.com/2cmwkz">raw type</a> to an
027 * instance of that type. In addition to implementing {@code Map}, the additional type-safe
028 * operations {@link #putInstance} and {@link #getInstance} are available.
029 *
030 * <p>Like any other {@code Map<Class, Object>}, this map may contain entries for primitive types,
031 * and a primitive type and its corresponding wrapper type may map to different values.
032 *
033 * <p>This class's support for {@code null} requires some explanation: From release 31.0 onward,
034 * Guava specifies the nullness of its types through annotations. In the case of {@code
035 * ClassToInstanceMap}, it specifies that both the key and value types are restricted to
036 * non-nullable types. This specification is reasonable for <i>keys</i>, which must be non-null
037 * classes. This is in contrast to the specification for <i>values</i>: Null values <i>are</i>
038 * supported by the implementation {@link MutableClassToInstanceMap}, even though that
039 * implementation and this interface specify otherwise. Thus, if you use a nullness checker, you can
040 * safely suppress any warnings it produces when you write null values into a {@code
041 * MutableClassToInstanceMap}. Just be sure to be prepared for null values when reading from it,
042 * since nullness checkers will assume that vaules are non-null then, too.
043 *
044 * <p>See the Guava User Guide article on <a href=
045 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#classtoinstancemap">{@code
046 * ClassToInstanceMap}</a>.
047 *
048 * <p>To map a generic type to an instance of that type, use {@link
049 * com.google.common.reflect.TypeToInstanceMap} instead.
050 *
051 * @param <B> the common supertype that all entries must share; often this is simply {@link Object}
052 * @author Kevin Bourrillion
053 * @since 2.0
054 */
055@DoNotMock("Use ImmutableClassToInstanceMap or MutableClassToInstanceMap")
056@GwtCompatible
057@ElementTypesAreNonnullByDefault
058// If we ever support non-null projections (https://github.com/jspecify/jspecify/issues/86), we
059// we might annotate this as...
060// ClassToInstanceMap<B extends @Nullable Object> extends Map<Class<? extends @Nonnull B>, B>
061// ...and change its methods similarly (<T extends @Nonnull B> or Class<@Nonnull T>).
062public interface ClassToInstanceMap<B> extends Map<Class<? extends B>, B> {
063  /**
064   * Returns the value the specified class is mapped to, or {@code null} if no entry for this class
065   * is present. This will only return a value that was bound to this specific class, not a value
066   * that may have been bound to a subtype.
067   */
068  @CheckForNull
069  <T extends B> T getInstance(Class<T> type);
070
071  /**
072   * Maps the specified class to the specified value. Does <i>not</i> associate this value with any
073   * of the class's supertypes.
074   *
075   * @return the value previously associated with this class (possibly {@code null}), or {@code
076   *     null} if there was no previous entry.
077   */
078  @CanIgnoreReturnValue
079  @CheckForNull
080  <T extends B> T putInstance(Class<T> type, T value);
081}