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