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 com.google.errorprone.annotations.DoNotMock;
020import java.util.Map;
021import org.checkerframework.checker.nullness.qual.Nullable;
022
023/**
024 * A map, each entry of which maps a {@link TypeToken} to an instance of that type. In addition to
025 * implementing {@code Map}, the additional type-safe operations {@link #putInstance} and {@link
026 * #getInstance} are available.
027 *
028 * <p>Generally, implementations don't support {@link #put} and {@link #putAll} because there is no
029 * way to check an object at runtime to be an instance of a {@link TypeToken}. Instead, caller
030 * should use the type safe {@link #putInstance}.
031 *
032 * <p>Also, if caller suppresses unchecked warnings and passes in an {@code Iterable<String>} for
033 * type {@code Iterable<Integer>}, the map won't be able to detect and throw type error.
034 *
035 * <p>Like any other {@code Map<Class, Object>}, this map may contain entries for primitive types,
036 * and a primitive type and its corresponding wrapper type may map to different values.
037 *
038 * @param <B> the common supertype that all entries must share; often this is simply {@link Object}
039 * @author Ben Yu
040 * @since 13.0
041 */
042@Beta
043@DoNotMock("Use ImmutableTypeToInstanceMap or MutableTypeToInstanceMap")
044public interface TypeToInstanceMap<B> extends Map<TypeToken<? extends B>, B> {
045
046  /**
047   * Returns the value the specified class is mapped to, or {@code null} if no entry for this class
048   * is present. This will only return a value that was bound to this specific class, not a value
049   * that may have been bound to a subtype.
050   *
051   * <p>{@code getInstance(Foo.class)} is equivalent to {@code
052   * getInstance(TypeToken.of(Foo.class))}.
053   */
054  <T extends B> @Nullable T getInstance(Class<T> type);
055
056  /**
057   * Returns the value the specified type is mapped to, or {@code null} if no entry for this type is
058   * present. This will only return a value that was bound to this specific type, not a value that
059   * may have been bound to a subtype.
060   */
061  <T extends B> @Nullable T getInstance(TypeToken<T> type);
062
063  /**
064   * Maps the specified class to the specified value. Does <i>not</i> associate this value with any
065   * of the class's supertypes.
066   *
067   * <p>{@code putInstance(Foo.class, foo)} is equivalent to {@code
068   * putInstance(TypeToken.of(Foo.class), foo)}.
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  <T extends B> @Nullable T putInstance(Class<T> type, @Nullable T value);
075
076  /**
077   * Maps the specified type to the specified value. Does <i>not</i> associate this value with any
078   * of the type's supertypes.
079   *
080   * @return the value previously associated with this type (possibly {@code null}), or {@code null}
081   *     if there was no previous entry.
082   */
083  @CanIgnoreReturnValue
084  <T extends B> @Nullable T putInstance(TypeToken<T> type, @Nullable T value);
085}