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