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;
023import static java.lang.System.arraycopy;
024import static java.util.Arrays.sort;
025import static java.util.Objects.requireNonNull;
026
027import com.google.common.annotations.GwtCompatible;
028import com.google.common.annotations.GwtIncompatible;
029import com.google.common.annotations.J2ktIncompatible;
030import com.google.common.annotations.VisibleForTesting;
031import com.google.errorprone.annotations.CanIgnoreReturnValue;
032import com.google.errorprone.annotations.DoNotCall;
033import com.google.errorprone.annotations.DoNotMock;
034import com.google.errorprone.annotations.concurrent.LazyInit;
035import com.google.j2objc.annotations.RetainedWith;
036import com.google.j2objc.annotations.WeakOuter;
037import java.io.InvalidObjectException;
038import java.io.ObjectInputStream;
039import java.io.Serializable;
040import java.util.Arrays;
041import java.util.BitSet;
042import java.util.Collection;
043import java.util.Collections;
044import java.util.Comparator;
045import java.util.EnumMap;
046import java.util.HashSet;
047import java.util.Iterator;
048import java.util.Map;
049import java.util.Set;
050import java.util.SortedMap;
051import java.util.Spliterator;
052import java.util.Spliterators;
053import java.util.function.BiFunction;
054import java.util.function.BinaryOperator;
055import java.util.function.Function;
056import java.util.stream.Collector;
057import java.util.stream.Collectors;
058import javax.annotation.CheckForNull;
059import org.checkerframework.checker.nullness.qual.Nullable;
060
061/**
062 * A {@link Map} whose contents will never change, with many other important properties detailed at
063 * {@link ImmutableCollection}.
064 *
065 * <p>See the Guava User Guide article on <a href=
066 * "https://github.com/google/guava/wiki/ImmutableCollectionsExplained">immutable collections</a>.
067 *
068 * @author Jesse Wilson
069 * @author Kevin Bourrillion
070 * @since 2.0
071 */
072@DoNotMock("Use ImmutableMap.of or another implementation")
073@GwtCompatible(serializable = true, emulated = true)
074@SuppressWarnings("serial") // we're overriding default serialization
075@ElementTypesAreNonnullByDefault
076public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
077
078  /**
079   * Returns a {@link Collector} that accumulates elements into an {@code ImmutableMap} whose keys
080   * and values are the result of applying the provided mapping functions to the input elements.
081   * Entries appear in the result {@code ImmutableMap} in encounter order.
082   *
083   * <p>If the mapped keys contain duplicates (according to {@link Object#equals(Object)}, an {@code
084   * IllegalArgumentException} is thrown when the collection operation is performed. (This differs
085   * from the {@code Collector} returned by {@link Collectors#toMap(Function, Function)}, which
086   * throws an {@code IllegalStateException}.)
087   *
088   * @since 21.0
089   */
090  public static <T extends @Nullable Object, K, V>
091      Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
092          Function<? super T, ? extends K> keyFunction,
093          Function<? super T, ? extends V> valueFunction) {
094    return CollectCollectors.toImmutableMap(keyFunction, valueFunction);
095  }
096
097  /**
098   * Returns a {@link Collector} that accumulates elements into an {@code ImmutableMap} whose keys
099   * and values are the result of applying the provided mapping functions to the input elements.
100   *
101   * <p>If the mapped keys contain duplicates (according to {@link Object#equals(Object)}), the
102   * values are merged using the specified merging function. If the merging function returns {@code
103   * null}, then the collector removes the value that has been computed for the key thus far (though
104   * future occurrences of the key would reinsert it).
105   *
106   * <p>Entries will appear in the encounter order of the first occurrence of the key.
107   *
108   * @since 21.0
109   */
110  public static <T extends @Nullable Object, K, V>
111      Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
112          Function<? super T, ? extends K> keyFunction,
113          Function<? super T, ? extends V> valueFunction,
114          BinaryOperator<V> mergeFunction) {
115    return CollectCollectors.toImmutableMap(keyFunction, valueFunction, mergeFunction);
116  }
117
118  /**
119   * Returns the empty map. This map behaves and performs comparably to {@link
120   * Collections#emptyMap}, and is preferable mainly for consistency and maintainability of your
121   * code.
122   *
123   * <p><b>Performance note:</b> the instance returned is a singleton.
124   */
125  @SuppressWarnings("unchecked")
126  public static <K, V> ImmutableMap<K, V> of() {
127    return (ImmutableMap<K, V>) RegularImmutableMap.EMPTY;
128  }
129
130  /**
131   * Returns an immutable map containing a single entry. This map behaves and performs comparably to
132   * {@link Collections#singletonMap} but will not accept a null key or value. It is preferable
133   * mainly for consistency and maintainability of your code.
134   */
135  public static <K, V> ImmutableMap<K, V> of(K k1, V v1) {
136    return ImmutableBiMap.of(k1, v1);
137  }
138
139  /**
140   * Returns an immutable map containing the given entries, in order.
141   *
142   * @throws IllegalArgumentException if duplicate keys are provided
143   */
144  public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2) {
145    return RegularImmutableMap.fromEntries(entryOf(k1, v1), entryOf(k2, v2));
146  }
147
148  /**
149   * Returns an immutable map containing the given entries, in order.
150   *
151   * @throws IllegalArgumentException if duplicate keys are provided
152   */
153  public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
154    return RegularImmutableMap.fromEntries(entryOf(k1, v1), entryOf(k2, v2), entryOf(k3, v3));
155  }
156
157  /**
158   * Returns an immutable map containing the given entries, in order.
159   *
160   * @throws IllegalArgumentException if duplicate keys are provided
161   */
162  public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
163    return RegularImmutableMap.fromEntries(
164        entryOf(k1, v1), entryOf(k2, v2), entryOf(k3, v3), entryOf(k4, v4));
165  }
166
167  /**
168   * Returns an immutable map containing the given entries, in order.
169   *
170   * @throws IllegalArgumentException if duplicate keys are provided
171   */
172  public static <K, V> ImmutableMap<K, V> of(
173      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
174    return RegularImmutableMap.fromEntries(
175        entryOf(k1, v1), entryOf(k2, v2), entryOf(k3, v3), entryOf(k4, v4), entryOf(k5, v5));
176  }
177
178  /**
179   * Returns an immutable map containing the given entries, in order.
180   *
181   * @throws IllegalArgumentException if duplicate keys are provided
182   * @since 31.0
183   */
184  public static <K, V> ImmutableMap<K, V> of(
185      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
186    return RegularImmutableMap.fromEntries(
187        entryOf(k1, v1),
188        entryOf(k2, v2),
189        entryOf(k3, v3),
190        entryOf(k4, v4),
191        entryOf(k5, v5),
192        entryOf(k6, v6));
193  }
194
195  /**
196   * Returns an immutable map containing the given entries, in order.
197   *
198   * @throws IllegalArgumentException if duplicate keys are provided
199   * @since 31.0
200   */
201  public static <K, V> ImmutableMap<K, V> of(
202      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) {
203    return RegularImmutableMap.fromEntries(
204        entryOf(k1, v1),
205        entryOf(k2, v2),
206        entryOf(k3, v3),
207        entryOf(k4, v4),
208        entryOf(k5, v5),
209        entryOf(k6, v6),
210        entryOf(k7, v7));
211  }
212
213  /**
214   * Returns an immutable map containing the given entries, in order.
215   *
216   * @throws IllegalArgumentException if duplicate keys are provided
217   * @since 31.0
218   */
219  public static <K, V> ImmutableMap<K, V> of(
220      K k1,
221      V v1,
222      K k2,
223      V v2,
224      K k3,
225      V v3,
226      K k4,
227      V v4,
228      K k5,
229      V v5,
230      K k6,
231      V v6,
232      K k7,
233      V v7,
234      K k8,
235      V v8) {
236    return RegularImmutableMap.fromEntries(
237        entryOf(k1, v1),
238        entryOf(k2, v2),
239        entryOf(k3, v3),
240        entryOf(k4, v4),
241        entryOf(k5, v5),
242        entryOf(k6, v6),
243        entryOf(k7, v7),
244        entryOf(k8, v8));
245  }
246
247  /**
248   * Returns an immutable map containing the given entries, in order.
249   *
250   * @throws IllegalArgumentException if duplicate keys are provided
251   * @since 31.0
252   */
253  public static <K, V> ImmutableMap<K, V> of(
254      K k1,
255      V v1,
256      K k2,
257      V v2,
258      K k3,
259      V v3,
260      K k4,
261      V v4,
262      K k5,
263      V v5,
264      K k6,
265      V v6,
266      K k7,
267      V v7,
268      K k8,
269      V v8,
270      K k9,
271      V v9) {
272    return RegularImmutableMap.fromEntries(
273        entryOf(k1, v1),
274        entryOf(k2, v2),
275        entryOf(k3, v3),
276        entryOf(k4, v4),
277        entryOf(k5, v5),
278        entryOf(k6, v6),
279        entryOf(k7, v7),
280        entryOf(k8, v8),
281        entryOf(k9, v9));
282  }
283
284  /**
285   * Returns an immutable map containing the given entries, in order.
286   *
287   * @throws IllegalArgumentException if duplicate keys are provided
288   * @since 31.0
289   */
290  public static <K, V> ImmutableMap<K, V> of(
291      K k1,
292      V v1,
293      K k2,
294      V v2,
295      K k3,
296      V v3,
297      K k4,
298      V v4,
299      K k5,
300      V v5,
301      K k6,
302      V v6,
303      K k7,
304      V v7,
305      K k8,
306      V v8,
307      K k9,
308      V v9,
309      K k10,
310      V v10) {
311    return RegularImmutableMap.fromEntries(
312        entryOf(k1, v1),
313        entryOf(k2, v2),
314        entryOf(k3, v3),
315        entryOf(k4, v4),
316        entryOf(k5, v5),
317        entryOf(k6, v6),
318        entryOf(k7, v7),
319        entryOf(k8, v8),
320        entryOf(k9, v9),
321        entryOf(k10, v10));
322  }
323
324  // looking for of() with > 10 entries? Use the builder or ofEntries instead.
325
326  /**
327   * Returns an immutable map containing the given entries, in order.
328   *
329   * @throws IllegalArgumentException if duplicate keys are provided
330   * @since 31.0
331   */
332  @SafeVarargs
333  public static <K, V> ImmutableMap<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {
334    @SuppressWarnings("unchecked") // we will only ever read these
335    Entry<K, V>[] entries2 = (Entry<K, V>[]) entries;
336    return RegularImmutableMap.fromEntries(entries2);
337  }
338
339  /**
340   * Verifies that {@code key} and {@code value} are non-null, and returns a new immutable entry
341   * with those values.
342   *
343   * <p>A call to {@link Entry#setValue} on the returned entry will always throw {@link
344   * UnsupportedOperationException}.
345   */
346  static <K, V> Entry<K, V> entryOf(K key, V value) {
347    return new ImmutableMapEntry<>(key, value);
348  }
349
350  /**
351   * Returns a new builder. The generated builder is equivalent to the builder created by the {@link
352   * Builder} constructor.
353   */
354  public static <K, V> Builder<K, V> builder() {
355    return new Builder<>();
356  }
357
358  /**
359   * Returns a new builder, expecting the specified number of entries to be added.
360   *
361   * <p>If {@code expectedSize} is exactly the number of entries added to the builder before {@link
362   * Builder#build} is called, the builder is likely to perform better than an unsized {@link
363   * #builder()} would have.
364   *
365   * <p>It is not specified if any performance benefits apply if {@code expectedSize} is close to,
366   * but not exactly, the number of entries added to the builder.
367   *
368   * @since 23.1
369   */
370  public static <K, V> Builder<K, V> builderWithExpectedSize(int expectedSize) {
371    checkNonnegative(expectedSize, "expectedSize");
372    return new Builder<>(expectedSize);
373  }
374
375  static void checkNoConflict(
376      boolean safe, String conflictDescription, Object entry1, Object entry2) {
377    if (!safe) {
378      throw conflictException(conflictDescription, entry1, entry2);
379    }
380  }
381
382  static IllegalArgumentException conflictException(
383      String conflictDescription, Object entry1, Object entry2) {
384    return new IllegalArgumentException(
385        "Multiple entries with same " + conflictDescription + ": " + entry1 + " and " + entry2);
386  }
387
388  /**
389   * A builder for creating immutable map instances, especially {@code public static final} maps
390   * ("constant maps"). Example:
391   *
392   * <pre>{@code
393   * static final ImmutableMap<String, Integer> WORD_TO_INT =
394   *     new ImmutableMap.Builder<String, Integer>()
395   *         .put("one", 1)
396   *         .put("two", 2)
397   *         .put("three", 3)
398   *         .buildOrThrow();
399   * }</pre>
400   *
401   * <p>For <i>small</i> immutable maps, the {@code ImmutableMap.of()} methods are even more
402   * convenient.
403   *
404   * <p>By default, a {@code Builder} will generate maps that iterate over entries in the order they
405   * were inserted into the builder, equivalently to {@code LinkedHashMap}. For example, in the
406   * above example, {@code WORD_TO_INT.entrySet()} is guaranteed to iterate over the entries in the
407   * order {@code "one"=1, "two"=2, "three"=3}, and {@code keySet()} and {@code values()} respect
408   * the same order. If you want a different order, consider using {@link ImmutableSortedMap} to
409   * sort by keys, or call {@link #orderEntriesByValue(Comparator)}, which changes this builder to
410   * sort entries by value.
411   *
412   * <p>Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to
413   * build multiple maps in series. Each map is a superset of the maps created before it.
414   *
415   * @since 2.0
416   */
417  @DoNotMock
418  public static class Builder<K, V> {
419    @CheckForNull Comparator<? super V> valueComparator;
420    @Nullable Entry<K, V>[] entries;
421    int size;
422    boolean entriesUsed;
423
424    /**
425     * Creates a new builder. The returned builder is equivalent to the builder generated by {@link
426     * ImmutableMap#builder}.
427     */
428    public Builder() {
429      this(ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY);
430    }
431
432    @SuppressWarnings({"unchecked", "rawtypes"})
433    Builder(int initialCapacity) {
434      this.entries = new @Nullable Entry[initialCapacity];
435      this.size = 0;
436      this.entriesUsed = false;
437    }
438
439    private void ensureCapacity(int minCapacity) {
440      if (minCapacity > entries.length) {
441        entries =
442            Arrays.copyOf(
443                entries, ImmutableCollection.Builder.expandedCapacity(entries.length, minCapacity));
444        entriesUsed = false;
445      }
446    }
447
448    /**
449     * Associates {@code key} with {@code value} in the built map. If the same key is put more than
450     * once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep the last
451     * value put for that key.
452     */
453    @CanIgnoreReturnValue
454    public Builder<K, V> put(K key, V value) {
455      ensureCapacity(size + 1);
456      Entry<K, V> entry = entryOf(key, value);
457      // don't inline this: we want to fail atomically if key or value is null
458      entries[size++] = entry;
459      return this;
460    }
461
462    /**
463     * Adds the given {@code entry} to the map, making it immutable if necessary. If the same key is
464     * put more than once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will
465     * keep the last value put for that key.
466     *
467     * @since 11.0
468     */
469    @CanIgnoreReturnValue
470    public Builder<K, V> put(Entry<? extends K, ? extends V> entry) {
471      return put(entry.getKey(), entry.getValue());
472    }
473
474    /**
475     * Associates all of the given map's keys and values in the built map. If the same key is put
476     * more than once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep
477     * the last value put for that key.
478     *
479     * @throws NullPointerException if any key or value in {@code map} is null
480     */
481    @CanIgnoreReturnValue
482    public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
483      return putAll(map.entrySet());
484    }
485
486    /**
487     * Adds all of the given entries to the built map. If the same key is put more than once, {@link
488     * #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep the last value put for
489     * that key.
490     *
491     * @throws NullPointerException if any key, value, or entry is null
492     * @since 19.0
493     */
494    @CanIgnoreReturnValue
495    public Builder<K, V> putAll(Iterable<? extends Entry<? extends K, ? extends V>> entries) {
496      if (entries instanceof Collection) {
497        ensureCapacity(size + ((Collection<?>) entries).size());
498      }
499      for (Entry<? extends K, ? extends V> entry : entries) {
500        put(entry);
501      }
502      return this;
503    }
504
505    /**
506     * Configures this {@code Builder} to order entries by value according to the specified
507     * comparator.
508     *
509     * <p>The sort order is stable, that is, if two entries have values that compare as equivalent,
510     * the entry that was inserted first will be first in the built map's iteration order.
511     *
512     * @throws IllegalStateException if this method was already called
513     * @since 19.0
514     */
515    @CanIgnoreReturnValue
516    public Builder<K, V> orderEntriesByValue(Comparator<? super V> valueComparator) {
517      checkState(this.valueComparator == null, "valueComparator was already set");
518      this.valueComparator = checkNotNull(valueComparator, "valueComparator");
519      return this;
520    }
521
522    @CanIgnoreReturnValue
523    Builder<K, V> combine(Builder<K, V> other) {
524      checkNotNull(other);
525      ensureCapacity(this.size + other.size);
526      arraycopy(other.entries, 0, this.entries, this.size, other.size);
527      this.size += other.size;
528      return this;
529    }
530
531    private ImmutableMap<K, V> build(boolean throwIfDuplicateKeys) {
532      /*
533       * If entries is full, or if hash flooding is detected, then this implementation may end up
534       * using the entries array directly and writing over the entry objects with non-terminal
535       * entries, but this is safe; if this Builder is used further, it will grow the entries array
536       * (so it can't affect the original array), and future build() calls will always copy any
537       * entry objects that cannot be safely reused.
538       */
539      switch (size) {
540        case 0:
541          return of();
542        case 1:
543          // requireNonNull is safe because the first `size` elements have been filled in.
544          Entry<K, V> onlyEntry = requireNonNull(entries[0]);
545          return of(onlyEntry.getKey(), onlyEntry.getValue());
546        default:
547          break;
548      }
549      // localEntries is an alias for the entries field, except if we end up removing duplicates in
550      // a copy of the entries array. Likewise, localSize is the same as size except in that case.
551      // It's possible to keep using this Builder after calling buildKeepingLast(), so we need to
552      // ensure that its state is not corrupted by removing duplicates that should cause a later
553      // buildOrThrow() to fail, or by changing the size.
554      @Nullable Entry<K, V>[] localEntries;
555      int localSize = size;
556      if (valueComparator == null) {
557        localEntries = entries;
558      } else {
559        if (entriesUsed) {
560          entries = Arrays.copyOf(entries, size);
561        }
562        @SuppressWarnings("nullness") // entries 0..localSize-1 are non-null
563        Entry<K, V>[] nonNullEntries = (Entry<K, V>[]) entries;
564        if (!throwIfDuplicateKeys) {
565          // We want to retain only the last-put value for any given key, before sorting.
566          // This could be improved, but orderEntriesByValue is rather rarely used anyway.
567          Entry<K, V>[] lastEntryForEachKey = lastEntryForEachKey(nonNullEntries, size);
568          if (lastEntryForEachKey != null) {
569            nonNullEntries = lastEntryForEachKey;
570            localSize = lastEntryForEachKey.length;
571          }
572        }
573        sort(
574            nonNullEntries,
575            0,
576            localSize,
577            Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction()));
578        localEntries = (@Nullable Entry<K, V>[]) nonNullEntries;
579      }
580      entriesUsed = true;
581      return RegularImmutableMap.fromEntryArray(localSize, localEntries, throwIfDuplicateKeys);
582    }
583
584    /**
585     * Returns a newly-created immutable map. The iteration order of the returned map is the order
586     * in which entries were inserted into the builder, unless {@link #orderEntriesByValue} was
587     * called, in which case entries are sorted by value.
588     *
589     * <p>Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method
590     * will throw an exception if there are duplicate keys. The {@code build()} method will soon be
591     * deprecated.
592     *
593     * @throws IllegalArgumentException if duplicate keys were added
594     */
595    public ImmutableMap<K, V> build() {
596      return buildOrThrow();
597    }
598
599    /**
600     * Returns a newly-created immutable map, or throws an exception if any key was added more than
601     * once. The iteration order of the returned map is the order in which entries were inserted
602     * into the builder, unless {@link #orderEntriesByValue} was called, in which case entries are
603     * sorted by value.
604     *
605     * @throws IllegalArgumentException if duplicate keys were added
606     * @since 31.0
607     */
608    public ImmutableMap<K, V> buildOrThrow() {
609      return build(true);
610    }
611
612    /**
613     * Returns a newly-created immutable map, using the last value for any key that was added more
614     * than once. The iteration order of the returned map is the order in which entries were
615     * inserted into the builder, unless {@link #orderEntriesByValue} was called, in which case
616     * entries are sorted by value. If a key was added more than once, it appears in iteration order
617     * based on the first time it was added, again unless {@link #orderEntriesByValue} was called.
618     *
619     * <p>In the current implementation, all values associated with a given key are stored in the
620     * {@code Builder} object, even though only one of them will be used in the built map. If there
621     * can be many repeated keys, it may be more space-efficient to use a {@link
622     * java.util.LinkedHashMap LinkedHashMap} and {@link ImmutableMap#copyOf(Map)} rather than
623     * {@code ImmutableMap.Builder}.
624     *
625     * @since 31.1
626     */
627    public ImmutableMap<K, V> buildKeepingLast() {
628      return build(false);
629    }
630
631    @VisibleForTesting // only for testing JDK backed implementation
632    ImmutableMap<K, V> buildJdkBacked() {
633      checkState(
634          valueComparator == null, "buildJdkBacked is only for testing; can't use valueComparator");
635      switch (size) {
636        case 0:
637          return of();
638        case 1:
639          // requireNonNull is safe because the first `size` elements have been filled in.
640          Entry<K, V> onlyEntry = requireNonNull(entries[0]);
641          return of(onlyEntry.getKey(), onlyEntry.getValue());
642        default:
643          entriesUsed = true;
644          return JdkBackedImmutableMap.create(size, entries, /* throwIfDuplicateKeys= */ true);
645      }
646    }
647
648    /**
649     * Scans the first {@code size} elements of {@code entries} looking for duplicate keys. If
650     * duplicates are found, a new correctly-sized array is returned with the same elements (up to
651     * {@code size}), except containing only the last occurrence of each duplicate key. Otherwise
652     * {@code null} is returned.
653     */
654    @CheckForNull
655    private static <K, V> Entry<K, V>[] lastEntryForEachKey(Entry<K, V>[] entries, int size) {
656      Set<K> seen = new HashSet<>();
657      BitSet dups = new BitSet(); // slots that are overridden by a later duplicate key
658      for (int i = size - 1; i >= 0; i--) {
659        if (!seen.add(entries[i].getKey())) {
660          dups.set(i);
661        }
662      }
663      if (dups.isEmpty()) {
664        return null;
665      }
666      @SuppressWarnings({"rawtypes", "unchecked"})
667      Entry<K, V>[] newEntries = new Entry[size - dups.cardinality()];
668      for (int inI = 0, outI = 0; inI < size; inI++) {
669        if (!dups.get(inI)) {
670          newEntries[outI++] = entries[inI];
671        }
672      }
673      return newEntries;
674    }
675  }
676
677  /**
678   * Returns an immutable map containing the same entries as {@code map}. The returned map iterates
679   * over entries in the same order as the {@code entrySet} of the original map. If {@code map}
680   * somehow contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose
681   * comparator is not <i>consistent with equals</i>), the results of this method are undefined.
682   *
683   * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
684   * safe to do so. The exact circumstances under which a copy will or will not be performed are
685   * undocumented and subject to change.
686   *
687   * @throws NullPointerException if any key or value in {@code map} is null
688   */
689  public static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V> map) {
690    if ((map instanceof ImmutableMap) && !(map instanceof SortedMap)) {
691      @SuppressWarnings("unchecked") // safe since map is not writable
692      ImmutableMap<K, V> kvMap = (ImmutableMap<K, V>) map;
693      if (!kvMap.isPartialView()) {
694        return kvMap;
695      }
696    } else if (map instanceof EnumMap) {
697      @SuppressWarnings("unchecked") // safe since map is not writable
698      ImmutableMap<K, V> kvMap =
699          (ImmutableMap<K, V>)
700              copyOfEnumMap(
701                  (EnumMap<?, ? extends V>) map); // hide K (violates bounds) from J2KT, preserve V.
702      return kvMap;
703    }
704    return copyOf(map.entrySet());
705  }
706
707  /**
708   * Returns an immutable map containing the specified entries. The returned map iterates over
709   * entries in the same order as the original iterable.
710   *
711   * @throws NullPointerException if any key, value, or entry is null
712   * @throws IllegalArgumentException if two entries have the same key
713   * @since 19.0
714   */
715  public static <K, V> ImmutableMap<K, V> copyOf(
716      Iterable<? extends Entry<? extends K, ? extends V>> entries) {
717    @SuppressWarnings("unchecked") // we'll only be using getKey and getValue, which are covariant
718    Entry<K, V>[] entryArray = (Entry<K, V>[]) Iterables.toArray(entries, EMPTY_ENTRY_ARRAY);
719    switch (entryArray.length) {
720      case 0:
721        return of();
722      case 1:
723        // requireNonNull is safe because the first `size` elements have been filled in.
724        Entry<K, V> onlyEntry = requireNonNull(entryArray[0]);
725        return of(onlyEntry.getKey(), onlyEntry.getValue());
726      default:
727        /*
728         * The current implementation will end up using entryArray directly, though it will write
729         * over the (arbitrary, potentially mutable) Entry objects actually stored in entryArray.
730         */
731        return RegularImmutableMap.fromEntries(entryArray);
732    }
733  }
734
735  private static <K extends Enum<K>, V> ImmutableMap<K, ? extends V> copyOfEnumMap(
736      EnumMap<?, ? extends V> original) {
737    @SuppressWarnings("unchecked") // the best we could do to make copyOf(Map) compile
738    EnumMap<K, V> copy = new EnumMap<>((EnumMap<K, ? extends V>) original);
739    for (Entry<K, V> entry : copy.entrySet()) {
740      checkEntryNotNull(entry.getKey(), entry.getValue());
741    }
742    return ImmutableEnumMap.asImmutable(copy);
743  }
744
745  static final Entry<?, ?>[] EMPTY_ENTRY_ARRAY = new Entry<?, ?>[0];
746
747  abstract static class IteratorBasedImmutableMap<K, V> extends ImmutableMap<K, V> {
748    abstract UnmodifiableIterator<Entry<K, V>> entryIterator();
749
750    Spliterator<Entry<K, V>> entrySpliterator() {
751      return Spliterators.spliterator(
752          entryIterator(),
753          size(),
754          Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.IMMUTABLE | Spliterator.ORDERED);
755    }
756
757    @Override
758    ImmutableSet<K> createKeySet() {
759      return new ImmutableMapKeySet<>(this);
760    }
761
762    @Override
763    ImmutableSet<Entry<K, V>> createEntrySet() {
764      class EntrySetImpl extends ImmutableMapEntrySet<K, V> {
765        @Override
766        ImmutableMap<K, V> map() {
767          return IteratorBasedImmutableMap.this;
768        }
769
770        @Override
771        public UnmodifiableIterator<Entry<K, V>> iterator() {
772          return entryIterator();
773        }
774
775        // redeclare to help optimizers with b/310253115
776        @SuppressWarnings("RedundantOverride")
777        @Override
778        @J2ktIncompatible // serialization
779        @GwtIncompatible // serialization
780        Object writeReplace() {
781          return super.writeReplace();
782        }
783      }
784      return new EntrySetImpl();
785    }
786
787    @Override
788    ImmutableCollection<V> createValues() {
789      return new ImmutableMapValues<>(this);
790    }
791
792    // redeclare to help optimizers with b/310253115
793    @SuppressWarnings("RedundantOverride")
794    @Override
795    @J2ktIncompatible // serialization
796    @GwtIncompatible // serialization
797    Object writeReplace() {
798      return super.writeReplace();
799    }
800  }
801
802  ImmutableMap() {}
803
804  /**
805   * Guaranteed to throw an exception and leave the map unmodified.
806   *
807   * @throws UnsupportedOperationException always
808   * @deprecated Unsupported operation.
809   */
810  @CanIgnoreReturnValue
811  @Deprecated
812  @Override
813  @DoNotCall("Always throws UnsupportedOperationException")
814  @CheckForNull
815  public final V put(K k, V v) {
816    throw new UnsupportedOperationException();
817  }
818
819  /**
820   * Guaranteed to throw an exception and leave the map unmodified.
821   *
822   * @throws UnsupportedOperationException always
823   * @deprecated Unsupported operation.
824   */
825  @CanIgnoreReturnValue
826  @Deprecated
827  @Override
828  @DoNotCall("Always throws UnsupportedOperationException")
829  @CheckForNull
830  public final V putIfAbsent(K key, V value) {
831    throw new UnsupportedOperationException();
832  }
833
834  /**
835   * Guaranteed to throw an exception and leave the map unmodified.
836   *
837   * @throws UnsupportedOperationException always
838   * @deprecated Unsupported operation.
839   */
840  @Deprecated
841  @Override
842  @DoNotCall("Always throws UnsupportedOperationException")
843  public final boolean replace(K key, V oldValue, V newValue) {
844    throw new UnsupportedOperationException();
845  }
846
847  /**
848   * Guaranteed to throw an exception and leave the map unmodified.
849   *
850   * @throws UnsupportedOperationException always
851   * @deprecated Unsupported operation.
852   */
853  @Deprecated
854  @Override
855  @CheckForNull
856  @DoNotCall("Always throws UnsupportedOperationException")
857  public final V replace(K key, V value) {
858    throw new UnsupportedOperationException();
859  }
860
861  /**
862   * Guaranteed to throw an exception and leave the map unmodified.
863   *
864   * @throws UnsupportedOperationException always
865   * @deprecated Unsupported operation.
866   */
867  @Deprecated
868  @Override
869  @DoNotCall("Always throws UnsupportedOperationException")
870  public final V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
871    throw new UnsupportedOperationException();
872  }
873
874  /**
875   * Guaranteed to throw an exception and leave the map unmodified.
876   *
877   * @throws UnsupportedOperationException always
878   * @deprecated Unsupported operation.
879   */
880  @Deprecated
881  @Override
882  @DoNotCall("Always throws UnsupportedOperationException")
883  @CheckForNull
884  public final V computeIfPresent(
885      K key, BiFunction<? super K, ? super V, ? extends @Nullable V> remappingFunction) {
886    throw new UnsupportedOperationException();
887  }
888
889  /**
890   * Guaranteed to throw an exception and leave the map unmodified.
891   *
892   * @throws UnsupportedOperationException always
893   * @deprecated Unsupported operation.
894   */
895  @Deprecated
896  @Override
897  @DoNotCall("Always throws UnsupportedOperationException")
898  @CheckForNull
899  public final V compute(
900      K key, BiFunction<? super K, ? super @Nullable V, ? extends @Nullable V> remappingFunction) {
901    throw new UnsupportedOperationException();
902  }
903
904  /**
905   * Guaranteed to throw an exception and leave the map unmodified.
906   *
907   * @throws UnsupportedOperationException always
908   * @deprecated Unsupported operation.
909   */
910  @Deprecated
911  @Override
912  @DoNotCall("Always throws UnsupportedOperationException")
913  @CheckForNull
914  public final V merge(
915      K key, V value, BiFunction<? super V, ? super V, ? extends @Nullable V> function) {
916    throw new UnsupportedOperationException();
917  }
918
919  /**
920   * Guaranteed to throw an exception and leave the map unmodified.
921   *
922   * @throws UnsupportedOperationException always
923   * @deprecated Unsupported operation.
924   */
925  @Deprecated
926  @Override
927  @DoNotCall("Always throws UnsupportedOperationException")
928  public final void putAll(Map<? extends K, ? extends V> map) {
929    throw new UnsupportedOperationException();
930  }
931
932  /**
933   * Guaranteed to throw an exception and leave the map unmodified.
934   *
935   * @throws UnsupportedOperationException always
936   * @deprecated Unsupported operation.
937   */
938  @Deprecated
939  @Override
940  @DoNotCall("Always throws UnsupportedOperationException")
941  public final void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
942    throw new UnsupportedOperationException();
943  }
944
945  /**
946   * Guaranteed to throw an exception and leave the map unmodified.
947   *
948   * @throws UnsupportedOperationException always
949   * @deprecated Unsupported operation.
950   */
951  @Deprecated
952  @Override
953  @DoNotCall("Always throws UnsupportedOperationException")
954  @CheckForNull
955  public final V remove(@CheckForNull Object o) {
956    throw new UnsupportedOperationException();
957  }
958
959  /**
960   * Guaranteed to throw an exception and leave the map unmodified.
961   *
962   * @throws UnsupportedOperationException always
963   * @deprecated Unsupported operation.
964   */
965  @Deprecated
966  @Override
967  @DoNotCall("Always throws UnsupportedOperationException")
968  public final boolean remove(@CheckForNull Object key, @CheckForNull Object value) {
969    throw new UnsupportedOperationException();
970  }
971
972  /**
973   * Guaranteed to throw an exception and leave the map unmodified.
974   *
975   * @throws UnsupportedOperationException always
976   * @deprecated Unsupported operation.
977   */
978  @Deprecated
979  @Override
980  @DoNotCall("Always throws UnsupportedOperationException")
981  public final void clear() {
982    throw new UnsupportedOperationException();
983  }
984
985  @Override
986  public boolean isEmpty() {
987    return size() == 0;
988  }
989
990  @Override
991  public boolean containsKey(@CheckForNull Object key) {
992    return get(key) != null;
993  }
994
995  @Override
996  public boolean containsValue(@CheckForNull Object value) {
997    return values().contains(value);
998  }
999
1000  // Overriding to mark it Nullable
1001  @Override
1002  @CheckForNull
1003  public abstract V get(@CheckForNull Object key);
1004
1005  /**
1006   * @since 21.0 (but only since 23.5 in the Android <a
1007   *     href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>).
1008   *     Note, however, that Java 8+ users can call this method with any version and flavor of
1009   *     Guava.
1010   */
1011  @Override
1012  @CheckForNull
1013  public final V getOrDefault(@CheckForNull Object key, @CheckForNull V defaultValue) {
1014    /*
1015     * Even though it's weird to pass a defaultValue that is null, some callers do so. Those who
1016     * pass a literal "null" should probably just use `get`, but I would expect other callers to
1017     * pass an expression that *might* be null. This could happen with:
1018     *
1019     * - a `getFooOrDefault(@CheckForNull Foo defaultValue)` method that returns
1020     *   `map.getOrDefault(FOO_KEY, defaultValue)`
1021     *
1022     * - a call that consults a chain of maps, as in `mapA.getOrDefault(key, mapB.getOrDefault(key,
1023     *   ...))`
1024     *
1025     * So it makes sense for the parameter (and thus the return type) to be @CheckForNull.
1026     *
1027     * Two other points:
1028     *
1029     * 1. We'll want to use something like @PolyNull once we can make that work for the various
1030     * platforms we target.
1031     *
1032     * 2. Kotlin's Map type has a getOrDefault method that accepts and returns a "plain V," in
1033     * contrast to the "V?" type that we're using. As a result, Kotlin sees a conflict between the
1034     * nullness annotations in ImmutableMap and those in its own Map type. In response, it considers
1035     * the parameter and return type both to be platform types. As a result, Kotlin permits calls
1036     * that can lead to NullPointerException. That's unfortunate. But hopefully most Kotlin callers
1037     * use `get(key) ?: defaultValue` instead of this method, anyway.
1038     */
1039    V result = get(key);
1040    // TODO(b/192579700): Use a ternary once it no longer confuses our nullness checker.
1041    if (result != null) {
1042      return result;
1043    } else {
1044      return defaultValue;
1045    }
1046  }
1047
1048  @LazyInit @RetainedWith @CheckForNull private transient ImmutableSet<Entry<K, V>> entrySet;
1049
1050  /**
1051   * Returns an immutable set of the mappings in this map. The iteration order is specified by the
1052   * method used to create this map. Typically, this is insertion order.
1053   */
1054  @Override
1055  public ImmutableSet<Entry<K, V>> entrySet() {
1056    ImmutableSet<Entry<K, V>> result = entrySet;
1057    return (result == null) ? entrySet = createEntrySet() : result;
1058  }
1059
1060  abstract ImmutableSet<Entry<K, V>> createEntrySet();
1061
1062  @LazyInit @RetainedWith @CheckForNull private transient ImmutableSet<K> keySet;
1063
1064  /**
1065   * Returns an immutable set of the keys in this map, in the same order that they appear in {@link
1066   * #entrySet}.
1067   */
1068  @Override
1069  public ImmutableSet<K> keySet() {
1070    ImmutableSet<K> result = keySet;
1071    return (result == null) ? keySet = createKeySet() : result;
1072  }
1073
1074  /*
1075   * This could have a good default implementation of return new ImmutableKeySet<K, V>(this),
1076   * but ProGuard can't figure out how to eliminate that default when RegularImmutableMap
1077   * overrides it.
1078   */
1079  abstract ImmutableSet<K> createKeySet();
1080
1081  UnmodifiableIterator<K> keyIterator() {
1082    final UnmodifiableIterator<Entry<K, V>> entryIterator = entrySet().iterator();
1083    return new UnmodifiableIterator<K>() {
1084      @Override
1085      public boolean hasNext() {
1086        return entryIterator.hasNext();
1087      }
1088
1089      @Override
1090      public K next() {
1091        return entryIterator.next().getKey();
1092      }
1093    };
1094  }
1095
1096  Spliterator<K> keySpliterator() {
1097    return CollectSpliterators.map(entrySet().spliterator(), Entry::getKey);
1098  }
1099
1100  @LazyInit @RetainedWith @CheckForNull private transient ImmutableCollection<V> values;
1101
1102  /**
1103   * Returns an immutable collection of the values in this map, in the same order that they appear
1104   * in {@link #entrySet}.
1105   */
1106  @Override
1107  public ImmutableCollection<V> values() {
1108    ImmutableCollection<V> result = values;
1109    return (result == null) ? values = createValues() : result;
1110  }
1111
1112  /*
1113   * This could have a good default implementation of {@code return new
1114   * ImmutableMapValues<K, V>(this)}, but ProGuard can't figure out how to eliminate that default
1115   * when RegularImmutableMap overrides it.
1116   */
1117  abstract ImmutableCollection<V> createValues();
1118
1119  // cached so that this.multimapView().inverse() only computes inverse once
1120  @LazyInit @CheckForNull private transient ImmutableSetMultimap<K, V> multimapView;
1121
1122  /**
1123   * Returns a multimap view of the map.
1124   *
1125   * @since 14.0
1126   */
1127  public ImmutableSetMultimap<K, V> asMultimap() {
1128    if (isEmpty()) {
1129      return ImmutableSetMultimap.of();
1130    }
1131    ImmutableSetMultimap<K, V> result = multimapView;
1132    return (result == null)
1133        ? (multimapView =
1134            new ImmutableSetMultimap<>(new MapViewOfValuesAsSingletonSets(), size(), null))
1135        : result;
1136  }
1137
1138  @WeakOuter
1139  private final class MapViewOfValuesAsSingletonSets
1140      extends IteratorBasedImmutableMap<K, ImmutableSet<V>> {
1141
1142    @Override
1143    public int size() {
1144      return ImmutableMap.this.size();
1145    }
1146
1147    @Override
1148    ImmutableSet<K> createKeySet() {
1149      return ImmutableMap.this.keySet();
1150    }
1151
1152    @Override
1153    public boolean containsKey(@CheckForNull Object key) {
1154      return ImmutableMap.this.containsKey(key);
1155    }
1156
1157    @Override
1158    @CheckForNull
1159    public ImmutableSet<V> get(@CheckForNull Object key) {
1160      V outerValue = ImmutableMap.this.get(key);
1161      return (outerValue == null) ? null : ImmutableSet.of(outerValue);
1162    }
1163
1164    @Override
1165    boolean isPartialView() {
1166      return ImmutableMap.this.isPartialView();
1167    }
1168
1169    @Override
1170    public int hashCode() {
1171      // ImmutableSet.of(value).hashCode() == value.hashCode(), so the hashes are the same
1172      return ImmutableMap.this.hashCode();
1173    }
1174
1175    @Override
1176    boolean isHashCodeFast() {
1177      return ImmutableMap.this.isHashCodeFast();
1178    }
1179
1180    @Override
1181    UnmodifiableIterator<Entry<K, ImmutableSet<V>>> entryIterator() {
1182      final Iterator<Entry<K, V>> backingIterator = ImmutableMap.this.entrySet().iterator();
1183      return new UnmodifiableIterator<Entry<K, ImmutableSet<V>>>() {
1184        @Override
1185        public boolean hasNext() {
1186          return backingIterator.hasNext();
1187        }
1188
1189        @Override
1190        public Entry<K, ImmutableSet<V>> next() {
1191          final Entry<K, V> backingEntry = backingIterator.next();
1192          return new AbstractMapEntry<K, ImmutableSet<V>>() {
1193            @Override
1194            public K getKey() {
1195              return backingEntry.getKey();
1196            }
1197
1198            @Override
1199            public ImmutableSet<V> getValue() {
1200              return ImmutableSet.of(backingEntry.getValue());
1201            }
1202          };
1203        }
1204      };
1205    }
1206
1207    // redeclare to help optimizers with b/310253115
1208    @SuppressWarnings("RedundantOverride")
1209    @Override
1210    @J2ktIncompatible // serialization
1211    @GwtIncompatible // serialization
1212    Object writeReplace() {
1213      return super.writeReplace();
1214    }
1215  }
1216
1217  @Override
1218  public boolean equals(@CheckForNull Object object) {
1219    return Maps.equalsImpl(this, object);
1220  }
1221
1222  abstract boolean isPartialView();
1223
1224  @Override
1225  public int hashCode() {
1226    return Sets.hashCodeImpl(entrySet());
1227  }
1228
1229  boolean isHashCodeFast() {
1230    return false;
1231  }
1232
1233  @Override
1234  public String toString() {
1235    return Maps.toStringImpl(this);
1236  }
1237
1238  /**
1239   * Serialized type for all ImmutableMap instances. It captures the logical contents and they are
1240   * reconstructed using public factory methods. This ensures that the implementation types remain
1241   * as implementation details.
1242   */
1243  @J2ktIncompatible // serialization
1244  static class SerializedForm<K, V> implements Serializable {
1245    // This object retains references to collections returned by keySet() and value(). This saves
1246    // bytes when the both the map and its keySet or value collection are written to the same
1247    // instance of ObjectOutputStream.
1248
1249    // TODO(b/160980469): remove support for the old serialization format after some time
1250    private static final boolean USE_LEGACY_SERIALIZATION = true;
1251
1252    private final Object keys;
1253    private final Object values;
1254
1255    SerializedForm(ImmutableMap<K, V> map) {
1256      if (USE_LEGACY_SERIALIZATION) {
1257        Object[] keys = new Object[map.size()];
1258        Object[] values = new Object[map.size()];
1259        int i = 0;
1260        // "extends Object" works around https://github.com/typetools/checker-framework/issues/3013
1261        for (Entry<? extends Object, ? extends Object> entry : map.entrySet()) {
1262          keys[i] = entry.getKey();
1263          values[i] = entry.getValue();
1264          i++;
1265        }
1266        this.keys = keys;
1267        this.values = values;
1268        return;
1269      }
1270      this.keys = map.keySet();
1271      this.values = map.values();
1272    }
1273
1274    @SuppressWarnings("unchecked")
1275    final Object readResolve() {
1276      if (!(this.keys instanceof ImmutableSet)) {
1277        return legacyReadResolve();
1278      }
1279
1280      ImmutableSet<K> keySet = (ImmutableSet<K>) this.keys;
1281      ImmutableCollection<V> values = (ImmutableCollection<V>) this.values;
1282
1283      Builder<K, V> builder = makeBuilder(keySet.size());
1284
1285      UnmodifiableIterator<K> keyIter = keySet.iterator();
1286      UnmodifiableIterator<V> valueIter = values.iterator();
1287
1288      while (keyIter.hasNext()) {
1289        builder.put(keyIter.next(), valueIter.next());
1290      }
1291
1292      return builder.buildOrThrow();
1293    }
1294
1295    @SuppressWarnings("unchecked")
1296    final Object legacyReadResolve() {
1297      K[] keys = (K[]) this.keys;
1298      V[] values = (V[]) this.values;
1299
1300      Builder<K, V> builder = makeBuilder(keys.length);
1301
1302      for (int i = 0; i < keys.length; i++) {
1303        builder.put(keys[i], values[i]);
1304      }
1305      return builder.buildOrThrow();
1306    }
1307
1308    /**
1309     * Returns a builder that builds the unserialized type. Subclasses should override this method.
1310     */
1311    Builder<K, V> makeBuilder(int size) {
1312      return new Builder<>(size);
1313    }
1314
1315    private static final long serialVersionUID = 0;
1316  }
1317
1318  /**
1319   * Returns a serializable form of this object. Non-public subclasses should not override this
1320   * method. Publicly-accessible subclasses must override this method and should return a subclass
1321   * of SerializedForm whose readResolve() method returns objects of the subclass type.
1322   */
1323  @J2ktIncompatible // serialization
1324  Object writeReplace() {
1325    return new SerializedForm<>(this);
1326  }
1327
1328  @J2ktIncompatible // java.io.ObjectInputStream
1329  private void readObject(ObjectInputStream stream) throws InvalidObjectException {
1330    throw new InvalidObjectException("Use SerializedForm");
1331  }
1332
1333  private static final long serialVersionUID = 0xcafebabe;
1334}