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