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