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