ImmutableBiMap.java revision 090f9b4c879985bc747c214f82c62471e60c7742
1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
2090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Copyright (C) 2008 Google Inc.
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Map;
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * An immutable {@link BiMap} with reliable user-specified iteration order. Does
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * not permit null keys or values. An {@code ImmutableBiMap} and its inverse
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * have the same iteration ordering.
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>An instance of {@code ImmutableBiMap} contains its own data and will
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <i>never</i> change. {@code ImmutableBiMap} is convenient for
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code public static final} maps ("constant maps") and also lets you easily
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * make a "defensive copy" of a bimap provided to your class by a caller.
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Note</b>: Although this class is not final, it cannot be subclassed as
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * it has no public or protected constructors. Thus, instances of this class are
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * guaranteed to be immutable.
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible(serializable = true)
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic abstract class ImmutableBiMap<K, V> extends ImmutableMap<K, V>
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    implements BiMap<K, V> {
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final ImmutableBiMap<Object, Object> EMPTY_IMMUTABLE_BIMAP
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      = new EmptyBiMap();
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the empty bimap.
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting to any type is safe because the set will never hold any elements.
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of() {
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (ImmutableBiMap<K, V>) EMPTY_IMMUTABLE_BIMAP;
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable bimap containing a single entry.
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(K k1, V v1) {
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(k1, v1));
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable map containing the given entries, in order.
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if duplicate keys or values are added
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(K k1, V v1, K k2, V v2) {
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(k1, v1, k2, v2));
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable map containing the given entries, in order.
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if duplicate keys or values are added
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3) {
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        k1, v1, k2, v2, k3, v3));
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable map containing the given entries, in order.
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if duplicate keys or values are added
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        k1, v1, k2, v2, k3, v3, k4, v4));
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable map containing the given entries, in order.
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if duplicate keys or values are added
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        k1, v1, k2, v2, k3, v3, k4, v4, k5, v5));
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // looking for of() with > 5 entries? Use the builder instead.
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a new builder. The generated builder is equivalent to the builder
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * created by the {@link Builder} constructor.
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> Builder<K, V> builder() {
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Builder<K, V>();
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * A builder for creating immutable bimap instances, especially {@code public
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * static final} bimaps ("constant bimaps"). Example: <pre>   {@code
119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   static final ImmutableBiMap<String, Integer> WORD_TO_INT =
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *       new ImmutableBiMap.Builder<String, Integer>()
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .put("one", 1)
123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .put("two", 2)
124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .put("three", 3)
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .build();}</pre>
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * For <i>small</i> immutable bimaps, the {@code ImmutableBiMap.of()} methods
128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * are even more convenient.
129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Builder instances can be reused - it is safe to call {@link #build}
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * multiple times to build multiple bimaps in series. Each bimap is a superset
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * of the bimaps created before it.
133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static final class Builder<K, V> extends ImmutableMap.Builder<K, V> {
135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Creates a new builder. The returned builder is equivalent to the builder
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * generated by {@link ImmutableBiMap#builder}.
139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Builder() {}
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Associates {@code key} with {@code value} in the built bimap. Duplicate
144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * keys or values are not allowed, and will cause {@link #build} to fail.
145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Builder<K, V> put(K key, V value) {
147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super.put(key, value);
148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Associates all of the given map's keys and values in the built bimap.
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Duplicate keys or values are not allowed, and will cause {@link #build}
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * to fail.
155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @throws NullPointerException if any key or value in {@code map} is null
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super.putAll(map);
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Returns a newly-created immutable bimap.
165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @throws IllegalArgumentException if duplicate keys or values were added
167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public ImmutableBiMap<K, V> build() {
169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ImmutableMap<K, V> map = super.build();
170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (map.isEmpty()) {
171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return of();
172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return new RegularImmutableBiMap<K, V>(super.build());
174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable bimap containing the same entries as {@code map}. If
179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code map} somehow contains entries with duplicate keys (for example, if
180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * it is a {@code SortedMap} whose comparator is not <i>consistent with
181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * equals</i>), the results of this method are undefined.
182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Note:</b> If {@code map} is an {@code ImmutableBiMap}, the given map
184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * itself will be returned.
185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if two keys have the same value
187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NullPointerException if any key or value in {@code map} is null
188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> copyOf(
190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Map<? extends K, ? extends V> map) {
191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (map instanceof ImmutableBiMap) {
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      @SuppressWarnings("unchecked") // safe since map is not writable
193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map;
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return bimap;
195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (map.isEmpty()) {
198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return of();
199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableMap<K, V> immutableMap = ImmutableMap.copyOf(map);
202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(immutableMap);
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  ImmutableBiMap() {}
206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  abstract ImmutableMap<K, V> delegate();
208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@inheritDoc}
211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The inverse of an {@code ImmutableBiMap} is another
213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code ImmutableBiMap}.
214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public abstract ImmutableBiMap<V, K> inverse();
216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean containsKey(@Nullable Object key) {
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().containsKey(key);
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean containsValue(@Nullable Object value) {
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return inverse().containsKey(value);
223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableSet<Entry<K, V>> entrySet() {
226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().entrySet();
227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public V get(@Nullable Object key) {
230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().get(key);
231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableSet<K> keySet() {
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().keySet();
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable set of the values in this map. The values are in the
239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * same order as the parameters used to build this map.
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableSet<V> values() {
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return inverse().keySet();
243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the bimap unmodified.
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public V forcePut(K key, V value) {
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean isEmpty() {
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().isEmpty();
256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public int size() {
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().size();
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean equals(@Nullable Object object) {
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return object == this || delegate().equals(object);
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public int hashCode() {
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().hashCode();
268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public String toString() {
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().toString();
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** Bimap with no mappings. */
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("serial") // uses writeReplace(), not default serialization
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static class EmptyBiMap extends ImmutableBiMap<Object, Object> {
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override ImmutableMap<Object, Object> delegate() {
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return ImmutableMap.of();
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public ImmutableBiMap<Object, Object> inverse() {
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Object readResolve() {
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return EMPTY_IMMUTABLE_BIMAP; // preserve singleton property
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Serialized type for all ImmutableBiMap instances. It captures the logical
290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * contents and they are reconstructed using public factory methods. This
291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * ensures that the implementation types remain as implementation details.
292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Since the bimap is immutable, ImmutableBiMap doesn't require special logic
294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * for keeping the bimap and its inverse in sync during serialization, the way
295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * AbstractBiMap does.
296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class SerializedForm extends ImmutableMap.SerializedForm {
298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    SerializedForm(ImmutableBiMap<?, ?> bimap) {
299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super(bimap);
300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override Object readResolve() {
302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Builder<Object, Object> builder = new Builder<Object, Object>();
303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return createMap(builder);
304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override Object writeReplace() {
309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new SerializedForm(this);
310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
312