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
017package com.google.common.collect;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020import static com.google.common.base.Preconditions.checkState;
021import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
022import static com.google.common.collect.CollectPreconditions.checkNonnegative;
023
024import com.google.common.annotations.Beta;
025import com.google.common.annotations.GwtCompatible;
026import com.google.errorprone.annotations.CanIgnoreReturnValue;
027import com.google.errorprone.annotations.DoNotMock;
028import com.google.errorprone.annotations.concurrent.LazyInit;
029import com.google.j2objc.annotations.RetainedWith;
030import com.google.j2objc.annotations.WeakOuter;
031import java.io.Serializable;
032import java.util.AbstractMap;
033import java.util.Arrays;
034import java.util.Collection;
035import java.util.Collections;
036import java.util.Comparator;
037import java.util.Iterator;
038import java.util.Map;
039import java.util.Map.Entry;
040import java.util.SortedMap;
041import org.checkerframework.checker.nullness.compatqual.NullableDecl;
042
043/**
044 * A {@link Map} whose contents will never change, with many other important properties detailed at
045 * {@link ImmutableCollection}.
046 *
047 * <p>See the Guava User Guide article on <a href=
048 * "https://github.com/google/guava/wiki/ImmutableCollectionsExplained"> immutable collections</a>.
049 *
050 * @author Jesse Wilson
051 * @author Kevin Bourrillion
052 * @since 2.0
053 */
054@DoNotMock("Use ImmutableMap.of or another implementation")
055@GwtCompatible(serializable = true, emulated = true)
056@SuppressWarnings("serial") // we're overriding default serialization
057public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
058
059  /**
060   * Returns the empty map. This map behaves and performs comparably to {@link
061   * Collections#emptyMap}, and is preferable mainly for consistency and maintainability of your
062   * code.
063   */
064  @SuppressWarnings("unchecked")
065  public static <K, V> ImmutableMap<K, V> of() {
066    return (ImmutableMap<K, V>) RegularImmutableMap.EMPTY;
067  }
068
069  /**
070   * Returns an immutable map containing a single entry. This map behaves and performs comparably to
071   * {@link Collections#singletonMap} but will not accept a null key or value. It is preferable
072   * mainly for consistency and maintainability of your code.
073   */
074  public static <K, V> ImmutableMap<K, V> of(K k1, V v1) {
075    checkEntryNotNull(k1, v1);
076    return RegularImmutableMap.create(1, new Object[] {k1, v1});
077  }
078
079  /**
080   * Returns an immutable map containing the given entries, in order.
081   *
082   * @throws IllegalArgumentException if duplicate keys are provided
083   */
084  public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2) {
085    checkEntryNotNull(k1, v1);
086    checkEntryNotNull(k2, v2);
087    return RegularImmutableMap.create(2, new Object[] {k1, v1, k2, v2});
088  }
089
090  /**
091   * Returns an immutable map containing the given entries, in order.
092   *
093   * @throws IllegalArgumentException if duplicate keys are provided
094   */
095  public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
096    checkEntryNotNull(k1, v1);
097    checkEntryNotNull(k2, v2);
098    checkEntryNotNull(k3, v3);
099    return RegularImmutableMap.create(3, new Object[] {k1, v1, k2, v2, k3, v3});
100  }
101
102  /**
103   * Returns an immutable map containing the given entries, in order.
104   *
105   * @throws IllegalArgumentException if duplicate keys are provided
106   */
107  public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
108    checkEntryNotNull(k1, v1);
109    checkEntryNotNull(k2, v2);
110    checkEntryNotNull(k3, v3);
111    checkEntryNotNull(k4, v4);
112    return RegularImmutableMap.create(4, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4});
113  }
114
115  /**
116   * Returns an immutable map containing the given entries, in order.
117   *
118   * @throws IllegalArgumentException if duplicate keys are provided
119   */
120  public static <K, V> ImmutableMap<K, V> of(
121      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
122    checkEntryNotNull(k1, v1);
123    checkEntryNotNull(k2, v2);
124    checkEntryNotNull(k3, v3);
125    checkEntryNotNull(k4, v4);
126    checkEntryNotNull(k5, v5);
127    return RegularImmutableMap.create(5, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4, k5, v5});
128  }
129
130  // looking for of() with > 5 entries? Use the builder instead.
131
132  /**
133   * Verifies that {@code key} and {@code value} are non-null, and returns a new immutable entry
134   * with those values.
135   *
136   * <p>A call to {@link Entry#setValue} on the returned entry will always throw {@link
137   * UnsupportedOperationException}.
138   */
139  static <K, V> Entry<K, V> entryOf(K key, V value) {
140    checkEntryNotNull(key, value);
141    return new AbstractMap.SimpleImmutableEntry<>(key, value);
142  }
143
144  /**
145   * Returns a new builder. The generated builder is equivalent to the builder created by the {@link
146   * Builder} constructor.
147   */
148  public static <K, V> Builder<K, V> builder() {
149    return new Builder<>();
150  }
151
152  /**
153   * Returns a new builder, expecting the specified number of entries to be added.
154   *
155   * <p>If {@code expectedSize} is exactly the number of entries added to the builder before {@link
156   * Builder#build} is called, the builder is likely to perform better than an unsized {@link
157   * #builder()} would have.
158   *
159   * <p>It is not specified if any performance benefits apply if {@code expectedSize} is close to,
160   * but not exactly, the number of entries added to the builder.
161   *
162   * @since 23.1
163   */
164  @Beta
165  public static <K, V> Builder<K, V> builderWithExpectedSize(int expectedSize) {
166    checkNonnegative(expectedSize, "expectedSize");
167    return new Builder<>(expectedSize);
168  }
169
170  static void checkNoConflict(
171      boolean safe, String conflictDescription, Entry<?, ?> entry1, Entry<?, ?> entry2) {
172    if (!safe) {
173      throw conflictException(conflictDescription, entry1, entry2);
174    }
175  }
176
177  static IllegalArgumentException conflictException(
178      String conflictDescription, Object entry1, Object entry2) {
179    return new IllegalArgumentException(
180        "Multiple entries with same " + conflictDescription + ": " + entry1 + " and " + entry2);
181  }
182
183  /**
184   * A builder for creating immutable map instances, especially {@code public static final} maps
185   * ("constant maps"). Example:
186   *
187   * <pre>{@code
188   * static final ImmutableMap<String, Integer> WORD_TO_INT =
189   *     new ImmutableMap.Builder<String, Integer>()
190   *         .put("one", 1)
191   *         .put("two", 2)
192   *         .put("three", 3)
193   *         .build();
194   * }</pre>
195   *
196   * <p>For <i>small</i> immutable maps, the {@code ImmutableMap.of()} methods are even more
197   * convenient.
198   *
199   * <p>By default, a {@code Builder} will generate maps that iterate over entries in the order they
200   * were inserted into the builder, equivalently to {@code LinkedHashMap}. For example, in the
201   * above example, {@code WORD_TO_INT.entrySet()} is guaranteed to iterate over the entries in the
202   * order {@code "one"=1, "two"=2, "three"=3}, and {@code keySet()} and {@code values()} respect
203   * the same order. If you want a different order, consider using {@link ImmutableSortedMap} to
204   * sort by keys, or call {@link #orderEntriesByValue(Comparator)}, which changes this builder to
205   * sort entries by value.
206   *
207   * <p>Builder instances can be reused - it is safe to call {@link #build} multiple times to build
208   * multiple maps in series. Each map is a superset of the maps created before it.
209   *
210   * @since 2.0
211   */
212  @DoNotMock
213  public static class Builder<K, V> {
214    @NullableDecl Comparator<? super V> valueComparator;
215    Object[] alternatingKeysAndValues;
216    int size;
217    boolean entriesUsed;
218
219    /**
220     * Creates a new builder. The returned builder is equivalent to the builder generated by {@link
221     * ImmutableMap#builder}.
222     */
223    public Builder() {
224      this(ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY);
225    }
226
227    @SuppressWarnings("unchecked")
228    Builder(int initialCapacity) {
229      this.alternatingKeysAndValues = new Object[2 * initialCapacity];
230      this.size = 0;
231      this.entriesUsed = false;
232    }
233
234    private void ensureCapacity(int minCapacity) {
235      if (minCapacity * 2 > alternatingKeysAndValues.length) {
236        alternatingKeysAndValues =
237            Arrays.copyOf(
238                alternatingKeysAndValues,
239                ImmutableCollection.Builder.expandedCapacity(
240                    alternatingKeysAndValues.length, minCapacity * 2));
241        entriesUsed = false;
242      }
243    }
244
245    /**
246     * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed,
247     * and will cause {@link #build} to fail.
248     */
249    @CanIgnoreReturnValue
250    public Builder<K, V> put(K key, V value) {
251      ensureCapacity(size + 1);
252      checkEntryNotNull(key, value);
253      alternatingKeysAndValues[2 * size] = key;
254      alternatingKeysAndValues[2 * size + 1] = value;
255      size++;
256      return this;
257    }
258
259    /**
260     * Adds the given {@code entry} to the map, making it immutable if necessary. Duplicate keys are
261     * not allowed, and will cause {@link #build} to fail.
262     *
263     * @since 11.0
264     */
265    @CanIgnoreReturnValue
266    public Builder<K, V> put(Entry<? extends K, ? extends V> entry) {
267      return put(entry.getKey(), entry.getValue());
268    }
269
270    /**
271     * Associates all of the given map's keys and values in the built map. Duplicate keys are not
272     * allowed, and will cause {@link #build} to fail.
273     *
274     * @throws NullPointerException if any key or value in {@code map} is null
275     */
276    @CanIgnoreReturnValue
277    public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
278      return putAll(map.entrySet());
279    }
280
281    /**
282     * Adds all of the given entries to the built map. Duplicate keys are not allowed, and will
283     * cause {@link #build} to fail.
284     *
285     * @throws NullPointerException if any key, value, or entry is null
286     * @since 19.0
287     */
288    @CanIgnoreReturnValue
289    @Beta
290    public Builder<K, V> putAll(Iterable<? extends Entry<? extends K, ? extends V>> entries) {
291      if (entries instanceof Collection) {
292        ensureCapacity(size + ((Collection<?>) entries).size());
293      }
294      for (Entry<? extends K, ? extends V> entry : entries) {
295        put(entry);
296      }
297      return this;
298    }
299
300    /**
301     * Configures this {@code Builder} to order entries by value according to the specified
302     * comparator.
303     *
304     * <p>The sort order is stable, that is, if two entries have values that compare as equivalent,
305     * the entry that was inserted first will be first in the built map's iteration order.
306     *
307     * @throws IllegalStateException if this method was already called
308     * @since 19.0
309     */
310    @CanIgnoreReturnValue
311    @Beta
312    public Builder<K, V> orderEntriesByValue(Comparator<? super V> valueComparator) {
313      checkState(this.valueComparator == null, "valueComparator was already set");
314      this.valueComparator = checkNotNull(valueComparator, "valueComparator");
315      return this;
316    }
317
318    /*
319     * TODO(kevinb): Should build() and the ImmutableBiMap & ImmutableSortedMap
320     * versions throw an IllegalStateException instead?
321     */
322
323    /**
324     * Returns a newly-created immutable map. The iteration order of the returned map is the order
325     * in which entries were inserted into the builder, unless {@link #orderEntriesByValue} was
326     * called, in which case entries are sorted by value.
327     *
328     * @throws IllegalArgumentException if duplicate keys were added
329     */
330    @SuppressWarnings("unchecked")
331    public ImmutableMap<K, V> build() {
332      /*
333       * If entries is full, then this implementation may end up using the entries array
334       * directly and writing over the entry objects with non-terminal entries, but this is
335       * safe; if this Builder is used further, it will grow the entries array (so it can't
336       * affect the original array), and future build() calls will always copy any entry
337       * objects that cannot be safely reused.
338       */
339      sortEntries();
340      entriesUsed = true;
341      return RegularImmutableMap.create(size, alternatingKeysAndValues);
342    }
343
344    void sortEntries() {
345      if (valueComparator != null) {
346        if (entriesUsed) {
347          alternatingKeysAndValues = Arrays.copyOf(alternatingKeysAndValues, 2 * size);
348        }
349        Entry<K, V>[] entries = new Entry[size];
350        for (int i = 0; i < size; i++) {
351          entries[i] =
352              new AbstractMap.SimpleImmutableEntry<K, V>(
353                  (K) alternatingKeysAndValues[2 * i], (V) alternatingKeysAndValues[2 * i + 1]);
354        }
355        Arrays.sort(
356            entries, 0, size, Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction()));
357        for (int i = 0; i < size; i++) {
358          alternatingKeysAndValues[2 * i] = entries[i].getKey();
359          alternatingKeysAndValues[2 * i + 1] = entries[i].getValue();
360        }
361      }
362    }
363  }
364
365  /**
366   * Returns an immutable map containing the same entries as {@code map}. The returned map iterates
367   * over entries in the same order as the {@code entrySet} of the original map. If {@code map}
368   * somehow contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose
369   * comparator is not <i>consistent with equals</i>), the results of this method are undefined.
370   *
371   * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
372   * safe to do so. The exact circumstances under which a copy will or will not be performed are
373   * undocumented and subject to change.
374   *
375   * @throws NullPointerException if any key or value in {@code map} is null
376   */
377  public static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V> map) {
378    if ((map instanceof ImmutableMap) && !(map instanceof SortedMap)) {
379      @SuppressWarnings("unchecked") // safe since map is not writable
380      ImmutableMap<K, V> kvMap = (ImmutableMap<K, V>) map;
381      if (!kvMap.isPartialView()) {
382        return kvMap;
383      }
384    }
385    return copyOf(map.entrySet());
386  }
387
388  /**
389   * Returns an immutable map containing the specified entries. The returned map iterates over
390   * entries in the same order as the original iterable.
391   *
392   * @throws NullPointerException if any key, value, or entry is null
393   * @throws IllegalArgumentException if two entries have the same key
394   * @since 19.0
395   */
396  @Beta
397  public static <K, V> ImmutableMap<K, V> copyOf(
398      Iterable<? extends Entry<? extends K, ? extends V>> entries) {
399    int initialCapacity =
400        (entries instanceof Collection)
401            ? ((Collection<?>) entries).size()
402            : ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY;
403    ImmutableMap.Builder<K, V> builder = new ImmutableMap.Builder<K, V>(initialCapacity);
404    builder.putAll(entries);
405    return builder.build();
406  }
407
408  static final Entry<?, ?>[] EMPTY_ENTRY_ARRAY = new Entry<?, ?>[0];
409
410  abstract static class IteratorBasedImmutableMap<K, V> extends ImmutableMap<K, V> {
411    abstract UnmodifiableIterator<Entry<K, V>> entryIterator();
412
413    @Override
414    ImmutableSet<K> createKeySet() {
415      return new ImmutableMapKeySet<>(this);
416    }
417
418    @Override
419    ImmutableSet<Entry<K, V>> createEntrySet() {
420      class EntrySetImpl extends ImmutableMapEntrySet<K, V> {
421        @Override
422        ImmutableMap<K, V> map() {
423          return IteratorBasedImmutableMap.this;
424        }
425
426        @Override
427        public UnmodifiableIterator<Entry<K, V>> iterator() {
428          return entryIterator();
429        }
430      }
431      return new EntrySetImpl();
432    }
433
434    @Override
435    ImmutableCollection<V> createValues() {
436      return new ImmutableMapValues<>(this);
437    }
438  }
439
440  ImmutableMap() {}
441
442  /**
443   * Guaranteed to throw an exception and leave the map unmodified.
444   *
445   * @throws UnsupportedOperationException always
446   * @deprecated Unsupported operation.
447   */
448  @CanIgnoreReturnValue
449  @Deprecated
450  @Override
451  public final V put(K k, V v) {
452    throw new UnsupportedOperationException();
453  }
454
455  /**
456   * Guaranteed to throw an exception and leave the map unmodified.
457   *
458   * @throws UnsupportedOperationException always
459   * @deprecated Unsupported operation.
460   */
461  @CanIgnoreReturnValue
462  @Deprecated
463  @Override
464  public final V remove(Object o) {
465    throw new UnsupportedOperationException();
466  }
467
468  /**
469   * Guaranteed to throw an exception and leave the map unmodified.
470   *
471   * @throws UnsupportedOperationException always
472   * @deprecated Unsupported operation.
473   */
474  @Deprecated
475  @Override
476  public final void putAll(Map<? extends K, ? extends V> map) {
477    throw new UnsupportedOperationException();
478  }
479
480  /**
481   * Guaranteed to throw an exception and leave the map unmodified.
482   *
483   * @throws UnsupportedOperationException always
484   * @deprecated Unsupported operation.
485   */
486  @Deprecated
487  @Override
488  public final void clear() {
489    throw new UnsupportedOperationException();
490  }
491
492  @Override
493  public boolean isEmpty() {
494    return size() == 0;
495  }
496
497  @Override
498  public boolean containsKey(@NullableDecl Object key) {
499    return get(key) != null;
500  }
501
502  @Override
503  public boolean containsValue(@NullableDecl Object value) {
504    return values().contains(value);
505  }
506
507  // Overriding to mark it Nullable
508  @Override
509  public abstract V get(@NullableDecl Object key);
510
511  /**
512   * {@inheritDoc}
513   *
514   * <p>See <a
515   * href="https://developer.android.com/reference/java/util/Map.html#getOrDefault%28java.lang.Object,%20V%29">{@code
516   * Map.getOrDefault}</a>.
517   *
518   * @since 23.5 (but since 21.0 in the JRE <a
519   *     href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>).
520   *     Note that API Level 24 users can call this method with any version of Guava.
521   */
522  // @Override under Java 8 / API Level 24
523  public final V getOrDefault(@NullableDecl Object key, @NullableDecl V defaultValue) {
524    V result = get(key);
525    return (result != null) ? result : defaultValue;
526  }
527
528  @LazyInit @RetainedWith private transient ImmutableSet<Entry<K, V>> entrySet;
529
530  /**
531   * Returns an immutable set of the mappings in this map. The iteration order is specified by the
532   * method used to create this map. Typically, this is insertion order.
533   */
534  @Override
535  public ImmutableSet<Entry<K, V>> entrySet() {
536    ImmutableSet<Entry<K, V>> result = entrySet;
537    return (result == null) ? entrySet = createEntrySet() : result;
538  }
539
540  abstract ImmutableSet<Entry<K, V>> createEntrySet();
541
542  @LazyInit @RetainedWith private transient ImmutableSet<K> keySet;
543
544  /**
545   * Returns an immutable set of the keys in this map, in the same order that they appear in {@link
546   * #entrySet}.
547   */
548  @Override
549  public ImmutableSet<K> keySet() {
550    ImmutableSet<K> result = keySet;
551    return (result == null) ? keySet = createKeySet() : result;
552  }
553
554  /*
555   * This could have a good default implementation of return new ImmutableKeySet<K, V>(this),
556   * but ProGuard can't figure out how to eliminate that default when RegularImmutableMap
557   * overrides it.
558   */
559  abstract ImmutableSet<K> createKeySet();
560
561  UnmodifiableIterator<K> keyIterator() {
562    final UnmodifiableIterator<Entry<K, V>> entryIterator = entrySet().iterator();
563    return new UnmodifiableIterator<K>() {
564      @Override
565      public boolean hasNext() {
566        return entryIterator.hasNext();
567      }
568
569      @Override
570      public K next() {
571        return entryIterator.next().getKey();
572      }
573    };
574  }
575
576  @LazyInit @RetainedWith private transient ImmutableCollection<V> values;
577
578  /**
579   * Returns an immutable collection of the values in this map, in the same order that they appear
580   * in {@link #entrySet}.
581   */
582  @Override
583  public ImmutableCollection<V> values() {
584    ImmutableCollection<V> result = values;
585    return (result == null) ? values = createValues() : result;
586  }
587
588  /*
589   * This could have a good default implementation of {@code return new
590   * ImmutableMapValues<K, V>(this)}, but ProGuard can't figure out how to eliminate that default
591   * when RegularImmutableMap overrides it.
592   */
593  abstract ImmutableCollection<V> createValues();
594
595  // cached so that this.multimapView().inverse() only computes inverse once
596  @LazyInit private transient ImmutableSetMultimap<K, V> multimapView;
597
598  /**
599   * Returns a multimap view of the map.
600   *
601   * @since 14.0
602   */
603  public ImmutableSetMultimap<K, V> asMultimap() {
604    if (isEmpty()) {
605      return ImmutableSetMultimap.of();
606    }
607    ImmutableSetMultimap<K, V> result = multimapView;
608    return (result == null)
609        ? (multimapView =
610            new ImmutableSetMultimap<>(new MapViewOfValuesAsSingletonSets(), size(), null))
611        : result;
612  }
613
614  @WeakOuter
615  private final class MapViewOfValuesAsSingletonSets
616      extends IteratorBasedImmutableMap<K, ImmutableSet<V>> {
617
618    @Override
619    public int size() {
620      return ImmutableMap.this.size();
621    }
622
623    @Override
624    ImmutableSet<K> createKeySet() {
625      return ImmutableMap.this.keySet();
626    }
627
628    @Override
629    public boolean containsKey(@NullableDecl Object key) {
630      return ImmutableMap.this.containsKey(key);
631    }
632
633    @Override
634    public ImmutableSet<V> get(@NullableDecl Object key) {
635      V outerValue = ImmutableMap.this.get(key);
636      return (outerValue == null) ? null : ImmutableSet.of(outerValue);
637    }
638
639    @Override
640    boolean isPartialView() {
641      return ImmutableMap.this.isPartialView();
642    }
643
644    @Override
645    public int hashCode() {
646      // ImmutableSet.of(value).hashCode() == value.hashCode(), so the hashes are the same
647      return ImmutableMap.this.hashCode();
648    }
649
650    @Override
651    boolean isHashCodeFast() {
652      return ImmutableMap.this.isHashCodeFast();
653    }
654
655    @Override
656    UnmodifiableIterator<Entry<K, ImmutableSet<V>>> entryIterator() {
657      final Iterator<Entry<K, V>> backingIterator = ImmutableMap.this.entrySet().iterator();
658      return new UnmodifiableIterator<Entry<K, ImmutableSet<V>>>() {
659        @Override
660        public boolean hasNext() {
661          return backingIterator.hasNext();
662        }
663
664        @Override
665        public Entry<K, ImmutableSet<V>> next() {
666          final Entry<K, V> backingEntry = backingIterator.next();
667          return new AbstractMapEntry<K, ImmutableSet<V>>() {
668            @Override
669            public K getKey() {
670              return backingEntry.getKey();
671            }
672
673            @Override
674            public ImmutableSet<V> getValue() {
675              return ImmutableSet.of(backingEntry.getValue());
676            }
677          };
678        }
679      };
680    }
681  }
682
683  @Override
684  public boolean equals(@NullableDecl Object object) {
685    return Maps.equalsImpl(this, object);
686  }
687
688  abstract boolean isPartialView();
689
690  @Override
691  public int hashCode() {
692    return Sets.hashCodeImpl(entrySet());
693  }
694
695  boolean isHashCodeFast() {
696    return false;
697  }
698
699  @Override
700  public String toString() {
701    return Maps.toStringImpl(this);
702  }
703
704  /**
705   * Serialized type for all ImmutableMap instances. It captures the logical contents and they are
706   * reconstructed using public factory methods. This ensures that the implementation types remain
707   * as implementation details.
708   */
709  static class SerializedForm<K, V> implements Serializable {
710    // This object retains references to collections returned by keySet() and value(). This saves
711    // bytes when the both the map and its keySet or value collection are written to the same
712    // instance of ObjectOutputStream.
713
714    // TODO(b/160980469): remove support for the old serialization format after some time
715    private static final boolean USE_LEGACY_SERIALIZATION = true;
716
717    private final Object keys;
718    private final Object values;
719
720    SerializedForm(ImmutableMap<K, V> map) {
721      if (USE_LEGACY_SERIALIZATION) {
722        Object[] keys = new Object[map.size()];
723        Object[] values = new Object[map.size()];
724        int i = 0;
725        for (Entry<?, ?> entry : map.entrySet()) {
726          keys[i] = entry.getKey();
727          values[i] = entry.getValue();
728          i++;
729        }
730        this.keys = keys;
731        this.values = values;
732        return;
733      }
734      this.keys = map.keySet();
735      this.values = map.values();
736    }
737
738    @SuppressWarnings("unchecked")
739    final Object readResolve() {
740      if (!(this.keys instanceof ImmutableSet)) {
741        return legacyReadResolve();
742      }
743
744      ImmutableSet<K> keySet = (ImmutableSet<K>) this.keys;
745      ImmutableCollection<V> values = (ImmutableCollection<V>) this.values;
746
747      Builder<K, V> builder = makeBuilder(keySet.size());
748
749      UnmodifiableIterator<K> keyIter = keySet.iterator();
750      UnmodifiableIterator<V> valueIter = values.iterator();
751
752      while (keyIter.hasNext()) {
753        builder.put(keyIter.next(), valueIter.next());
754      }
755
756      return builder.build();
757    }
758
759    @SuppressWarnings("unchecked")
760    final Object legacyReadResolve() {
761      K[] keys = (K[]) this.keys;
762      V[] values = (V[]) this.values;
763
764      Builder<K, V> builder = makeBuilder(keys.length);
765
766      for (int i = 0; i < keys.length; i++) {
767        builder.put(keys[i], values[i]);
768      }
769      return builder.build();
770    }
771
772    /**
773     * Returns a builder that builds the unserialized type. Subclasses should override this method.
774     */
775    Builder<K, V> makeBuilder(int size) {
776      return new Builder<>(size);
777    }
778
779    private static final long serialVersionUID = 0;
780  }
781
782  /**
783   * Returns a serializable form of this object. Non-public subclasses should not override this
784   * method. Publicly-accessible subclasses must override this method and should return a subclass
785   * of SerializedForm whose readResolve() method returns objects of the subclass type.
786   */
787  Object writeReplace() {
788    return new SerializedForm<>(this);
789  }
790}