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