View Javadoc
1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.reflect;
16  
17  import com.google.common.annotations.Beta;
18  import com.google.errorprone.annotations.CanIgnoreReturnValue;
19  import java.util.Map;
20  import javax.annotation.Nullable;
21  
22  /**
23   * A map, each entry of which maps a {@link TypeToken} to an instance of that type. In addition to
24   * implementing {@code Map}, the additional type-safe operations {@link #putInstance} and
25   * {@link #getInstance} are available.
26   *
27   * <p>Generally, implementations don't support {@link #put} and {@link #putAll} because there is no
28   * way to check an object at runtime to be an instance of a {@link TypeToken}. Instead, caller
29   * should use the type safe {@link #putInstance}.
30   *
31   * <p>Also, if caller suppresses unchecked warnings and passes in an {@code Iterable<String>} for
32   * type {@code Iterable<Integer>}, the map won't be able to detect and throw type error.
33   *
34   * <p>Like any other {@code Map<Class, Object>}, this map may contain entries for primitive types,
35   * and a primitive type and its corresponding wrapper type may map to different values.
36   *
37   * @param <B> the common supertype that all entries must share; often this is simply {@link Object}
38   *
39   * @author Ben Yu
40   * @since 13.0
41   */
42  @Beta
43  public interface TypeToInstanceMap<B> extends Map<TypeToken<? extends B>, B> {
44  
45    /**
46     * Returns the value the specified class is mapped to, or {@code null} if no entry for this class
47     * is present. This will only return a value that was bound to this specific class, not a value
48     * that may have been bound to a subtype.
49     *
50     * <p>{@code getInstance(Foo.class)} is equivalent to
51     * {@code getInstance(TypeToken.of(Foo.class))}.
52     */
53    @Nullable
54    <T extends B> T getInstance(Class<T> type);
55  
56    /**
57     * Maps the specified class to the specified value. Does <i>not</i> associate this value with any
58     * of the class's supertypes.
59     *
60     * <p>{@code putInstance(Foo.class, foo)} is equivalent to
61     * {@code putInstance(TypeToken.of(Foo.class), foo)}.
62     *
63     * @return the value previously associated with this class (possibly {@code null}), or
64     *     {@code null} if there was no previous entry.
65     */
66    @Nullable
67    @CanIgnoreReturnValue
68    <T extends B> T putInstance(Class<T> type, @Nullable T value);
69  
70    /**
71     * Returns the value the specified type is mapped to, or {@code null} if no entry for this type is
72     * present. This will only return a value that was bound to this specific type, not a value that
73     * may have been bound to a subtype.
74     */
75    @Nullable
76    <T extends B> T getInstance(TypeToken<T> type);
77  
78    /**
79     * Maps the specified type to the specified value. Does <i>not</i> associate this value with any
80     * of the type's supertypes.
81     *
82     * @return the value previously associated with this type (possibly {@code null}), or {@code null}
83     *     if there was no previous entry.
84     */
85    @Nullable
86    @CanIgnoreReturnValue
87    <T extends B> T putInstance(TypeToken<T> type, @Nullable T value);
88  }