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