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