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