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