aboutsummaryrefslogtreecommitdiffstats
path: root/guava/src/com/google/common/reflect/ImmutableTypeToInstanceMap.java
blob: 43e5e1e51c40b0b8782e55a58456c6854a0f57dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2012 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.reflect;

import com.google.common.annotations.Beta;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

/**
 * A type-to-instance map backed by an {@link ImmutableMap}. See also {@link
 * MutableTypeToInstanceMap}.
 *
 * @author Ben Yu
 * @since 13.0
 */
@Beta
public final class ImmutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B>
    implements TypeToInstanceMap<B> {

  /** Returns an empty type to instance map. */
  public static <B> ImmutableTypeToInstanceMap<B> of() {
    return new ImmutableTypeToInstanceMap<B>(ImmutableMap.<TypeToken<? extends B>, B>of());
  }

  /** Returns a new builder. */
  public static <B> Builder<B> builder() {
    return new Builder<B>();
  }

  /**
   * A builder for creating immutable type-to-instance maps. Example:
   * <pre>   {@code
   *
   *   static final ImmutableTypeToInstanceMap<Handler<?>> HANDLERS =
   *       ImmutableTypeToInstanceMap.<Handler<?>>builder()
   *           .put(new TypeToken<Handler<Foo>>() {}, new FooHandler())
   *           .put(new TypeToken<Handler<Bar>>() {}, new SubBarHandler())
   *           .build();}</pre>
   *
   * After invoking {@link #build()} it is still possible to add more entries
   * and build again. Thus each map generated by this builder will be a superset
   * of any map generated before it.
   *
   * @since 13.0
   */
  @Beta
  public static final class Builder<B> {
    private final ImmutableMap.Builder<TypeToken<? extends B>, B> mapBuilder
        = ImmutableMap.builder();

    private Builder() {}

    /**
     * Associates {@code key} with {@code value} in the built map. Duplicate
     * keys are not allowed, and will cause {@link #build} to fail.
     */
    public <T extends B> Builder<B> put(Class<T> key, T value) {
      mapBuilder.put(TypeToken.of(key), value);
      return this;
    }

    /**
     * Associates {@code key} with {@code value} in the built map. Duplicate
     * keys are not allowed, and will cause {@link #build} to fail.
     */
    public <T extends B> Builder<B> put(TypeToken<T> key, T value) {
      mapBuilder.put(key.rejectTypeVariables(), value);
      return this;
    }

    /**
     * Returns a new immutable type-to-instance map containing the entries
     * provided to this builder.
     *
     * @throws IllegalArgumentException if duplicate keys were added
     */
    public ImmutableTypeToInstanceMap<B> build() {
      return new ImmutableTypeToInstanceMap<B>(mapBuilder.build());
    }
  }

  private final ImmutableMap<TypeToken<? extends B>, B> delegate;

  private ImmutableTypeToInstanceMap(ImmutableMap<TypeToken<? extends B>, B> delegate) {
    this.delegate = delegate;
  }

  @Override public <T extends B> T getInstance(TypeToken<T> type) {
    return trustedGet(type.rejectTypeVariables());
  }

  /**
   * Guaranteed to throw an exception and leave the map unmodified.
   *
   * @throws UnsupportedOperationException always
   */
  @Override public <T extends B> T putInstance(TypeToken<T> type, T value) {
    throw new UnsupportedOperationException();
  }

  @Override public <T extends B> T getInstance(Class<T> type) {
    return trustedGet(TypeToken.of(type));
  }

  /**
   * Guaranteed to throw an exception and leave the map unmodified.
   *
   * @throws UnsupportedOperationException always
   */
  @Override public <T extends B> T putInstance(Class<T> type, T value) {
    throw new UnsupportedOperationException();
  }

  @Override protected Map<TypeToken<? extends B>, B> delegate() {
    return delegate;
  }

  @SuppressWarnings("unchecked") // value could not get in if not a T
  private <T extends B> T trustedGet(TypeToken<T> type) {
    return (T) delegate.get(type);
  }
}