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