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