17dd252788645e940eada959bdde927426e2531c9Paul Duffin/*
27dd252788645e940eada959bdde927426e2531c9Paul Duffin * Copyright (C) 2012 The Guava Authors
37dd252788645e940eada959bdde927426e2531c9Paul Duffin *
47dd252788645e940eada959bdde927426e2531c9Paul Duffin * Licensed under the Apache License, Version 2.0 (the "License");
57dd252788645e940eada959bdde927426e2531c9Paul Duffin * you may not use this file except in compliance with the License.
67dd252788645e940eada959bdde927426e2531c9Paul Duffin * You may obtain a copy of the License at
77dd252788645e940eada959bdde927426e2531c9Paul Duffin *
87dd252788645e940eada959bdde927426e2531c9Paul Duffin * http://www.apache.org/licenses/LICENSE-2.0
97dd252788645e940eada959bdde927426e2531c9Paul Duffin *
107dd252788645e940eada959bdde927426e2531c9Paul Duffin * Unless required by applicable law or agreed to in writing, software
117dd252788645e940eada959bdde927426e2531c9Paul Duffin * distributed under the License is distributed on an "AS IS" BASIS,
127dd252788645e940eada959bdde927426e2531c9Paul Duffin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137dd252788645e940eada959bdde927426e2531c9Paul Duffin * See the License for the specific language governing permissions and
147dd252788645e940eada959bdde927426e2531c9Paul Duffin * limitations under the License.
157dd252788645e940eada959bdde927426e2531c9Paul Duffin */
167dd252788645e940eada959bdde927426e2531c9Paul Duffin
177dd252788645e940eada959bdde927426e2531c9Paul Duffinpackage com.google.common.reflect;
187dd252788645e940eada959bdde927426e2531c9Paul Duffin
197dd252788645e940eada959bdde927426e2531c9Paul Duffinimport com.google.common.annotations.Beta;
207dd252788645e940eada959bdde927426e2531c9Paul Duffin
217dd252788645e940eada959bdde927426e2531c9Paul Duffinimport java.util.Map;
227dd252788645e940eada959bdde927426e2531c9Paul Duffin
237dd252788645e940eada959bdde927426e2531c9Paul Duffinimport javax.annotation.Nullable;
247dd252788645e940eada959bdde927426e2531c9Paul Duffin
257dd252788645e940eada959bdde927426e2531c9Paul Duffin/**
267dd252788645e940eada959bdde927426e2531c9Paul Duffin * A map, each entry of which maps a {@link TypeToken} to an instance of that type.
277dd252788645e940eada959bdde927426e2531c9Paul Duffin * In addition to implementing {@code Map}, the additional type-safe operations
287dd252788645e940eada959bdde927426e2531c9Paul Duffin * {@link #putInstance} and {@link #getInstance} are available.
297dd252788645e940eada959bdde927426e2531c9Paul Duffin *
307dd252788645e940eada959bdde927426e2531c9Paul Duffin * <p>Generally, implementations don't support {@link #put} and {@link #putAll}
317dd252788645e940eada959bdde927426e2531c9Paul Duffin * because there is no way to check an object at runtime to be an instance of a
327dd252788645e940eada959bdde927426e2531c9Paul Duffin * {@link TypeToken}. Instead, caller should use the type safe {@link #putInstance}.
337dd252788645e940eada959bdde927426e2531c9Paul Duffin *
347dd252788645e940eada959bdde927426e2531c9Paul Duffin * <p>Also, if caller suppresses unchecked warnings and passes in an {@code Iterable<String>}
357dd252788645e940eada959bdde927426e2531c9Paul Duffin * for type {@code Iterable<Integer>}, the map won't be able to detect and throw type error.
367dd252788645e940eada959bdde927426e2531c9Paul Duffin *
377dd252788645e940eada959bdde927426e2531c9Paul Duffin * <p>Like any other {@code Map<Class, Object>}, this map may contain entries
387dd252788645e940eada959bdde927426e2531c9Paul Duffin * for primitive types, and a primitive type and its corresponding wrapper type
397dd252788645e940eada959bdde927426e2531c9Paul Duffin * may map to different values.
407dd252788645e940eada959bdde927426e2531c9Paul Duffin *
417dd252788645e940eada959bdde927426e2531c9Paul Duffin * @param <B> the common supertype that all entries must share; often this is
427dd252788645e940eada959bdde927426e2531c9Paul Duffin *     simply {@link Object}
437dd252788645e940eada959bdde927426e2531c9Paul Duffin *
447dd252788645e940eada959bdde927426e2531c9Paul Duffin * @author Ben Yu
457dd252788645e940eada959bdde927426e2531c9Paul Duffin * @since 13.0
467dd252788645e940eada959bdde927426e2531c9Paul Duffin */
477dd252788645e940eada959bdde927426e2531c9Paul Duffin@Beta
480888a09821a98ac0680fad765217302858e70fa4Paul Duffinpublic interface TypeToInstanceMap<B> extends Map<TypeToken<? extends B>, B>  {
497dd252788645e940eada959bdde927426e2531c9Paul Duffin
507dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
517dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Returns the value the specified class is mapped to, or {@code null} if no
527dd252788645e940eada959bdde927426e2531c9Paul Duffin   * entry for this class is present. This will only return a value that was
537dd252788645e940eada959bdde927426e2531c9Paul Duffin   * bound to this specific class, not a value that may have been bound to a
547dd252788645e940eada959bdde927426e2531c9Paul Duffin   * subtype.
557dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
567dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p>{@code getInstance(Foo.class)} is equivalent to
577dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code getInstance(TypeToken.of(Foo.class))}.
587dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
597dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
607dd252788645e940eada959bdde927426e2531c9Paul Duffin  <T extends B> T getInstance(Class<T> type);
617dd252788645e940eada959bdde927426e2531c9Paul Duffin
627dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
637dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Maps the specified class to the specified value. Does <i>not</i> associate
647dd252788645e940eada959bdde927426e2531c9Paul Duffin   * this value with any of the class's supertypes.
657dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
667dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p>{@code putInstance(Foo.class, foo)} is equivalent to
677dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code putInstance(TypeToken.of(Foo.class), foo)}.
687dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
697dd252788645e940eada959bdde927426e2531c9Paul Duffin   * @return the value previously associated with this class (possibly {@code null}),
707dd252788645e940eada959bdde927426e2531c9Paul Duffin   *         or {@code null} if there was no previous entry.
717dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
727dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
737dd252788645e940eada959bdde927426e2531c9Paul Duffin  <T extends B> T putInstance(Class<T> type, @Nullable T value);
747dd252788645e940eada959bdde927426e2531c9Paul Duffin
757dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
767dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Returns the value the specified type is mapped to, or {@code null} if no
777dd252788645e940eada959bdde927426e2531c9Paul Duffin   * entry for this type is present. This will only return a value that was
787dd252788645e940eada959bdde927426e2531c9Paul Duffin   * bound to this specific type, not a value that may have been bound to a subtype.
797dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
807dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
817dd252788645e940eada959bdde927426e2531c9Paul Duffin  <T extends B> T getInstance(TypeToken<T> type);
827dd252788645e940eada959bdde927426e2531c9Paul Duffin
837dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
847dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Maps the specified type to the specified value. Does <i>not</i> associate
857dd252788645e940eada959bdde927426e2531c9Paul Duffin   * this value with any of the type's supertypes.
867dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
877dd252788645e940eada959bdde927426e2531c9Paul Duffin   * @return the value previously associated with this type (possibly {@code null}),
887dd252788645e940eada959bdde927426e2531c9Paul Duffin   *         or {@code null} if there was no previous entry.
897dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
907dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
917dd252788645e940eada959bdde927426e2531c9Paul Duffin  <T extends B> T putInstance(TypeToken<T> type, @Nullable T value);
927dd252788645e940eada959bdde927426e2531c9Paul Duffin}
93