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.common.collect.ForwardingMap;
19  import com.google.common.collect.ImmutableMap;
20  import com.google.errorprone.annotations.CanIgnoreReturnValue;
21  import java.util.Map;
22  
23  /**
24   * A type-to-instance map backed by an {@link ImmutableMap}. See also
25   * {@link MutableTypeToInstanceMap}.
26   *
27   * @author Ben Yu
28   * @since 13.0
29   */
30  @Beta
31  public final class ImmutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B>
32      implements TypeToInstanceMap<B> {
33  
34    /** Returns an empty type to instance map. */
35    public static <B> ImmutableTypeToInstanceMap<B> of() {
36      return new ImmutableTypeToInstanceMap<B>(ImmutableMap.<TypeToken<? extends B>, B>of());
37    }
38  
39    /** Returns a new builder. */
40    public static <B> Builder<B> builder() {
41      return new Builder<B>();
42    }
43  
44    /**
45     * A builder for creating immutable type-to-instance maps. Example:
46     * <pre>   {@code
47     *
48     *   static final ImmutableTypeToInstanceMap<Handler<?>> HANDLERS =
49     *       ImmutableTypeToInstanceMap.<Handler<?>>builder()
50     *           .put(new TypeToken<Handler<Foo>>() {}, new FooHandler())
51     *           .put(new TypeToken<Handler<Bar>>() {}, new SubBarHandler())
52     *           .build();}</pre>
53     *
54     * <p>After invoking {@link #build()} it is still possible to add more entries and build again.
55     * Thus each map generated by this builder will be a superset of any map generated before it.
56     *
57     * @since 13.0
58     */
59    @Beta
60    public static final class Builder<B> {
61      private final ImmutableMap.Builder<TypeToken<? extends B>, B> mapBuilder =
62          ImmutableMap.builder();
63  
64      private Builder() {}
65  
66      /**
67       * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed,
68       * and will cause {@link #build} to fail.
69       */
70      @CanIgnoreReturnValue
71      public <T extends B> Builder<B> put(Class<T> key, T value) {
72        mapBuilder.put(TypeToken.of(key), value);
73        return this;
74      }
75  
76      /**
77       * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed,
78       * and will cause {@link #build} to fail.
79       */
80      @CanIgnoreReturnValue
81      public <T extends B> Builder<B> put(TypeToken<T> key, T value) {
82        mapBuilder.put(key.rejectTypeVariables(), value);
83        return this;
84      }
85  
86      /**
87       * Returns a new immutable type-to-instance map containing the entries provided to this builder.
88       *
89       * @throws IllegalArgumentException if duplicate keys were added
90       */
91      public ImmutableTypeToInstanceMap<B> build() {
92        return new ImmutableTypeToInstanceMap<B>(mapBuilder.build());
93      }
94    }
95  
96    private final ImmutableMap<TypeToken<? extends B>, B> delegate;
97  
98    private ImmutableTypeToInstanceMap(ImmutableMap<TypeToken<? extends B>, B> delegate) {
99      this.delegate = delegate;
100   }
101 
102   @Override
103   public <T extends B> T getInstance(TypeToken<T> type) {
104     return trustedGet(type.rejectTypeVariables());
105   }
106 
107   /**
108    * Guaranteed to throw an exception and leave the map unmodified.
109    *
110    * @deprecated unsupported operation
111    * @throws UnsupportedOperationException always
112    */
113   @CanIgnoreReturnValue
114   @Deprecated
115   @Override
116   public <T extends B> T putInstance(TypeToken<T> type, T value) {
117     throw new UnsupportedOperationException();
118   }
119 
120   @Override
121   public <T extends B> T getInstance(Class<T> type) {
122     return trustedGet(TypeToken.of(type));
123   }
124 
125   /**
126    * Guaranteed to throw an exception and leave the map unmodified.
127    *
128    * @deprecated unsupported operation
129    * @throws UnsupportedOperationException always
130    */
131   @CanIgnoreReturnValue
132   @Deprecated
133   @Override
134   public <T extends B> T putInstance(Class<T> type, T value) {
135     throw new UnsupportedOperationException();
136   }
137 
138   /**
139    * Guaranteed to throw an exception and leave the map unmodified.
140    *
141    * @deprecated unsupported operation
142    * @throws UnsupportedOperationException always
143    */
144   @CanIgnoreReturnValue
145   @Deprecated
146   @Override
147   public B put(TypeToken<? extends B> key, B value) {
148     throw new UnsupportedOperationException();
149   }
150 
151   /**
152    * Guaranteed to throw an exception and leave the map unmodified.
153    *
154    * @deprecated unsupported operation
155    * @throws UnsupportedOperationException always
156    */
157   @Deprecated
158   @Override
159   public void putAll(Map<? extends TypeToken<? extends B>, ? extends B> map) {
160     throw new UnsupportedOperationException();
161   }
162 
163   @Override
164   protected Map<TypeToken<? extends B>, B> delegate() {
165     return delegate;
166   }
167 
168   @SuppressWarnings("unchecked") // value could not get in if not a T
169   private <T extends B> T trustedGet(TypeToken<T> type) {
170     return (T) delegate.get(type);
171   }
172 }