001/*
002 * Copyright (C) 2009 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.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
022import static com.google.common.collect.Maps.keyOrNull;
023import static java.util.Objects.requireNonNull;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.annotations.GwtIncompatible;
027import com.google.common.annotations.J2ktIncompatible;
028import com.google.errorprone.annotations.CanIgnoreReturnValue;
029import com.google.errorprone.annotations.DoNotCall;
030import java.io.InvalidObjectException;
031import java.io.ObjectInputStream;
032import java.util.AbstractMap;
033import java.util.Arrays;
034import java.util.Comparator;
035import java.util.Map;
036import java.util.NavigableMap;
037import java.util.SortedMap;
038import java.util.Spliterator;
039import java.util.TreeMap;
040import java.util.function.BiConsumer;
041import java.util.function.BinaryOperator;
042import java.util.function.Consumer;
043import java.util.function.Function;
044import java.util.stream.Collector;
045import java.util.stream.Collectors;
046import javax.annotation.CheckForNull;
047import org.checkerframework.checker.nullness.qual.Nullable;
048
049/**
050 * A {@link NavigableMap} whose contents will never change, with many other important properties
051 * detailed at {@link ImmutableCollection}.
052 *
053 * <p><b>Warning:</b> as with any sorted collection, you are strongly advised not to use a {@link
054 * Comparator} or {@link Comparable} type whose comparison behavior is <i>inconsistent with
055 * equals</i>. That is, {@code a.compareTo(b)} or {@code comparator.compare(a, b)} should equal zero
056 * <i>if and only if</i> {@code a.equals(b)}. If this advice is not followed, the resulting map will
057 * not correctly obey its specification.
058 *
059 * <p>See the Guava User Guide article on <a href=
060 * "https://github.com/google/guava/wiki/ImmutableCollectionsExplained">immutable collections</a>.
061 *
062 * @author Jared Levy
063 * @author Louis Wasserman
064 * @since 2.0 (implements {@code NavigableMap} since 12.0)
065 */
066@GwtCompatible(serializable = true, emulated = true)
067@ElementTypesAreNonnullByDefault
068public final class ImmutableSortedMap<K, V> extends ImmutableMap<K, V>
069    implements NavigableMap<K, V> {
070  /**
071   * Returns a {@link Collector} that accumulates elements into an {@code ImmutableSortedMap} whose
072   * keys and values are the result of applying the provided mapping functions to the input
073   * elements. The generated map is sorted by the specified comparator.
074   *
075   * <p>If the mapped keys contain duplicates (according to the specified comparator), an {@code
076   * IllegalArgumentException} is thrown when the collection operation is performed. (This differs
077   * from the {@code Collector} returned by {@link Collectors#toMap(Function, Function)}, which
078   * throws an {@code IllegalStateException}.)
079   *
080   * @since 21.0
081   */
082  public static <T extends @Nullable Object, K, V>
083      Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
084          Comparator<? super K> comparator,
085          Function<? super T, ? extends K> keyFunction,
086          Function<? super T, ? extends V> valueFunction) {
087    return CollectCollectors.toImmutableSortedMap(comparator, keyFunction, valueFunction);
088  }
089
090  /**
091   * Returns a {@link Collector} that accumulates elements into an {@code ImmutableSortedMap} whose
092   * keys and values are the result of applying the provided mapping functions to the input
093   * elements.
094   *
095   * <p>If the mapped keys contain duplicates (according to the comparator), the values are merged
096   * using the specified merging function. Entries will appear in the encounter order of the first
097   * occurrence of the key.
098   *
099   * @since 21.0
100   */
101  public static <T extends @Nullable Object, K, V>
102      Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
103          Comparator<? super K> comparator,
104          Function<? super T, ? extends K> keyFunction,
105          Function<? super T, ? extends V> valueFunction,
106          BinaryOperator<V> mergeFunction) {
107    return CollectCollectors.toImmutableSortedMap(
108        comparator, keyFunction, valueFunction, mergeFunction);
109  }
110
111  /*
112   * TODO(kevinb): Confirm that ImmutableSortedMap is faster to construct and
113   * uses less memory than TreeMap; then say so in the class Javadoc.
114   */
115  private static final Comparator<Comparable> NATURAL_ORDER = Ordering.natural();
116
117  private static final ImmutableSortedMap<Comparable, Object> NATURAL_EMPTY_MAP =
118      new ImmutableSortedMap<>(
119          ImmutableSortedSet.emptySet(Ordering.natural()), ImmutableList.<Object>of());
120
121  static <K, V> ImmutableSortedMap<K, V> emptyMap(Comparator<? super K> comparator) {
122    if (Ordering.natural().equals(comparator)) {
123      return of();
124    } else {
125      return new ImmutableSortedMap<>(
126          ImmutableSortedSet.emptySet(comparator), ImmutableList.<V>of());
127    }
128  }
129
130  /**
131   * Returns the empty sorted map.
132   *
133   * <p><b>Performance note:</b> the instance returned is a singleton.
134   */
135  @SuppressWarnings("unchecked")
136  // unsafe, comparator() returns a comparator on the specified type
137  // TODO(kevinb): evaluate whether or not of().comparator() should return null
138  public static <K, V> ImmutableSortedMap<K, V> of() {
139    return (ImmutableSortedMap<K, V>) NATURAL_EMPTY_MAP;
140  }
141
142  /** Returns an immutable map containing a single entry. */
143  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(K k1, V v1) {
144    return of(Ordering.natural(), k1, v1);
145  }
146
147  /** Returns an immutable map containing a single entry. */
148  private static <K, V> ImmutableSortedMap<K, V> of(Comparator<? super K> comparator, K k1, V v1) {
149    return new ImmutableSortedMap<>(
150        new RegularImmutableSortedSet<K>(ImmutableList.of(k1), checkNotNull(comparator)),
151        ImmutableList.of(v1));
152  }
153
154  /**
155   * Returns an immutable sorted map containing the given entries, sorted by the natural ordering of
156   * their keys.
157   *
158   * @throws IllegalArgumentException if the two keys are equal according to their natural ordering
159   */
160  @SuppressWarnings("unchecked")
161  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
162      K k1, V v1, K k2, V v2) {
163    return fromEntries(entryOf(k1, v1), entryOf(k2, v2));
164  }
165
166  /**
167   * Returns an immutable sorted map containing the given entries, sorted by the natural ordering of
168   * their keys.
169   *
170   * @throws IllegalArgumentException if any two keys are equal according to their natural ordering
171   */
172  @SuppressWarnings("unchecked")
173  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
174      K k1, V v1, K k2, V v2, K k3, V v3) {
175    return fromEntries(entryOf(k1, v1), entryOf(k2, v2), entryOf(k3, v3));
176  }
177
178  /**
179   * Returns an immutable sorted map containing the given entries, sorted by the natural ordering of
180   * their keys.
181   *
182   * @throws IllegalArgumentException if any two keys are equal according to their natural ordering
183   */
184  @SuppressWarnings("unchecked")
185  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
186      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
187    return fromEntries(entryOf(k1, v1), entryOf(k2, v2), entryOf(k3, v3), entryOf(k4, v4));
188  }
189
190  /**
191   * Returns an immutable sorted map containing the given entries, sorted by the natural ordering of
192   * their keys.
193   *
194   * @throws IllegalArgumentException if any two keys are equal according to their natural ordering
195   */
196  @SuppressWarnings("unchecked")
197  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
198      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
199    return fromEntries(
200        entryOf(k1, v1), entryOf(k2, v2), entryOf(k3, v3), entryOf(k4, v4), entryOf(k5, v5));
201  }
202
203  /**
204   * Returns an immutable sorted map containing the given entries, sorted by the natural ordering of
205   * their keys.
206   *
207   * @throws IllegalArgumentException if any two keys are equal according to their natural ordering
208   * @since 31.0
209   */
210  @SuppressWarnings("unchecked")
211  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
212      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
213    return fromEntries(
214        entryOf(k1, v1),
215        entryOf(k2, v2),
216        entryOf(k3, v3),
217        entryOf(k4, v4),
218        entryOf(k5, v5),
219        entryOf(k6, v6));
220  }
221
222  /**
223   * Returns an immutable sorted map containing the given entries, sorted by the natural ordering of
224   * their keys.
225   *
226   * @throws IllegalArgumentException if any two keys are equal according to their natural ordering
227   * @since 31.0
228   */
229  @SuppressWarnings("unchecked")
230  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
231      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) {
232    return fromEntries(
233        entryOf(k1, v1),
234        entryOf(k2, v2),
235        entryOf(k3, v3),
236        entryOf(k4, v4),
237        entryOf(k5, v5),
238        entryOf(k6, v6),
239        entryOf(k7, v7));
240  }
241
242  /**
243   * Returns an immutable sorted map containing the given entries, sorted by the natural ordering of
244   * their keys.
245   *
246   * @throws IllegalArgumentException if any two keys are equal according to their natural ordering
247   * @since 31.0
248   */
249  @SuppressWarnings("unchecked")
250  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
251      K k1,
252      V v1,
253      K k2,
254      V v2,
255      K k3,
256      V v3,
257      K k4,
258      V v4,
259      K k5,
260      V v5,
261      K k6,
262      V v6,
263      K k7,
264      V v7,
265      K k8,
266      V v8) {
267    return fromEntries(
268        entryOf(k1, v1),
269        entryOf(k2, v2),
270        entryOf(k3, v3),
271        entryOf(k4, v4),
272        entryOf(k5, v5),
273        entryOf(k6, v6),
274        entryOf(k7, v7),
275        entryOf(k8, v8));
276  }
277
278  /**
279   * Returns an immutable sorted map containing the given entries, sorted by the natural ordering of
280   * their keys.
281   *
282   * @throws IllegalArgumentException if any two keys are equal according to their natural ordering
283   * @since 31.0
284   */
285  @SuppressWarnings("unchecked")
286  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
287      K k1,
288      V v1,
289      K k2,
290      V v2,
291      K k3,
292      V v3,
293      K k4,
294      V v4,
295      K k5,
296      V v5,
297      K k6,
298      V v6,
299      K k7,
300      V v7,
301      K k8,
302      V v8,
303      K k9,
304      V v9) {
305    return fromEntries(
306        entryOf(k1, v1),
307        entryOf(k2, v2),
308        entryOf(k3, v3),
309        entryOf(k4, v4),
310        entryOf(k5, v5),
311        entryOf(k6, v6),
312        entryOf(k7, v7),
313        entryOf(k8, v8),
314        entryOf(k9, v9));
315  }
316
317  /**
318   * Returns an immutable sorted map containing the given entries, sorted by the natural ordering of
319   * their keys.
320   *
321   * @throws IllegalArgumentException if any two keys are equal according to their natural ordering
322   * @since 31.0
323   */
324  @SuppressWarnings("unchecked")
325  public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
326      K k1,
327      V v1,
328      K k2,
329      V v2,
330      K k3,
331      V v3,
332      K k4,
333      V v4,
334      K k5,
335      V v5,
336      K k6,
337      V v6,
338      K k7,
339      V v7,
340      K k8,
341      V v8,
342      K k9,
343      V v9,
344      K k10,
345      V v10) {
346    return fromEntries(
347        entryOf(k1, v1),
348        entryOf(k2, v2),
349        entryOf(k3, v3),
350        entryOf(k4, v4),
351        entryOf(k5, v5),
352        entryOf(k6, v6),
353        entryOf(k7, v7),
354        entryOf(k8, v8),
355        entryOf(k9, v9),
356        entryOf(k10, v10));
357  }
358
359  /**
360   * Returns an immutable map containing the same entries as {@code map}, sorted by the natural
361   * ordering of the keys.
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   * <p>This method is not type-safe, as it may be called on a map with keys that are not mutually
368   * comparable.
369   *
370   * @throws ClassCastException if the keys in {@code map} are not mutually comparable
371   * @throws NullPointerException if any key or value in {@code map} is null
372   * @throws IllegalArgumentException if any two keys are equal according to their natural ordering
373   */
374  public static <K, V> ImmutableSortedMap<K, V> copyOf(Map<? extends K, ? extends V> map) {
375    // Hack around K not being a subtype of Comparable.
376    // Unsafe, see ImmutableSortedSetFauxverideShim.
377    @SuppressWarnings("unchecked")
378    Ordering<K> naturalOrder = (Ordering<K>) NATURAL_ORDER;
379    return copyOfInternal(map, naturalOrder);
380  }
381
382  /**
383   * Returns an immutable map containing the same entries as {@code map}, with keys sorted by the
384   * provided comparator.
385   *
386   * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
387   * safe to do so. The exact circumstances under which a copy will or will not be performed are
388   * undocumented and subject to change.
389   *
390   * @throws NullPointerException if any key or value in {@code map} is null
391   * @throws IllegalArgumentException if any two keys are equal according to the comparator
392   */
393  public static <K, V> ImmutableSortedMap<K, V> copyOf(
394      Map<? extends K, ? extends V> map, Comparator<? super K> comparator) {
395    return copyOfInternal(map, checkNotNull(comparator));
396  }
397
398  /**
399   * Returns an immutable map containing the given entries, with keys sorted by their natural
400   * ordering.
401   *
402   * <p>This method is not type-safe, as it may be called on a map with keys that are not mutually
403   * comparable.
404   *
405   * @throws NullPointerException if any key or value in {@code map} is null
406   * @throws IllegalArgumentException if any two keys are equal according to the comparator
407   * @since 19.0
408   */
409  public static <K, V> ImmutableSortedMap<K, V> copyOf(
410      Iterable<? extends Entry<? extends K, ? extends V>> entries) {
411    // Hack around K not being a subtype of Comparable.
412    // Unsafe, see ImmutableSortedSetFauxverideShim.
413    @SuppressWarnings("unchecked")
414    Ordering<K> naturalOrder = (Ordering<K>) NATURAL_ORDER;
415    return copyOf(entries, naturalOrder);
416  }
417
418  /**
419   * Returns an immutable map containing the given entries, with keys sorted by the provided
420   * comparator.
421   *
422   * @throws NullPointerException if any key or value in {@code map} is null
423   * @throws IllegalArgumentException if any two keys are equal according to the comparator
424   * @since 19.0
425   */
426  public static <K, V> ImmutableSortedMap<K, V> copyOf(
427      Iterable<? extends Entry<? extends K, ? extends V>> entries,
428      Comparator<? super K> comparator) {
429    return fromEntries(checkNotNull(comparator), false, entries);
430  }
431
432  /**
433   * Returns an immutable map containing the same entries as the provided sorted map, with the same
434   * ordering.
435   *
436   * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
437   * safe to do so. The exact circumstances under which a copy will or will not be performed are
438   * undocumented and subject to change.
439   *
440   * @throws NullPointerException if any key or value in {@code map} is null
441   */
442  @SuppressWarnings("unchecked")
443  public static <K, V> ImmutableSortedMap<K, V> copyOfSorted(SortedMap<K, ? extends V> map) {
444    Comparator<? super K> comparator = map.comparator();
445    if (comparator == null) {
446      // If map has a null comparator, the keys should have a natural ordering,
447      // even though K doesn't explicitly implement Comparable.
448      comparator = (Comparator<? super K>) NATURAL_ORDER;
449    }
450    if (map instanceof ImmutableSortedMap) {
451      // TODO(kevinb): Prove that this cast is safe, even though
452      // Collections.unmodifiableSortedMap requires the same key type.
453      @SuppressWarnings("unchecked")
454      ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map;
455      if (!kvMap.isPartialView()) {
456        return kvMap;
457      }
458    }
459    return fromEntries(comparator, true, map.entrySet());
460  }
461
462  private static <K, V> ImmutableSortedMap<K, V> copyOfInternal(
463      Map<? extends K, ? extends V> map, Comparator<? super K> comparator) {
464    boolean sameComparator = false;
465    if (map instanceof SortedMap) {
466      SortedMap<?, ?> sortedMap = (SortedMap<?, ?>) map;
467      Comparator<?> comparator2 = sortedMap.comparator();
468      sameComparator =
469          (comparator2 == null) ? comparator == NATURAL_ORDER : comparator.equals(comparator2);
470    }
471
472    if (sameComparator && (map instanceof ImmutableSortedMap)) {
473      // TODO(kevinb): Prove that this cast is safe, even though
474      // Collections.unmodifiableSortedMap requires the same key type.
475      @SuppressWarnings("unchecked")
476      ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map;
477      if (!kvMap.isPartialView()) {
478        return kvMap;
479      }
480    }
481    return fromEntries(comparator, sameComparator, map.entrySet());
482  }
483
484  private static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> fromEntries(
485      Entry<K, V>... entries) {
486    return fromEntries(Ordering.natural(), false, entries, entries.length);
487  }
488
489  /**
490   * Accepts a collection of possibly-null entries. If {@code sameComparator}, then it is assumed
491   * that they do not need to be sorted or checked for dupes.
492   */
493  private static <K, V> ImmutableSortedMap<K, V> fromEntries(
494      Comparator<? super K> comparator,
495      boolean sameComparator,
496      Iterable<? extends Entry<? extends K, ? extends V>> entries) {
497    // "adding" type params to an array of a raw type should be safe as
498    // long as no one can ever cast that same array instance back to a
499    // raw type.
500    @SuppressWarnings("unchecked")
501    Entry<K, V>[] entryArray = (Entry[]) Iterables.toArray(entries, EMPTY_ENTRY_ARRAY);
502    return fromEntries(comparator, sameComparator, entryArray, entryArray.length);
503  }
504
505  private static <K, V> ImmutableSortedMap<K, V> fromEntries(
506      final Comparator<? super K> comparator,
507      boolean sameComparator,
508      @Nullable Entry<K, V>[] entryArray,
509      int size) {
510    switch (size) {
511      case 0:
512        return emptyMap(comparator);
513      case 1:
514        // requireNonNull is safe because the first `size` elements have been filled in.
515        Entry<K, V> onlyEntry = requireNonNull(entryArray[0]);
516        return of(comparator, onlyEntry.getKey(), onlyEntry.getValue());
517      default:
518        Object[] keys = new Object[size];
519        Object[] values = new Object[size];
520        if (sameComparator) {
521          // Need to check for nulls, but don't need to sort or validate.
522          for (int i = 0; i < size; i++) {
523            // requireNonNull is safe because the first `size` elements have been filled in.
524            Entry<K, V> entry = requireNonNull(entryArray[i]);
525            Object key = entry.getKey();
526            Object value = entry.getValue();
527            checkEntryNotNull(key, value);
528            keys[i] = key;
529            values[i] = value;
530          }
531        } else {
532          // Need to sort and check for nulls and dupes.
533          // Inline the Comparator implementation rather than transforming with a Function
534          // to save code size.
535          Arrays.sort(
536              entryArray,
537              0,
538              size,
539              (e1, e2) -> {
540                // requireNonNull is safe because the first `size` elements have been filled in.
541                requireNonNull(e1);
542                requireNonNull(e2);
543                return comparator.compare(e1.getKey(), e2.getKey());
544              });
545          // requireNonNull is safe because the first `size` elements have been filled in.
546          Entry<K, V> firstEntry = requireNonNull(entryArray[0]);
547          K prevKey = firstEntry.getKey();
548          keys[0] = prevKey;
549          values[0] = firstEntry.getValue();
550          checkEntryNotNull(keys[0], values[0]);
551          for (int i = 1; i < size; i++) {
552            // requireNonNull is safe because the first `size` elements have been filled in.
553            Entry<K, V> prevEntry = requireNonNull(entryArray[i - 1]);
554            Entry<K, V> entry = requireNonNull(entryArray[i]);
555            K key = entry.getKey();
556            V value = entry.getValue();
557            checkEntryNotNull(key, value);
558            keys[i] = key;
559            values[i] = value;
560            checkNoConflict(comparator.compare(prevKey, key) != 0, "key", prevEntry, entry);
561            prevKey = key;
562          }
563        }
564        return new ImmutableSortedMap<>(
565            new RegularImmutableSortedSet<K>(new RegularImmutableList<K>(keys), comparator),
566            new RegularImmutableList<V>(values));
567    }
568  }
569
570  /**
571   * Returns a builder that creates immutable sorted maps whose keys are ordered by their natural
572   * ordering. The sorted maps use {@link Ordering#natural()} as the comparator.
573   */
574  public static <K extends Comparable<?>, V> Builder<K, V> naturalOrder() {
575    return new Builder<>(Ordering.natural());
576  }
577
578  /**
579   * Returns a builder that creates immutable sorted maps with an explicit comparator. If the
580   * comparator has a more general type than the map's keys, such as creating a {@code
581   * SortedMap<Integer, String>} with a {@code Comparator<Number>}, use the {@link Builder}
582   * constructor instead.
583   *
584   * @throws NullPointerException if {@code comparator} is null
585   */
586  public static <K, V> Builder<K, V> orderedBy(Comparator<K> comparator) {
587    return new Builder<>(comparator);
588  }
589
590  /**
591   * Returns a builder that creates immutable sorted maps whose keys are ordered by the reverse of
592   * their natural ordering.
593   */
594  public static <K extends Comparable<?>, V> Builder<K, V> reverseOrder() {
595    return new Builder<>(Ordering.<K>natural().reverse());
596  }
597
598  /**
599   * A builder for creating immutable sorted map instances, especially {@code public static final}
600   * maps ("constant maps"). Example:
601   *
602   * <pre>{@code
603   * static final ImmutableSortedMap<Integer, String> INT_TO_WORD =
604   *     new ImmutableSortedMap.Builder<Integer, String>(Ordering.natural())
605   *         .put(1, "one")
606   *         .put(2, "two")
607   *         .put(3, "three")
608   *         .buildOrThrow();
609   * }</pre>
610   *
611   * <p>For <i>small</i> immutable sorted maps, the {@code ImmutableSortedMap.of()} methods are even
612   * more convenient.
613   *
614   * <p>Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to
615   * build multiple maps in series. Each map is a superset of the maps created before it.
616   *
617   * @since 2.0
618   */
619  public static class Builder<K, V> extends ImmutableMap.Builder<K, V> {
620    private final Comparator<? super K> comparator;
621
622    /**
623     * Creates a new builder. The returned builder is equivalent to the builder generated by {@link
624     * ImmutableSortedMap#orderedBy}.
625     */
626    @SuppressWarnings("unchecked")
627    public Builder(Comparator<? super K> comparator) {
628      this.comparator = checkNotNull(comparator);
629    }
630
631    /**
632     * Associates {@code key} with {@code value} in the built map. Duplicate keys, according to the
633     * comparator (which might be the keys' natural order), are not allowed, and will cause {@link
634     * #build} to fail.
635     */
636    @CanIgnoreReturnValue
637    @Override
638    public Builder<K, V> put(K key, V value) {
639      super.put(key, value);
640      return this;
641    }
642
643    /**
644     * Adds the given {@code entry} to the map, making it immutable if necessary. Duplicate keys,
645     * according to the comparator (which might be the keys' natural order), are not allowed, and
646     * will cause {@link #build} to fail.
647     *
648     * @since 11.0
649     */
650    @CanIgnoreReturnValue
651    @Override
652    public Builder<K, V> put(Entry<? extends K, ? extends V> entry) {
653      super.put(entry);
654      return this;
655    }
656
657    /**
658     * Associates all of the given map's keys and values in the built map. Duplicate keys, according
659     * to the comparator (which might be the keys' natural order), are not allowed, and will cause
660     * {@link #build} to fail.
661     *
662     * @throws NullPointerException if any key or value in {@code map} is null
663     */
664    @CanIgnoreReturnValue
665    @Override
666    public Builder<K, V> putAll(Map<? extends K, ? extends V> map) {
667      super.putAll(map);
668      return this;
669    }
670
671    /**
672     * Adds all the given entries to the built map. Duplicate keys, according to the comparator
673     * (which might be the keys' natural order), are not allowed, and will cause {@link #build} to
674     * fail.
675     *
676     * @throws NullPointerException if any key, value, or entry is null
677     * @since 19.0
678     */
679    @CanIgnoreReturnValue
680    @Override
681    public Builder<K, V> putAll(Iterable<? extends Entry<? extends K, ? extends V>> entries) {
682      super.putAll(entries);
683      return this;
684    }
685
686    /**
687     * Throws an {@code UnsupportedOperationException}.
688     *
689     * @since 19.0
690     * @deprecated Unsupported by ImmutableSortedMap.Builder.
691     */
692    @CanIgnoreReturnValue
693    @Override
694    @Deprecated
695    @DoNotCall("Always throws UnsupportedOperationException")
696    public final Builder<K, V> orderEntriesByValue(Comparator<? super V> valueComparator) {
697      throw new UnsupportedOperationException("Not available on ImmutableSortedMap.Builder");
698    }
699
700    @Override
701    Builder<K, V> combine(ImmutableMap.Builder<K, V> other) {
702      super.combine(other);
703      return this;
704    }
705
706    /**
707     * Returns a newly-created immutable sorted map.
708     *
709     * <p>Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method
710     * will throw an exception if there are duplicate keys. The {@code build()} method will soon be
711     * deprecated.
712     *
713     * @throws IllegalArgumentException if any two keys are equal according to the comparator (which
714     *     might be the keys' natural order)
715     */
716    @Override
717    public ImmutableSortedMap<K, V> build() {
718      return buildOrThrow();
719    }
720
721    /**
722     * Returns a newly-created immutable sorted map, or throws an exception if any two keys are
723     * equal.
724     *
725     * @throws IllegalArgumentException if any two keys are equal according to the comparator (which
726     *     might be the keys' natural order)
727     * @since 31.0
728     */
729    @Override
730    public ImmutableSortedMap<K, V> buildOrThrow() {
731      switch (size) {
732        case 0:
733          return emptyMap(comparator);
734        case 1:
735          // requireNonNull is safe because the first `size` elements have been filled in.
736          Entry<K, V> onlyEntry = requireNonNull(entries[0]);
737          return of(comparator, onlyEntry.getKey(), onlyEntry.getValue());
738        default:
739          return fromEntries(comparator, false, entries, size);
740      }
741    }
742
743    /**
744     * Throws UnsupportedOperationException. A future version may support this operation. Then the
745     * value for any given key will be the one that was last supplied in a {@code put} operation for
746     * that key.
747     *
748     * @throws UnsupportedOperationException always
749     * @since 31.1
750     * @deprecated This method is not currently implemented, and may never be.
751     */
752    @DoNotCall
753    @Deprecated
754    @Override
755    public final ImmutableSortedMap<K, V> buildKeepingLast() {
756      // TODO(emcmanus): implement
757      throw new UnsupportedOperationException(
758          "ImmutableSortedMap.Builder does not yet implement buildKeepingLast()");
759    }
760  }
761
762  private final transient RegularImmutableSortedSet<K> keySet;
763  private final transient ImmutableList<V> valueList;
764  @CheckForNull private transient ImmutableSortedMap<K, V> descendingMap;
765
766  ImmutableSortedMap(RegularImmutableSortedSet<K> keySet, ImmutableList<V> valueList) {
767    this(keySet, valueList, null);
768  }
769
770  ImmutableSortedMap(
771      RegularImmutableSortedSet<K> keySet,
772      ImmutableList<V> valueList,
773      @CheckForNull ImmutableSortedMap<K, V> descendingMap) {
774    this.keySet = keySet;
775    this.valueList = valueList;
776    this.descendingMap = descendingMap;
777  }
778
779  @Override
780  public int size() {
781    return valueList.size();
782  }
783
784  @Override
785  public void forEach(BiConsumer<? super K, ? super V> action) {
786    checkNotNull(action);
787    ImmutableList<K> keyList = keySet.asList();
788    for (int i = 0; i < size(); i++) {
789      action.accept(keyList.get(i), valueList.get(i));
790    }
791  }
792
793  @Override
794  @CheckForNull
795  public V get(@CheckForNull Object key) {
796    int index = keySet.indexOf(key);
797    return (index == -1) ? null : valueList.get(index);
798  }
799
800  @Override
801  boolean isPartialView() {
802    return keySet.isPartialView() || valueList.isPartialView();
803  }
804
805  /** Returns an immutable set of the mappings in this map, sorted by the key ordering. */
806  @Override
807  public ImmutableSet<Entry<K, V>> entrySet() {
808    return super.entrySet();
809  }
810
811  @Override
812  ImmutableSet<Entry<K, V>> createEntrySet() {
813    class EntrySet extends ImmutableMapEntrySet<K, V> {
814      @Override
815      public UnmodifiableIterator<Entry<K, V>> iterator() {
816        return asList().iterator();
817      }
818
819      @Override
820      public Spliterator<Entry<K, V>> spliterator() {
821        return asList().spliterator();
822      }
823
824      @Override
825      public void forEach(Consumer<? super Entry<K, V>> action) {
826        asList().forEach(action);
827      }
828
829      @Override
830      ImmutableList<Entry<K, V>> createAsList() {
831        return new ImmutableAsList<Entry<K, V>>() {
832          @Override
833          public Entry<K, V> get(int index) {
834            return new AbstractMap.SimpleImmutableEntry<>(
835                keySet.asList().get(index), valueList.get(index));
836          }
837
838          @Override
839          public Spliterator<Entry<K, V>> spliterator() {
840            return CollectSpliterators.indexed(
841                size(), ImmutableSet.SPLITERATOR_CHARACTERISTICS, this::get);
842          }
843
844          @Override
845          ImmutableCollection<Entry<K, V>> delegateCollection() {
846            return EntrySet.this;
847          }
848
849          // redeclare to help optimizers with b/310253115
850          @SuppressWarnings("RedundantOverride")
851          @Override
852          @J2ktIncompatible // serialization
853          @GwtIncompatible // serialization
854          Object writeReplace() {
855            return super.writeReplace();
856          }
857        };
858      }
859
860      @Override
861      ImmutableMap<K, V> map() {
862        return ImmutableSortedMap.this;
863      }
864
865      // redeclare to help optimizers with b/310253115
866      @SuppressWarnings("RedundantOverride")
867      @Override
868      @J2ktIncompatible // serialization
869      @GwtIncompatible // serialization
870      Object writeReplace() {
871        return super.writeReplace();
872      }
873    }
874    return isEmpty() ? ImmutableSet.<Entry<K, V>>of() : new EntrySet();
875  }
876
877  /** Returns an immutable sorted set of the keys in this map. */
878  @Override
879  public ImmutableSortedSet<K> keySet() {
880    return keySet;
881  }
882
883  @Override
884  ImmutableSet<K> createKeySet() {
885    throw new AssertionError("should never be called");
886  }
887
888  /**
889   * Returns an immutable collection of the values in this map, sorted by the ordering of the
890   * corresponding keys.
891   */
892  @Override
893  public ImmutableCollection<V> values() {
894    return valueList;
895  }
896
897  @Override
898  ImmutableCollection<V> createValues() {
899    throw new AssertionError("should never be called");
900  }
901
902  /**
903   * Returns the comparator that orders the keys, which is {@link Ordering#natural()} when the
904   * natural ordering of the keys is used. Note that its behavior is not consistent with {@link
905   * TreeMap#comparator()}, which returns {@code null} to indicate natural ordering.
906   */
907  @Override
908  public Comparator<? super K> comparator() {
909    return keySet().comparator();
910  }
911
912  @Override
913  public K firstKey() {
914    return keySet().first();
915  }
916
917  @Override
918  public K lastKey() {
919    return keySet().last();
920  }
921
922  private ImmutableSortedMap<K, V> getSubMap(int fromIndex, int toIndex) {
923    if (fromIndex == 0 && toIndex == size()) {
924      return this;
925    } else if (fromIndex == toIndex) {
926      return emptyMap(comparator());
927    } else {
928      return new ImmutableSortedMap<>(
929          keySet.getSubSet(fromIndex, toIndex), valueList.subList(fromIndex, toIndex));
930    }
931  }
932
933  /**
934   * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys are less
935   * than {@code toKey}.
936   *
937   * <p>The {@link SortedMap#headMap} documentation states that a submap of a submap throws an
938   * {@link IllegalArgumentException} if passed a {@code toKey} greater than an earlier {@code
939   * toKey}. However, this method doesn't throw an exception in that situation, but instead keeps
940   * the original {@code toKey}.
941   */
942  @Override
943  public ImmutableSortedMap<K, V> headMap(K toKey) {
944    return headMap(toKey, false);
945  }
946
947  /**
948   * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys are less
949   * than (or equal to, if {@code inclusive}) {@code toKey}.
950   *
951   * <p>The {@link SortedMap#headMap} documentation states that a submap of a submap throws an
952   * {@link IllegalArgumentException} if passed a {@code toKey} greater than an earlier {@code
953   * toKey}. However, this method doesn't throw an exception in that situation, but instead keeps
954   * the original {@code toKey}.
955   *
956   * @since 12.0
957   */
958  @Override
959  public ImmutableSortedMap<K, V> headMap(K toKey, boolean inclusive) {
960    return getSubMap(0, keySet.headIndex(checkNotNull(toKey), inclusive));
961  }
962
963  /**
964   * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys ranges
965   * from {@code fromKey}, inclusive, to {@code toKey}, exclusive.
966   *
967   * <p>The {@link SortedMap#subMap} documentation states that a submap of a submap throws an {@link
968   * IllegalArgumentException} if passed a {@code fromKey} less than an earlier {@code fromKey}.
969   * However, this method doesn't throw an exception in that situation, but instead keeps the
970   * original {@code fromKey}. Similarly, this method keeps the original {@code toKey}, instead of
971   * throwing an exception, if passed a {@code toKey} greater than an earlier {@code toKey}.
972   */
973  @Override
974  public ImmutableSortedMap<K, V> subMap(K fromKey, K toKey) {
975    return subMap(fromKey, true, toKey, false);
976  }
977
978  /**
979   * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys ranges
980   * from {@code fromKey} to {@code toKey}, inclusive or exclusive as indicated by the boolean
981   * flags.
982   *
983   * <p>The {@link SortedMap#subMap} documentation states that a submap of a submap throws an {@link
984   * IllegalArgumentException} if passed a {@code fromKey} less than an earlier {@code fromKey}.
985   * However, this method doesn't throw an exception in that situation, but instead keeps the
986   * original {@code fromKey}. Similarly, this method keeps the original {@code toKey}, instead of
987   * throwing an exception, if passed a {@code toKey} greater than an earlier {@code toKey}.
988   *
989   * @since 12.0
990   */
991  @Override
992  public ImmutableSortedMap<K, V> subMap(
993      K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
994    checkNotNull(fromKey);
995    checkNotNull(toKey);
996    checkArgument(
997        comparator().compare(fromKey, toKey) <= 0,
998        "expected fromKey <= toKey but %s > %s",
999        fromKey,
1000        toKey);
1001    return headMap(toKey, toInclusive).tailMap(fromKey, fromInclusive);
1002  }
1003
1004  /**
1005   * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys are
1006   * greater than or equals to {@code fromKey}.
1007   *
1008   * <p>The {@link SortedMap#tailMap} documentation states that a submap of a submap throws an
1009   * {@link IllegalArgumentException} if passed a {@code fromKey} less than an earlier {@code
1010   * fromKey}. However, this method doesn't throw an exception in that situation, but instead keeps
1011   * the original {@code fromKey}.
1012   */
1013  @Override
1014  public ImmutableSortedMap<K, V> tailMap(K fromKey) {
1015    return tailMap(fromKey, true);
1016  }
1017
1018  /**
1019   * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys are
1020   * greater than (or equal to, if {@code inclusive}) {@code fromKey}.
1021   *
1022   * <p>The {@link SortedMap#tailMap} documentation states that a submap of a submap throws an
1023   * {@link IllegalArgumentException} if passed a {@code fromKey} less than an earlier {@code
1024   * fromKey}. However, this method doesn't throw an exception in that situation, but instead keeps
1025   * the original {@code fromKey}.
1026   *
1027   * @since 12.0
1028   */
1029  @Override
1030  public ImmutableSortedMap<K, V> tailMap(K fromKey, boolean inclusive) {
1031    return getSubMap(keySet.tailIndex(checkNotNull(fromKey), inclusive), size());
1032  }
1033
1034  @Override
1035  @CheckForNull
1036  public Entry<K, V> lowerEntry(K key) {
1037    return headMap(key, false).lastEntry();
1038  }
1039
1040  @Override
1041  @CheckForNull
1042  public K lowerKey(K key) {
1043    return keyOrNull(lowerEntry(key));
1044  }
1045
1046  @Override
1047  @CheckForNull
1048  public Entry<K, V> floorEntry(K key) {
1049    return headMap(key, true).lastEntry();
1050  }
1051
1052  @Override
1053  @CheckForNull
1054  public K floorKey(K key) {
1055    return keyOrNull(floorEntry(key));
1056  }
1057
1058  @Override
1059  @CheckForNull
1060  public Entry<K, V> ceilingEntry(K key) {
1061    return tailMap(key, true).firstEntry();
1062  }
1063
1064  @Override
1065  @CheckForNull
1066  public K ceilingKey(K key) {
1067    return keyOrNull(ceilingEntry(key));
1068  }
1069
1070  @Override
1071  @CheckForNull
1072  public Entry<K, V> higherEntry(K key) {
1073    return tailMap(key, false).firstEntry();
1074  }
1075
1076  @Override
1077  @CheckForNull
1078  public K higherKey(K key) {
1079    return keyOrNull(higherEntry(key));
1080  }
1081
1082  @Override
1083  @CheckForNull
1084  public Entry<K, V> firstEntry() {
1085    return isEmpty() ? null : entrySet().asList().get(0);
1086  }
1087
1088  @Override
1089  @CheckForNull
1090  public Entry<K, V> lastEntry() {
1091    return isEmpty() ? null : entrySet().asList().get(size() - 1);
1092  }
1093
1094  /**
1095   * Guaranteed to throw an exception and leave the map unmodified.
1096   *
1097   * @throws UnsupportedOperationException always
1098   * @deprecated Unsupported operation.
1099   */
1100  @CanIgnoreReturnValue
1101  @Deprecated
1102  @Override
1103  @DoNotCall("Always throws UnsupportedOperationException")
1104  @CheckForNull
1105  public final Entry<K, V> pollFirstEntry() {
1106    throw new UnsupportedOperationException();
1107  }
1108
1109  /**
1110   * Guaranteed to throw an exception and leave the map unmodified.
1111   *
1112   * @throws UnsupportedOperationException always
1113   * @deprecated Unsupported operation.
1114   */
1115  @CanIgnoreReturnValue
1116  @Deprecated
1117  @Override
1118  @DoNotCall("Always throws UnsupportedOperationException")
1119  @CheckForNull
1120  public final Entry<K, V> pollLastEntry() {
1121    throw new UnsupportedOperationException();
1122  }
1123
1124  @Override
1125  public ImmutableSortedMap<K, V> descendingMap() {
1126    // TODO(kevinb): The descendingMap is never actually cached at all. Either:
1127    //
1128    // - Cache it, and annotate the field with @LazyInit.
1129    // - Simplify the code below, and consider eliminating the field (b/287198172), which is also
1130    //   set by one of the constructors.
1131    ImmutableSortedMap<K, V> result = descendingMap;
1132    if (result == null) {
1133      if (isEmpty()) {
1134        return emptyMap(Ordering.from(comparator()).reverse());
1135      } else {
1136        return new ImmutableSortedMap<>(
1137            (RegularImmutableSortedSet<K>) keySet.descendingSet(), valueList.reverse(), this);
1138      }
1139    }
1140    return result;
1141  }
1142
1143  @Override
1144  public ImmutableSortedSet<K> navigableKeySet() {
1145    return keySet;
1146  }
1147
1148  @Override
1149  public ImmutableSortedSet<K> descendingKeySet() {
1150    return keySet.descendingSet();
1151  }
1152
1153  /**
1154   * Serialized type for all ImmutableSortedMap instances. It captures the logical contents and they
1155   * are reconstructed using public factory methods. This ensures that the implementation types
1156   * remain as implementation details.
1157   */
1158  @J2ktIncompatible // serialization
1159  private static class SerializedForm<K, V> extends ImmutableMap.SerializedForm<K, V> {
1160    private final Comparator<? super K> comparator;
1161
1162    SerializedForm(ImmutableSortedMap<K, V> sortedMap) {
1163      super(sortedMap);
1164      comparator = sortedMap.comparator();
1165    }
1166
1167    @Override
1168    Builder<K, V> makeBuilder(int size) {
1169      return new Builder<>(comparator);
1170    }
1171
1172    private static final long serialVersionUID = 0;
1173  }
1174
1175  @Override
1176  @J2ktIncompatible // serialization
1177  Object writeReplace() {
1178    return new SerializedForm<>(this);
1179  }
1180
1181  @J2ktIncompatible // java.io.ObjectInputStream
1182  private void readObject(ObjectInputStream stream) throws InvalidObjectException {
1183    throw new InvalidObjectException("Use SerializedForm");
1184  }
1185
1186  // This class is never actually serialized directly, but we have to make the
1187  // warning go away (and suppressing would suppress for all nested classes too)
1188  private static final long serialVersionUID = 0;
1189
1190  /**
1191   * Not supported. Use {@link #toImmutableSortedMap}, which offers better type-safety, instead.
1192   * This method exists only to hide {@link ImmutableMap#toImmutableMap} from consumers of {@code
1193   * ImmutableSortedMap}.
1194   *
1195   * @throws UnsupportedOperationException always
1196   * @deprecated Use {@link ImmutableSortedMap#toImmutableSortedMap}.
1197   */
1198  @DoNotCall("Use toImmutableSortedMap")
1199  @Deprecated
1200  public static <T extends @Nullable Object, K, V>
1201      Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
1202          Function<? super T, ? extends K> keyFunction,
1203          Function<? super T, ? extends V> valueFunction) {
1204    throw new UnsupportedOperationException();
1205  }
1206
1207  /**
1208   * Not supported. Use {@link #toImmutableSortedMap}, which offers better type-safety, instead.
1209   * This method exists only to hide {@link ImmutableMap#toImmutableMap} from consumers of {@code
1210   * ImmutableSortedMap}.
1211   *
1212   * @throws UnsupportedOperationException always
1213   * @deprecated Use {@link ImmutableSortedMap#toImmutableSortedMap}.
1214   */
1215  @DoNotCall("Use toImmutableSortedMap")
1216  @Deprecated
1217  public static <T extends @Nullable Object, K, V>
1218      Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
1219          Function<? super T, ? extends K> keyFunction,
1220          Function<? super T, ? extends V> valueFunction,
1221          BinaryOperator<V> mergeFunction) {
1222    throw new UnsupportedOperationException();
1223  }
1224
1225  /**
1226   * Not supported. Use {@link #naturalOrder}, which offers better type-safety, instead. This method
1227   * exists only to hide {@link ImmutableMap#builder} from consumers of {@code ImmutableSortedMap}.
1228   *
1229   * @throws UnsupportedOperationException always
1230   * @deprecated Use {@link ImmutableSortedMap#naturalOrder}, which offers better type-safety.
1231   */
1232  @DoNotCall("Use naturalOrder")
1233  @Deprecated
1234  public static <K, V> ImmutableSortedMap.Builder<K, V> builder() {
1235    throw new UnsupportedOperationException();
1236  }
1237
1238  /**
1239   * Not supported for ImmutableSortedMap.
1240   *
1241   * @throws UnsupportedOperationException always
1242   * @deprecated Not supported for ImmutableSortedMap.
1243   */
1244  @DoNotCall("Use naturalOrder (which does not accept an expected size)")
1245  @Deprecated
1246  public static <K, V> ImmutableSortedMap.Builder<K, V> builderWithExpectedSize(int expectedSize) {
1247    throw new UnsupportedOperationException();
1248  }
1249
1250  /**
1251   * Not supported. <b>You are attempting to create a map that may contain a non-{@code Comparable}
1252   * key.</b> Proper calls will resolve to the version in {@code ImmutableSortedMap}, not this dummy
1253   * version.
1254   *
1255   * @throws UnsupportedOperationException always
1256   * @deprecated <b>Pass a key of type {@code Comparable} to use {@link
1257   *     ImmutableSortedMap#of(Comparable, Object)}.</b>
1258   */
1259  @DoNotCall("Pass a key of type Comparable")
1260  @Deprecated
1261  public static <K, V> ImmutableSortedMap<K, V> of(K k1, V v1) {
1262    throw new UnsupportedOperationException();
1263  }
1264
1265  /**
1266   * Not supported. <b>You are attempting to create a map that may contain non-{@code Comparable}
1267   * keys.</b> Proper calls will resolve to the version in {@code ImmutableSortedMap}, not this
1268   * dummy version.
1269   *
1270   * @throws UnsupportedOperationException always
1271   * @deprecated <b>Pass keys of type {@code Comparable} to use {@link
1272   *     ImmutableSortedMap#of(Comparable, Object, Comparable, Object)}.</b>
1273   */
1274  @DoNotCall("Pass keys of type Comparable")
1275  @Deprecated
1276  public static <K, V> ImmutableSortedMap<K, V> of(K k1, V v1, K k2, V v2) {
1277    throw new UnsupportedOperationException();
1278  }
1279
1280  /**
1281   * Not supported. <b>You are attempting to create a map that may contain non-{@code Comparable}
1282   * keys.</b> Proper calls to will resolve to the version in {@code ImmutableSortedMap}, not this
1283   * dummy version.
1284   *
1285   * @throws UnsupportedOperationException always
1286   * @deprecated <b>Pass keys of type {@code Comparable} to use {@link
1287   *     ImmutableSortedMap#of(Comparable, Object, Comparable, Object, Comparable, Object)}.</b>
1288   */
1289  @DoNotCall("Pass keys of type Comparable")
1290  @Deprecated
1291  public static <K, V> ImmutableSortedMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
1292    throw new UnsupportedOperationException();
1293  }
1294
1295  /**
1296   * Not supported. <b>You are attempting to create a map that may contain non-{@code Comparable}
1297   * keys.</b> Proper calls will resolve to the version in {@code ImmutableSortedMap}, not this
1298   * dummy version.
1299   *
1300   * @throws UnsupportedOperationException always
1301   * @deprecated <b>Pass keys of type {@code Comparable} to use {@link
1302   *     ImmutableSortedMap#of(Comparable, Object, Comparable, Object, Comparable, Object,
1303   *     Comparable, Object)}.</b>
1304   */
1305  @DoNotCall("Pass keys of type Comparable")
1306  @Deprecated
1307  public static <K, V> ImmutableSortedMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
1308    throw new UnsupportedOperationException();
1309  }
1310
1311  /**
1312   * Not supported. <b>You are attempting to create a map that may contain non-{@code Comparable}
1313   * keys.</b> Proper calls will resolve to the version in {@code ImmutableSortedMap}, not this
1314   * dummy version.
1315   *
1316   * @throws UnsupportedOperationException always
1317   * @deprecated <b>Pass keys of type {@code Comparable} to use {@link
1318   *     ImmutableSortedMap#of(Comparable, Object, Comparable, Object, Comparable, Object,
1319   *     Comparable, Object, Comparable, Object)}.</b>
1320   */
1321  @DoNotCall("Pass keys of type Comparable")
1322  @Deprecated
1323  public static <K, V> ImmutableSortedMap<K, V> of(
1324      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
1325    throw new UnsupportedOperationException();
1326  }
1327
1328  /**
1329   * Not supported. <b>You are attempting to create a map that may contain non-{@code Comparable}
1330   * keys.</b> Proper calls will resolve to the version in {@code ImmutableSortedMap}, not this
1331   * dummy version.
1332   *
1333   * @throws UnsupportedOperationException always
1334   * @deprecated <b>Pass keys of type {@code Comparable} to use {@link
1335   *     ImmutableSortedMap#of(Comparable, Object, Comparable, Object, Comparable, Object,
1336   *     Comparable, Object, Comparable, Object)}.</b>
1337   */
1338  @DoNotCall("Pass keys of type Comparable")
1339  @Deprecated
1340  public static <K, V> ImmutableSortedMap<K, V> of(
1341      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
1342    throw new UnsupportedOperationException();
1343  }
1344
1345  /**
1346   * Not supported. <b>You are attempting to create a map that may contain non-{@code Comparable}
1347   * keys.</b> Proper calls will resolve to the version in {@code ImmutableSortedMap}, not this
1348   * dummy version.
1349   *
1350   * @throws UnsupportedOperationException always
1351   * @deprecated <b>Pass keys of type {@code Comparable} to use {@link
1352   *     ImmutableSortedMap#of(Comparable, Object, Comparable, Object, Comparable, Object,
1353   *     Comparable, Object, Comparable, Object)}.</b>
1354   */
1355  @DoNotCall("Pass keys of type Comparable")
1356  @Deprecated
1357  public static <K, V> ImmutableSortedMap<K, V> of(
1358      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) {
1359    throw new UnsupportedOperationException();
1360  }
1361
1362  /**
1363   * Not supported. <b>You are attempting to create a map that may contain non-{@code Comparable}
1364   * keys.</b> Proper calls will resolve to the version in {@code ImmutableSortedMap}, not this
1365   * dummy version.
1366   *
1367   * @throws UnsupportedOperationException always
1368   * @deprecated <b>Pass keys of type {@code Comparable} to use {@link
1369   *     ImmutableSortedMap#of(Comparable, Object, Comparable, Object, Comparable, Object,
1370   *     Comparable, Object, Comparable, Object)}.</b>
1371   */
1372  @DoNotCall("Pass keys of type Comparable")
1373  @Deprecated
1374  public static <K, V> ImmutableSortedMap<K, V> of(
1375      K k1,
1376      V v1,
1377      K k2,
1378      V v2,
1379      K k3,
1380      V v3,
1381      K k4,
1382      V v4,
1383      K k5,
1384      V v5,
1385      K k6,
1386      V v6,
1387      K k7,
1388      V v7,
1389      K k8,
1390      V v8) {
1391    throw new UnsupportedOperationException();
1392  }
1393
1394  /**
1395   * Not supported. <b>You are attempting to create a map that may contain non-{@code Comparable}
1396   * keys.</b> Proper calls will resolve to the version in {@code ImmutableSortedMap}, not this
1397   * dummy version.
1398   *
1399   * @throws UnsupportedOperationException always
1400   * @deprecated <b>Pass keys of type {@code Comparable} to use {@link
1401   *     ImmutableSortedMap#of(Comparable, Object, Comparable, Object, Comparable, Object,
1402   *     Comparable, Object, Comparable, Object)}.</b>
1403   */
1404  @DoNotCall("Pass keys of type Comparable")
1405  @Deprecated
1406  public static <K, V> ImmutableSortedMap<K, V> of(
1407      K k1,
1408      V v1,
1409      K k2,
1410      V v2,
1411      K k3,
1412      V v3,
1413      K k4,
1414      V v4,
1415      K k5,
1416      V v5,
1417      K k6,
1418      V v6,
1419      K k7,
1420      V v7,
1421      K k8,
1422      V v8,
1423      K k9,
1424      V v9) {
1425    throw new UnsupportedOperationException();
1426  }
1427
1428  /**
1429   * Not supported. <b>You are attempting to create a map that may contain non-{@code Comparable}
1430   * keys.</b> Proper calls will resolve to the version in {@code ImmutableSortedMap}, not this
1431   * dummy version.
1432   *
1433   * @throws UnsupportedOperationException always
1434   * @deprecated <b>Pass keys of type {@code Comparable} to use {@link
1435   *     ImmutableSortedMap#of(Comparable, Object, Comparable, Object, Comparable, Object,
1436   *     Comparable, Object, Comparable, Object)}.</b>
1437   */
1438  @DoNotCall("Pass keys of type Comparable")
1439  @Deprecated
1440  public static <K, V> ImmutableSortedMap<K, V> of(
1441      K k1,
1442      V v1,
1443      K k2,
1444      V v2,
1445      K k3,
1446      V v3,
1447      K k4,
1448      V v4,
1449      K k5,
1450      V v5,
1451      K k6,
1452      V v6,
1453      K k7,
1454      V v7,
1455      K k8,
1456      V v8,
1457      K k9,
1458      V v9,
1459      K k10,
1460      V v10) {
1461    throw new UnsupportedOperationException();
1462  }
1463
1464  /**
1465   * Not supported. Use {@code ImmutableSortedMap.copyOf(ImmutableMap.ofEntries(...))}.
1466   *
1467   * @deprecated Use {@code ImmutableSortedMap.copyOf(ImmutableMap.ofEntries(...))}.
1468   */
1469  @DoNotCall("ImmutableSortedMap.ofEntries not currently available; use ImmutableSortedMap.copyOf")
1470  @Deprecated
1471  public static <K, V> ImmutableSortedMap<K, V> ofEntries(
1472      Entry<? extends K, ? extends V>... entries) {
1473    throw new UnsupportedOperationException();
1474  }
1475}