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