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