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