1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2008 The Guava Authors
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 *
351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <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
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(serializable = true, emulated = true)
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic abstract class ImmutableBiMap<K, V> extends ImmutableMap<K, V>
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    implements BiMap<K, V> {
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final ImmutableBiMap<Object, Object> EMPTY_IMMUTABLE_BIMAP
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      = new EmptyBiMap();
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the empty bimap.
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting to any type is safe because the set will never hold any elements.
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of() {
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (ImmutableBiMap<K, V>) EMPTY_IMMUTABLE_BIMAP;
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable bimap containing a single entry.
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(K k1, V v1) {
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(k1, v1));
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable map containing the given entries, in order.
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if duplicate keys or values are added
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(K k1, V v1, K k2, V v2) {
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(k1, v1, k2, v2));
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable map containing the given entries, in order.
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if duplicate keys or values are added
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3) {
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        k1, v1, k2, v2, k3, v3));
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable map containing the given entries, in order.
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if duplicate keys or values are added
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        k1, v1, k2, v2, k3, v3, k4, v4));
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable map containing the given entries, in order.
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if duplicate keys or values are added
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> of(
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        k1, v1, k2, v2, k3, v3, k4, v4, k5, v5));
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // looking for of() with > 5 entries? Use the builder instead.
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a new builder. The generated builder is equivalent to the builder
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * created by the {@link Builder} constructor.
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> Builder<K, V> builder() {
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Builder<K, V>();
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * A builder for creating immutable bimap instances, especially {@code public
119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * static final} bimaps ("constant bimaps"). Example: <pre>   {@code
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   static final ImmutableBiMap<String, Integer> WORD_TO_INT =
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *       new ImmutableBiMap.Builder<String, Integer>()
123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .put("one", 1)
124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .put("two", 2)
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .put("three", 3)
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .build();}</pre>
127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * For <i>small</i> immutable bimaps, the {@code ImmutableBiMap.of()} methods
129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * are even more convenient.
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Builder instances can be reused - it is safe to call {@link #build}
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * multiple times to build multiple bimaps in series. Each bimap is a superset
133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * of the bimaps created before it.
1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0 (imported from Google Collections Library)
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static final class Builder<K, V> extends ImmutableMap.Builder<K, V> {
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Creates a new builder. The returned builder is equivalent to the builder
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * generated by {@link ImmutableBiMap#builder}.
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Builder() {}
144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Associates {@code key} with {@code value} in the built bimap. Duplicate
147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * keys or values are not allowed, and will cause {@link #build} to fail.
148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Builder<K, V> put(K key, V value) {
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super.put(key, value);
151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Associates all of the given map's keys and values in the built bimap.
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Duplicate keys or values are not allowed, and will cause {@link #build}
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * to fail.
158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @throws NullPointerException if any key or value in {@code map} is null
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super.putAll(map);
163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Returns a newly-created immutable bimap.
168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * @throws IllegalArgumentException if duplicate keys or values were added
170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public ImmutableBiMap<K, V> build() {
172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ImmutableMap<K, V> map = super.build();
173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (map.isEmpty()) {
174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return of();
175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return new RegularImmutableBiMap<K, V>(map);
177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable bimap containing the same entries as {@code map}. If
182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code map} somehow contains entries with duplicate keys (for example, if
183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * it is a {@code SortedMap} whose comparator is not <i>consistent with
184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * equals</i>), the results of this method are undefined.
185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Despite the method name, this method attempts to avoid actually copying
1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the data when it is safe to do so. The exact circumstances under which a
1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * copy will or will not be performed are undocumented and subject to change.
189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if two keys have the same value
191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NullPointerException if any key or value in {@code map} is null
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableBiMap<K, V> copyOf(
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Map<? extends K, ? extends V> map) {
195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (map instanceof ImmutableBiMap) {
196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      @SuppressWarnings("unchecked") // safe since map is not writable
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map;
1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // TODO(user): if we need to make a copy of a BiMap because the
1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // forward map is a view, don't make a copy of the non-view delegate map
2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (!bimap.isPartialView()) {
2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return bimap;
2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (map.isEmpty()) {
206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return of();
207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableMap<K, V> immutableMap = ImmutableMap.copyOf(map);
210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new RegularImmutableBiMap<K, V>(immutableMap);
211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  ImmutableBiMap() {}
214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  abstract ImmutableMap<K, V> delegate();
216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@inheritDoc}
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The inverse of an {@code ImmutableBiMap} is another
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code ImmutableBiMap}.
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public abstract ImmutableBiMap<V, K> inverse();
225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean containsKey(@Nullable Object key) {
227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().containsKey(key);
228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean containsValue(@Nullable Object value) {
231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return inverse().containsKey(value);
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableSet<Entry<K, V>> entrySet() {
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().entrySet();
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public V get(@Nullable Object key) {
239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().get(key);
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableSet<K> keySet() {
243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().keySet();
244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable set of the values in this map. The values are in the
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * same order as the parameters used to build this map.
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableSet<V> values() {
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return inverse().keySet();
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the bimap unmodified.
256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public V forcePut(K key, V value) {
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean isEmpty() {
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().isEmpty();
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public int size() {
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().size();
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean equals(@Nullable Object object) {
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return object == this || delegate().equals(object);
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public int hashCode() {
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().hashCode();
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public String toString() {
282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return delegate().toString();
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** Bimap with no mappings. */
286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("serial") // uses writeReplace(), not default serialization
287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static class EmptyBiMap extends ImmutableBiMap<Object, Object> {
288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override ImmutableMap<Object, Object> delegate() {
289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return ImmutableMap.of();
290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public ImmutableBiMap<Object, Object> inverse() {
292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override boolean isPartialView() {
2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return false;
2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Object readResolve() {
298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return EMPTY_IMMUTABLE_BIMAP; // preserve singleton property
299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Serialized type for all ImmutableBiMap instances. It captures the logical
304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * contents and they are reconstructed using public factory methods. This
305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * ensures that the implementation types remain as implementation details.
306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Since the bimap is immutable, ImmutableBiMap doesn't require special logic
308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * for keeping the bimap and its inverse in sync during serialization, the way
309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * AbstractBiMap does.
310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class SerializedForm extends ImmutableMap.SerializedForm {
312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    SerializedForm(ImmutableBiMap<?, ?> bimap) {
313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super(bimap);
314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override Object readResolve() {
316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Builder<Object, Object> builder = new Builder<Object, Object>();
317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return createMap(builder);
318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override Object writeReplace() {
323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new SerializedForm(this);
324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
326