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