001 /*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package com.google.common.collect;
018
019 import com.google.common.annotations.GwtCompatible;
020
021 import java.util.Map;
022
023 import javax.annotation.Nullable;
024
025 /**
026 * An immutable {@link BiMap} with reliable user-specified iteration order. Does
027 * not permit null keys or values. An {@code ImmutableBiMap} and its inverse
028 * have the same iteration ordering.
029 *
030 * <p>An instance of {@code ImmutableBiMap} contains its own data and will
031 * <i>never</i> change. {@code ImmutableBiMap} is convenient for
032 * {@code public static final} maps ("constant maps") and also lets you easily
033 * make a "defensive copy" of a bimap provided to your class by a caller.
034 *
035 * <p><b>Note:</b> Although this class is not final, it cannot be subclassed as
036 * it has no public or protected constructors. Thus, instances of this class are
037 * guaranteed to be immutable.
038 *
039 * @author Jared Levy
040 * @since 2.0 (imported from Google Collections Library)
041 */
042 @GwtCompatible(serializable = true, emulated = true)
043 public abstract class ImmutableBiMap<K, V> extends ImmutableMap<K, V>
044 implements BiMap<K, V> {
045
046 /**
047 * Returns the empty bimap.
048 */
049 // Casting to any type is safe because the set will never hold any elements.
050 @SuppressWarnings("unchecked")
051 public static <K, V> ImmutableBiMap<K, V> of() {
052 return (ImmutableBiMap<K, V>) EmptyImmutableBiMap.INSTANCE;
053 }
054
055 /**
056 * Returns an immutable bimap containing a single entry.
057 */
058 public static <K, V> ImmutableBiMap<K, V> of(K k1, V v1) {
059 return new RegularImmutableBiMap<K, V>(ImmutableMap.of(k1, v1));
060 }
061
062 /**
063 * Returns an immutable map containing the given entries, in order.
064 *
065 * @throws IllegalArgumentException if duplicate keys or values are added
066 */
067 public static <K, V> ImmutableBiMap<K, V> of(K k1, V v1, K k2, V v2) {
068 return new RegularImmutableBiMap<K, V>(ImmutableMap.of(k1, v1, k2, v2));
069 }
070
071 /**
072 * Returns an immutable map containing the given entries, in order.
073 *
074 * @throws IllegalArgumentException if duplicate keys or values are added
075 */
076 public static <K, V> ImmutableBiMap<K, V> of(
077 K k1, V v1, K k2, V v2, K k3, V v3) {
078 return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
079 k1, v1, k2, v2, k3, v3));
080 }
081
082 /**
083 * Returns an immutable map containing the given entries, in order.
084 *
085 * @throws IllegalArgumentException if duplicate keys or values are added
086 */
087 public static <K, V> ImmutableBiMap<K, V> of(
088 K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
089 return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
090 k1, v1, k2, v2, k3, v3, k4, v4));
091 }
092
093 /**
094 * Returns an immutable map containing the given entries, in order.
095 *
096 * @throws IllegalArgumentException if duplicate keys or values are added
097 */
098 public static <K, V> ImmutableBiMap<K, V> of(
099 K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
100 return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
101 k1, v1, k2, v2, k3, v3, k4, v4, k5, v5));
102 }
103
104 // looking for of() with > 5 entries? Use the builder instead.
105
106 /**
107 * Returns a new builder. The generated builder is equivalent to the builder
108 * created by the {@link Builder} constructor.
109 */
110 public static <K, V> Builder<K, V> builder() {
111 return new Builder<K, V>();
112 }
113
114 /**
115 * A builder for creating immutable bimap instances, especially {@code public
116 * static final} bimaps ("constant bimaps"). Example: <pre> {@code
117 *
118 * static final ImmutableBiMap<String, Integer> WORD_TO_INT =
119 * new ImmutableBiMap.Builder<String, Integer>()
120 * .put("one", 1)
121 * .put("two", 2)
122 * .put("three", 3)
123 * .build();}</pre>
124 *
125 * For <i>small</i> immutable bimaps, the {@code ImmutableBiMap.of()} methods
126 * are even more convenient.
127 *
128 * <p>Builder instances can be reused - it is safe to call {@link #build}
129 * multiple times to build multiple bimaps in series. Each bimap is a superset
130 * of the bimaps created before it.
131 *
132 * @since 2.0 (imported from Google Collections Library)
133 */
134 public static final class Builder<K, V> extends ImmutableMap.Builder<K, V> {
135
136 /**
137 * Creates a new builder. The returned builder is equivalent to the builder
138 * generated by {@link ImmutableBiMap#builder}.
139 */
140 public Builder() {}
141
142 /**
143 * Associates {@code key} with {@code value} in the built bimap. Duplicate
144 * keys or values are not allowed, and will cause {@link #build} to fail.
145 */
146
147 @Override
148 public Builder<K, V> put(K key, V value) {
149 super.put(key, value);
150 return this;
151 }
152
153 /**
154 * Associates all of the given map's keys and values in the built bimap.
155 * Duplicate keys or values are not allowed, and will cause {@link #build}
156 * to fail.
157 *
158 * @throws NullPointerException if any key or value in {@code map} is null
159 */
160
161 @Override
162 public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
163 super.putAll(map);
164 return this;
165 }
166
167 /**
168 * Returns a newly-created immutable bimap.
169 *
170 * @throws IllegalArgumentException if duplicate keys or values were added
171 */
172
173 @Override
174 public ImmutableBiMap<K, V> build() {
175 ImmutableMap<K, V> map = super.build();
176 if (map.isEmpty()) {
177 return of();
178 }
179 return new RegularImmutableBiMap<K, V>(map);
180 }
181 }
182
183 /**
184 * Returns an immutable bimap containing the same entries as {@code map}. If
185 * {@code map} somehow contains entries with duplicate keys (for example, if
186 * it is a {@code SortedMap} whose comparator is not <i>consistent with
187 * equals</i>), the results of this method are undefined.
188 *
189 * <p>Despite the method name, this method attempts to avoid actually copying
190 * the data when it is safe to do so. The exact circumstances under which a
191 * copy will or will not be performed are undocumented and subject to change.
192 *
193 * @throws IllegalArgumentException if two keys have the same value
194 * @throws NullPointerException if any key or value in {@code map} is null
195 */
196 public static <K, V> ImmutableBiMap<K, V> copyOf(
197 Map<? extends K, ? extends V> map) {
198 if (map instanceof ImmutableBiMap) {
199 @SuppressWarnings("unchecked") // safe since map is not writable
200 ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map;
201 // TODO(user): if we need to make a copy of a BiMap because the
202 // forward map is a view, don't make a copy of the non-view delegate map
203 if (!bimap.isPartialView()) {
204 return bimap;
205 }
206 }
207
208 if (map.isEmpty()) {
209 return of();
210 }
211
212 ImmutableMap<K, V> immutableMap = ImmutableMap.copyOf(map);
213 return new RegularImmutableBiMap<K, V>(immutableMap);
214 }
215
216 ImmutableBiMap() {}
217
218 abstract ImmutableMap<K, V> delegate();
219
220 /**
221 * {@inheritDoc}
222 *
223 * <p>The inverse of an {@code ImmutableBiMap} is another
224 * {@code ImmutableBiMap}.
225 */
226 public abstract ImmutableBiMap<V, K> inverse();
227
228
229 @Override
230 public boolean containsKey(@Nullable Object key) {
231 return delegate().containsKey(key);
232 }
233
234
235 @Override
236 public boolean containsValue(@Nullable Object value) {
237 return inverse().containsKey(value);
238 }
239
240
241 @Override
242 ImmutableSet<Entry<K, V>> createEntrySet() {
243 return delegate().entrySet();
244 }
245
246
247 @Override
248 public V get(@Nullable Object key) {
249 return delegate().get(key);
250 }
251
252
253 @Override
254 public ImmutableSet<K> keySet() {
255 return delegate().keySet();
256 }
257
258 /**
259 * Returns an immutable set of the values in this map. The values are in the
260 * same order as the parameters used to build this map.
261 */
262
263 @Override
264 public ImmutableSet<V> values() {
265 return inverse().keySet();
266 }
267
268 /**
269 * Guaranteed to throw an exception and leave the bimap unmodified.
270 *
271 * @throws UnsupportedOperationException always
272 */
273 public V forcePut(K key, V value) {
274 throw new UnsupportedOperationException();
275 }
276
277
278 @Override
279 public boolean isEmpty() {
280 return delegate().isEmpty();
281 }
282
283 public int size() {
284 return delegate().size();
285 }
286
287
288 @Override
289 public boolean equals(@Nullable Object object) {
290 return object == this || delegate().equals(object);
291 }
292
293
294 @Override
295 public int hashCode() {
296 return delegate().hashCode();
297 }
298
299
300 @Override
301 public String toString() {
302 return delegate().toString();
303 }
304
305 /**
306 * Serialized type for all ImmutableBiMap instances. It captures the logical
307 * contents and they are reconstructed using public factory methods. This
308 * ensures that the implementation types remain as implementation details.
309 *
310 * Since the bimap is immutable, ImmutableBiMap doesn't require special logic
311 * for keeping the bimap and its inverse in sync during serialization, the way
312 * AbstractBiMap does.
313 */
314 private static class SerializedForm extends ImmutableMap.SerializedForm {
315 SerializedForm(ImmutableBiMap<?, ?> bimap) {
316 super(bimap);
317 }
318
319 @Override
320 Object readResolve() {
321 Builder<Object, Object> builder = new Builder<Object, Object>();
322 return createMap(builder);
323 }
324 private static final long serialVersionUID = 0;
325 }
326
327
328 @Override
329 Object writeReplace() {
330 return new SerializedForm(this);
331 }
332 }