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