001/* 002 * Copyright (C) 2008 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.checkNotNull; 020import static com.google.common.base.Preconditions.checkState; 021import static com.google.common.collect.CollectPreconditions.checkEntryNotNull; 022import static com.google.common.collect.CollectPreconditions.checkNonnegative; 023import static java.util.Objects.requireNonNull; 024 025import com.google.common.annotations.Beta; 026import com.google.common.annotations.GwtCompatible; 027import com.google.errorprone.annotations.CanIgnoreReturnValue; 028import com.google.errorprone.annotations.DoNotCall; 029import com.google.errorprone.annotations.DoNotMock; 030import com.google.errorprone.annotations.concurrent.LazyInit; 031import com.google.j2objc.annotations.RetainedWith; 032import com.google.j2objc.annotations.WeakOuter; 033import java.io.Serializable; 034import java.util.AbstractMap; 035import java.util.Arrays; 036import java.util.BitSet; 037import java.util.Collection; 038import java.util.Collections; 039import java.util.Comparator; 040import java.util.HashSet; 041import java.util.Iterator; 042import java.util.Map; 043import java.util.Map.Entry; 044import java.util.Set; 045import java.util.SortedMap; 046import javax.annotation.CheckForNull; 047import org.checkerframework.checker.nullness.qual.Nullable; 048 049/** 050 * A {@link Map} whose contents will never change, with many other important properties detailed at 051 * {@link ImmutableCollection}. 052 * 053 * <p>See the Guava User Guide article on <a href= 054 * "https://github.com/google/guava/wiki/ImmutableCollectionsExplained">immutable collections</a>. 055 * 056 * @author Jesse Wilson 057 * @author Kevin Bourrillion 058 * @since 2.0 059 */ 060@DoNotMock("Use ImmutableMap.of or another implementation") 061@GwtCompatible(serializable = true, emulated = true) 062@SuppressWarnings("serial") // we're overriding default serialization 063@ElementTypesAreNonnullByDefault 064public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable { 065 066 /** 067 * Returns the empty map. This map behaves and performs comparably to {@link 068 * Collections#emptyMap}, and is preferable mainly for consistency and maintainability of your 069 * code. 070 * 071 * <p><b>Performance note:</b> the instance returned is a singleton. 072 */ 073 @SuppressWarnings("unchecked") 074 public static <K, V> ImmutableMap<K, V> of() { 075 return (ImmutableMap<K, V>) RegularImmutableMap.EMPTY; 076 } 077 078 /** 079 * Returns an immutable map containing a single entry. This map behaves and performs comparably to 080 * {@link Collections#singletonMap} but will not accept a null key or value. It is preferable 081 * mainly for consistency and maintainability of your code. 082 */ 083 public static <K, V> ImmutableMap<K, V> of(K k1, V v1) { 084 checkEntryNotNull(k1, v1); 085 return RegularImmutableMap.create(1, new Object[] {k1, v1}); 086 } 087 088 /** 089 * Returns an immutable map containing the given entries, in order. 090 * 091 * @throws IllegalArgumentException if duplicate keys are provided 092 */ 093 public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2) { 094 checkEntryNotNull(k1, v1); 095 checkEntryNotNull(k2, v2); 096 return RegularImmutableMap.create(2, new Object[] {k1, v1, k2, v2}); 097 } 098 099 /** 100 * Returns an immutable map containing the given entries, in order. 101 * 102 * @throws IllegalArgumentException if duplicate keys are provided 103 */ 104 public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) { 105 checkEntryNotNull(k1, v1); 106 checkEntryNotNull(k2, v2); 107 checkEntryNotNull(k3, v3); 108 return RegularImmutableMap.create(3, new Object[] {k1, v1, k2, v2, k3, v3}); 109 } 110 111 /** 112 * Returns an immutable map containing the given entries, in order. 113 * 114 * @throws IllegalArgumentException if duplicate keys are provided 115 */ 116 public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { 117 checkEntryNotNull(k1, v1); 118 checkEntryNotNull(k2, v2); 119 checkEntryNotNull(k3, v3); 120 checkEntryNotNull(k4, v4); 121 return RegularImmutableMap.create(4, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4}); 122 } 123 124 /** 125 * Returns an immutable map containing the given entries, in order. 126 * 127 * @throws IllegalArgumentException if duplicate keys are provided 128 */ 129 public static <K, V> ImmutableMap<K, V> of( 130 K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { 131 checkEntryNotNull(k1, v1); 132 checkEntryNotNull(k2, v2); 133 checkEntryNotNull(k3, v3); 134 checkEntryNotNull(k4, v4); 135 checkEntryNotNull(k5, v5); 136 return RegularImmutableMap.create(5, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4, k5, v5}); 137 } 138 139 /** 140 * Returns an immutable map containing the given entries, in order. 141 * 142 * @throws IllegalArgumentException if duplicate keys are provided 143 * @since 31.0 144 */ 145 public static <K, V> ImmutableMap<K, V> of( 146 K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) { 147 checkEntryNotNull(k1, v1); 148 checkEntryNotNull(k2, v2); 149 checkEntryNotNull(k3, v3); 150 checkEntryNotNull(k4, v4); 151 checkEntryNotNull(k5, v5); 152 checkEntryNotNull(k6, v6); 153 return RegularImmutableMap.create( 154 6, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6}); 155 } 156 /** 157 * Returns an immutable map containing the given entries, in order. 158 * 159 * @throws IllegalArgumentException if duplicate keys are provided 160 * @since 31.0 161 */ 162 public static <K, V> ImmutableMap<K, V> of( 163 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) { 164 checkEntryNotNull(k1, v1); 165 checkEntryNotNull(k2, v2); 166 checkEntryNotNull(k3, v3); 167 checkEntryNotNull(k4, v4); 168 checkEntryNotNull(k5, v5); 169 checkEntryNotNull(k6, v6); 170 checkEntryNotNull(k7, v7); 171 return RegularImmutableMap.create( 172 7, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7}); 173 } 174 /** 175 * Returns an immutable map containing the given entries, in order. 176 * 177 * @throws IllegalArgumentException if duplicate keys are provided 178 * @since 31.0 179 */ 180 public static <K, V> ImmutableMap<K, V> of( 181 K k1, 182 V v1, 183 K k2, 184 V v2, 185 K k3, 186 V v3, 187 K k4, 188 V v4, 189 K k5, 190 V v5, 191 K k6, 192 V v6, 193 K k7, 194 V v7, 195 K k8, 196 V v8) { 197 checkEntryNotNull(k1, v1); 198 checkEntryNotNull(k2, v2); 199 checkEntryNotNull(k3, v3); 200 checkEntryNotNull(k4, v4); 201 checkEntryNotNull(k5, v5); 202 checkEntryNotNull(k6, v6); 203 checkEntryNotNull(k7, v7); 204 checkEntryNotNull(k8, v8); 205 return RegularImmutableMap.create( 206 8, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8}); 207 } 208 /** 209 * Returns an immutable map containing the given entries, in order. 210 * 211 * @throws IllegalArgumentException if duplicate keys are provided 212 * @since 31.0 213 */ 214 public static <K, V> ImmutableMap<K, V> of( 215 K k1, 216 V v1, 217 K k2, 218 V v2, 219 K k3, 220 V v3, 221 K k4, 222 V v4, 223 K k5, 224 V v5, 225 K k6, 226 V v6, 227 K k7, 228 V v7, 229 K k8, 230 V v8, 231 K k9, 232 V v9) { 233 checkEntryNotNull(k1, v1); 234 checkEntryNotNull(k2, v2); 235 checkEntryNotNull(k3, v3); 236 checkEntryNotNull(k4, v4); 237 checkEntryNotNull(k5, v5); 238 checkEntryNotNull(k6, v6); 239 checkEntryNotNull(k7, v7); 240 checkEntryNotNull(k8, v8); 241 checkEntryNotNull(k9, v9); 242 return RegularImmutableMap.create( 243 9, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9}); 244 } 245 /** 246 * Returns an immutable map containing the given entries, in order. 247 * 248 * @throws IllegalArgumentException if duplicate keys are provided 249 * @since 31.0 250 */ 251 public static <K, V> ImmutableMap<K, V> of( 252 K k1, 253 V v1, 254 K k2, 255 V v2, 256 K k3, 257 V v3, 258 K k4, 259 V v4, 260 K k5, 261 V v5, 262 K k6, 263 V v6, 264 K k7, 265 V v7, 266 K k8, 267 V v8, 268 K k9, 269 V v9, 270 K k10, 271 V v10) { 272 checkEntryNotNull(k1, v1); 273 checkEntryNotNull(k2, v2); 274 checkEntryNotNull(k3, v3); 275 checkEntryNotNull(k4, v4); 276 checkEntryNotNull(k5, v5); 277 checkEntryNotNull(k6, v6); 278 checkEntryNotNull(k7, v7); 279 checkEntryNotNull(k8, v8); 280 checkEntryNotNull(k9, v9); 281 checkEntryNotNull(k10, v10); 282 return RegularImmutableMap.create( 283 10, 284 new Object[] { 285 k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10 286 }); 287 } 288 289 // looking for of() with > 10 entries? Use the builder or ofEntries instead. 290 291 /** 292 * Returns an immutable map containing the given entries, in order. 293 * 294 * @throws IllegalArgumentException if duplicate keys are provided 295 * @since 31.0 296 */ 297 @SafeVarargs 298 public static <K, V> ImmutableMap<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) { 299 @SuppressWarnings("unchecked") // we will only ever read these 300 Entry<K, V>[] entries2 = (Entry<K, V>[]) entries; 301 return copyOf(Arrays.asList(entries2)); 302 } 303 304 /** 305 * Verifies that {@code key} and {@code value} are non-null, and returns a new immutable entry 306 * with those values. 307 * 308 * <p>A call to {@link Entry#setValue} on the returned entry will always throw {@link 309 * UnsupportedOperationException}. 310 */ 311 static <K, V> Entry<K, V> entryOf(K key, V value) { 312 checkEntryNotNull(key, value); 313 return new AbstractMap.SimpleImmutableEntry<>(key, value); 314 } 315 316 /** 317 * Returns a new builder. The generated builder is equivalent to the builder created by the {@link 318 * Builder} constructor. 319 */ 320 public static <K, V> Builder<K, V> builder() { 321 return new Builder<>(); 322 } 323 324 /** 325 * Returns a new builder, expecting the specified number of entries to be added. 326 * 327 * <p>If {@code expectedSize} is exactly the number of entries added to the builder before {@link 328 * Builder#build} is called, the builder is likely to perform better than an unsized {@link 329 * #builder()} would have. 330 * 331 * <p>It is not specified if any performance benefits apply if {@code expectedSize} is close to, 332 * but not exactly, the number of entries added to the builder. 333 * 334 * @since 23.1 335 */ 336 @Beta 337 public static <K, V> Builder<K, V> builderWithExpectedSize(int expectedSize) { 338 checkNonnegative(expectedSize, "expectedSize"); 339 return new Builder<>(expectedSize); 340 } 341 342 static void checkNoConflict( 343 boolean safe, String conflictDescription, Object entry1, Object entry2) { 344 if (!safe) { 345 throw conflictException(conflictDescription, entry1, entry2); 346 } 347 } 348 349 static IllegalArgumentException conflictException( 350 String conflictDescription, Object entry1, Object entry2) { 351 return new IllegalArgumentException( 352 "Multiple entries with same " + conflictDescription + ": " + entry1 + " and " + entry2); 353 } 354 355 /** 356 * A builder for creating immutable map instances, especially {@code public static final} maps 357 * ("constant maps"). Example: 358 * 359 * <pre>{@code 360 * static final ImmutableMap<String, Integer> WORD_TO_INT = 361 * new ImmutableMap.Builder<String, Integer>() 362 * .put("one", 1) 363 * .put("two", 2) 364 * .put("three", 3) 365 * .buildOrThrow(); 366 * }</pre> 367 * 368 * <p>For <i>small</i> immutable maps, the {@code ImmutableMap.of()} methods are even more 369 * convenient. 370 * 371 * <p>By default, a {@code Builder} will generate maps that iterate over entries in the order they 372 * were inserted into the builder, equivalently to {@code LinkedHashMap}. For example, in the 373 * above example, {@code WORD_TO_INT.entrySet()} is guaranteed to iterate over the entries in the 374 * order {@code "one"=1, "two"=2, "three"=3}, and {@code keySet()} and {@code values()} respect 375 * the same order. If you want a different order, consider using {@link ImmutableSortedMap} to 376 * sort by keys, or call {@link #orderEntriesByValue(Comparator)}, which changes this builder to 377 * sort entries by value. 378 * 379 * <p>Builder instances can be reused - it is safe to call {@link #buildOrThrow} multiple times to 380 * build multiple maps in series. Each map is a superset of the maps created before it. 381 * 382 * @since 2.0 383 */ 384 @DoNotMock 385 public static class Builder<K, V> { 386 @CheckForNull Comparator<? super V> valueComparator; 387 @Nullable Object[] alternatingKeysAndValues; 388 int size; 389 boolean entriesUsed; 390 /** 391 * If non-null, a duplicate key we found in a previous buildKeepingLast() or buildOrThrow() 392 * call. A later buildOrThrow() can simply report this duplicate immediately. 393 */ 394 @Nullable DuplicateKey duplicateKey; 395 396 /** 397 * Creates a new builder. The returned builder is equivalent to the builder generated by {@link 398 * ImmutableMap#builder}. 399 */ 400 public Builder() { 401 this(ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY); 402 } 403 404 @SuppressWarnings({"unchecked", "rawtypes"}) 405 Builder(int initialCapacity) { 406 this.alternatingKeysAndValues = new @Nullable Object[2 * initialCapacity]; 407 this.size = 0; 408 this.entriesUsed = false; 409 } 410 411 private void ensureCapacity(int minCapacity) { 412 if (minCapacity * 2 > alternatingKeysAndValues.length) { 413 alternatingKeysAndValues = 414 Arrays.copyOf( 415 alternatingKeysAndValues, 416 ImmutableCollection.Builder.expandedCapacity( 417 alternatingKeysAndValues.length, minCapacity * 2)); 418 entriesUsed = false; 419 } 420 } 421 422 /** 423 * Associates {@code key} with {@code value} in the built map. If the same key is put more than 424 * once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep the last 425 * value put for that key. 426 */ 427 @CanIgnoreReturnValue 428 public Builder<K, V> put(K key, V value) { 429 ensureCapacity(size + 1); 430 checkEntryNotNull(key, value); 431 alternatingKeysAndValues[2 * size] = key; 432 alternatingKeysAndValues[2 * size + 1] = value; 433 size++; 434 return this; 435 } 436 437 /** 438 * Adds the given {@code entry} to the map, making it immutable if necessary. If the same key is 439 * put more than once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will 440 * keep the last value put for that key. 441 * 442 * @since 11.0 443 */ 444 @CanIgnoreReturnValue 445 public Builder<K, V> put(Entry<? extends K, ? extends V> entry) { 446 return put(entry.getKey(), entry.getValue()); 447 } 448 449 /** 450 * Associates all of the given map's keys and values in the built map. If the same key is put 451 * more than once, {@link #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep 452 * the last value put for that key. 453 * 454 * @throws NullPointerException if any key or value in {@code map} is null 455 */ 456 @CanIgnoreReturnValue 457 public Builder<K, V> putAll(Map<? extends K, ? extends V> map) { 458 return putAll(map.entrySet()); 459 } 460 461 /** 462 * Adds all of the given entries to the built map. If the same key is put more than once, {@link 463 * #buildOrThrow} will fail, while {@link #buildKeepingLast} will keep the last value put for 464 * that key. 465 * 466 * @throws NullPointerException if any key, value, or entry is null 467 * @since 19.0 468 */ 469 @CanIgnoreReturnValue 470 @Beta 471 public Builder<K, V> putAll(Iterable<? extends Entry<? extends K, ? extends V>> entries) { 472 if (entries instanceof Collection) { 473 ensureCapacity(size + ((Collection<?>) entries).size()); 474 } 475 for (Entry<? extends K, ? extends V> entry : entries) { 476 put(entry); 477 } 478 return this; 479 } 480 481 /** 482 * Configures this {@code Builder} to order entries by value according to the specified 483 * comparator. 484 * 485 * <p>The sort order is stable, that is, if two entries have values that compare as equivalent, 486 * the entry that was inserted first will be first in the built map's iteration order. 487 * 488 * @throws IllegalStateException if this method was already called 489 * @since 19.0 490 */ 491 @CanIgnoreReturnValue 492 @Beta 493 public Builder<K, V> orderEntriesByValue(Comparator<? super V> valueComparator) { 494 checkState(this.valueComparator == null, "valueComparator was already set"); 495 this.valueComparator = checkNotNull(valueComparator, "valueComparator"); 496 return this; 497 } 498 499 @CanIgnoreReturnValue 500 Builder<K, V> combine(Builder<K, V> other) { 501 checkNotNull(other); 502 ensureCapacity(this.size + other.size); 503 System.arraycopy( 504 other.alternatingKeysAndValues, 505 0, 506 this.alternatingKeysAndValues, 507 this.size * 2, 508 other.size * 2); 509 this.size += other.size; 510 return this; 511 } 512 513 private ImmutableMap<K, V> build(boolean throwIfDuplicateKeys) { 514 if (throwIfDuplicateKeys && duplicateKey != null) { 515 throw duplicateKey.exception(); 516 } 517 /* 518 * If entries is full, then this implementation may end up using the entries array 519 * directly and writing over the entry objects with non-terminal entries, but this is 520 * safe; if this Builder is used further, it will grow the entries array (so it can't 521 * affect the original array), and future build() calls will always copy any entry 522 * objects that cannot be safely reused. 523 */ 524 // localAlternatingKeysAndValues is an alias for the alternatingKeysAndValues field, except if 525 // we end up removing duplicates in a copy of the array. 526 @Nullable Object[] localAlternatingKeysAndValues; 527 int localSize = size; 528 if (valueComparator == null) { 529 localAlternatingKeysAndValues = alternatingKeysAndValues; 530 } else { 531 if (entriesUsed) { 532 alternatingKeysAndValues = Arrays.copyOf(alternatingKeysAndValues, 2 * size); 533 } 534 localAlternatingKeysAndValues = alternatingKeysAndValues; 535 if (!throwIfDuplicateKeys) { 536 // We want to retain only the last-put value for any given key, before sorting. 537 // This could be improved, but orderEntriesByValue is rather rarely used anyway. 538 localAlternatingKeysAndValues = lastEntryForEachKey(localAlternatingKeysAndValues, size); 539 if (localAlternatingKeysAndValues.length < alternatingKeysAndValues.length) { 540 localSize = localAlternatingKeysAndValues.length >>> 1; 541 } 542 } 543 sortEntries(localAlternatingKeysAndValues, localSize, valueComparator); 544 } 545 entriesUsed = true; 546 ImmutableMap<K, V> map = 547 RegularImmutableMap.create(localSize, localAlternatingKeysAndValues, this); 548 if (throwIfDuplicateKeys && duplicateKey != null) { 549 throw duplicateKey.exception(); 550 } 551 return map; 552 } 553 554 /** 555 * Returns a newly-created immutable map. The iteration order of the returned map is the order 556 * in which entries were inserted into the builder, unless {@link #orderEntriesByValue} was 557 * called, in which case entries are sorted by value. 558 * 559 * <p>Prefer the equivalent method {@link #buildOrThrow()} to make it explicit that the method 560 * will throw an exception if there are duplicate keys. The {@code build()} method will soon be 561 * deprecated. 562 * 563 * @throws IllegalArgumentException if duplicate keys were added 564 */ 565 public ImmutableMap<K, V> build() { 566 return buildOrThrow(); 567 } 568 569 /** 570 * Returns a newly-created immutable map, or throws an exception if any key was added more than 571 * once. The iteration order of the returned map is the order in which entries were inserted 572 * into the builder, unless {@link #orderEntriesByValue} was called, in which case entries are 573 * sorted by value. 574 * 575 * @throws IllegalArgumentException if duplicate keys were added 576 * @since 31.0 577 */ 578 public ImmutableMap<K, V> buildOrThrow() { 579 return build(true); 580 } 581 582 /** 583 * Returns a newly-created immutable map, using the last value for any key that was added more 584 * than once. The iteration order of the returned map is the order in which entries were 585 * inserted into the builder, unless {@link #orderEntriesByValue} was called, in which case 586 * entries are sorted by value. If a key was added more than once, it appears in iteration order 587 * based on the first time it was added, again unless {@link #orderEntriesByValue} was called. 588 * 589 * <p>In the current implementation, all values associated with a given key are stored in the 590 * {@code Builder} object, even though only one of them will be used in the built map. If there 591 * can be many repeated keys, it may be more space-efficient to use a {@link 592 * java.util.LinkedHashMap LinkedHashMap} and {@link ImmutableMap#copyOf(Map)} rather than 593 * {@code ImmutableMap.Builder}. 594 * 595 * @since 31.1 596 */ 597 public ImmutableMap<K, V> buildKeepingLast() { 598 return build(false); 599 } 600 601 static <V> void sortEntries( 602 @Nullable Object[] alternatingKeysAndValues, 603 int size, 604 Comparator<? super V> valueComparator) { 605 @SuppressWarnings({"rawtypes", "unchecked"}) 606 Entry<Object, V>[] entries = new Entry[size]; 607 for (int i = 0; i < size; i++) { 608 // requireNonNull is safe because the first `2*size` elements have been filled in. 609 Object key = requireNonNull(alternatingKeysAndValues[2 * i]); 610 @SuppressWarnings("unchecked") 611 V value = (V) requireNonNull(alternatingKeysAndValues[2 * i + 1]); 612 entries[i] = new AbstractMap.SimpleImmutableEntry<Object, V>(key, value); 613 } 614 Arrays.sort( 615 entries, 0, size, Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction())); 616 for (int i = 0; i < size; i++) { 617 alternatingKeysAndValues[2 * i] = entries[i].getKey(); 618 alternatingKeysAndValues[2 * i + 1] = entries[i].getValue(); 619 } 620 } 621 622 private @Nullable Object[] lastEntryForEachKey( 623 @Nullable Object[] localAlternatingKeysAndValues, int size) { 624 Set<Object> seenKeys = new HashSet<>(); 625 BitSet dups = new BitSet(); // slots that are overridden by a later duplicate key 626 for (int i = size - 1; i >= 0; i--) { 627 Object key = requireNonNull(localAlternatingKeysAndValues[2 * i]); 628 if (!seenKeys.add(key)) { 629 dups.set(i); 630 } 631 } 632 if (dups.isEmpty()) { 633 return localAlternatingKeysAndValues; 634 } 635 Object[] newAlternatingKeysAndValues = new Object[(size - dups.cardinality()) * 2]; 636 for (int inI = 0, outI = 0; inI < size * 2; ) { 637 if (dups.get(inI >>> 1)) { 638 inI += 2; 639 } else { 640 newAlternatingKeysAndValues[outI++] = 641 requireNonNull(localAlternatingKeysAndValues[inI++]); 642 newAlternatingKeysAndValues[outI++] = 643 requireNonNull(localAlternatingKeysAndValues[inI++]); 644 } 645 } 646 return newAlternatingKeysAndValues; 647 } 648 649 static final class DuplicateKey { 650 private final Object key; 651 private final Object value1; 652 private final Object value2; 653 654 DuplicateKey(Object key, Object value1, Object value2) { 655 this.key = key; 656 this.value1 = value1; 657 this.value2 = value2; 658 } 659 660 IllegalArgumentException exception() { 661 return new IllegalArgumentException( 662 "Multiple entries with same key: " + key + "=" + value1 + " and " + key + "=" + value2); 663 } 664 } 665 } 666 667 /** 668 * Returns an immutable map containing the same entries as {@code map}. The returned map iterates 669 * over entries in the same order as the {@code entrySet} of the original map. If {@code map} 670 * somehow contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose 671 * comparator is not <i>consistent with equals</i>), the results of this method are undefined. 672 * 673 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is 674 * safe to do so. The exact circumstances under which a copy will or will not be performed are 675 * undocumented and subject to change. 676 * 677 * @throws NullPointerException if any key or value in {@code map} is null 678 */ 679 public static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V> map) { 680 if ((map instanceof ImmutableMap) && !(map instanceof SortedMap)) { 681 @SuppressWarnings("unchecked") // safe since map is not writable 682 ImmutableMap<K, V> kvMap = (ImmutableMap<K, V>) map; 683 if (!kvMap.isPartialView()) { 684 return kvMap; 685 } 686 } 687 return copyOf(map.entrySet()); 688 } 689 690 /** 691 * Returns an immutable map containing the specified entries. The returned map iterates over 692 * entries in the same order as the original iterable. 693 * 694 * @throws NullPointerException if any key, value, or entry is null 695 * @throws IllegalArgumentException if two entries have the same key 696 * @since 19.0 697 */ 698 @Beta 699 public static <K, V> ImmutableMap<K, V> copyOf( 700 Iterable<? extends Entry<? extends K, ? extends V>> entries) { 701 int initialCapacity = 702 (entries instanceof Collection) 703 ? ((Collection<?>) entries).size() 704 : ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY; 705 ImmutableMap.Builder<K, V> builder = new ImmutableMap.Builder<K, V>(initialCapacity); 706 builder.putAll(entries); 707 return builder.build(); 708 } 709 710 static final Entry<?, ?>[] EMPTY_ENTRY_ARRAY = new Entry<?, ?>[0]; 711 712 abstract static class IteratorBasedImmutableMap<K, V> extends ImmutableMap<K, V> { 713 abstract UnmodifiableIterator<Entry<K, V>> entryIterator(); 714 715 @Override 716 ImmutableSet<K> createKeySet() { 717 return new ImmutableMapKeySet<>(this); 718 } 719 720 @Override 721 ImmutableSet<Entry<K, V>> createEntrySet() { 722 class EntrySetImpl extends ImmutableMapEntrySet<K, V> { 723 @Override 724 ImmutableMap<K, V> map() { 725 return IteratorBasedImmutableMap.this; 726 } 727 728 @Override 729 public UnmodifiableIterator<Entry<K, V>> iterator() { 730 return entryIterator(); 731 } 732 } 733 return new EntrySetImpl(); 734 } 735 736 @Override 737 ImmutableCollection<V> createValues() { 738 return new ImmutableMapValues<>(this); 739 } 740 } 741 742 ImmutableMap() {} 743 744 /** 745 * Guaranteed to throw an exception and leave the map unmodified. 746 * 747 * @throws UnsupportedOperationException always 748 * @deprecated Unsupported operation. 749 */ 750 @CanIgnoreReturnValue 751 @Deprecated 752 @Override 753 @DoNotCall("Always throws UnsupportedOperationException") 754 @CheckForNull 755 public final V put(K k, V v) { 756 throw new UnsupportedOperationException(); 757 } 758 759 /** 760 * Guaranteed to throw an exception and leave the map unmodified. 761 * 762 * @throws UnsupportedOperationException always 763 * @deprecated Unsupported operation. 764 */ 765 @CanIgnoreReturnValue 766 @Deprecated 767 @Override 768 @CheckForNull 769 public final V remove(@CheckForNull Object o) { 770 throw new UnsupportedOperationException(); 771 } 772 773 /** 774 * Guaranteed to throw an exception and leave the map unmodified. 775 * 776 * @throws UnsupportedOperationException always 777 * @deprecated Unsupported operation. 778 */ 779 @Deprecated 780 @Override 781 @DoNotCall("Always throws UnsupportedOperationException") 782 public final void putAll(Map<? extends K, ? extends V> map) { 783 throw new UnsupportedOperationException(); 784 } 785 786 /** 787 * Guaranteed to throw an exception and leave the map unmodified. 788 * 789 * @throws UnsupportedOperationException always 790 * @deprecated Unsupported operation. 791 */ 792 @Deprecated 793 @Override 794 @DoNotCall("Always throws UnsupportedOperationException") 795 public final void clear() { 796 throw new UnsupportedOperationException(); 797 } 798 799 @Override 800 public boolean isEmpty() { 801 return size() == 0; 802 } 803 804 @Override 805 public boolean containsKey(@CheckForNull Object key) { 806 return get(key) != null; 807 } 808 809 @Override 810 public boolean containsValue(@CheckForNull Object value) { 811 return values().contains(value); 812 } 813 814 // Overriding to mark it Nullable 815 @Override 816 @CheckForNull 817 public abstract V get(@CheckForNull Object key); 818 819 /** 820 * {@inheritDoc} 821 * 822 * <p>See <a 823 * href="https://developer.android.com/reference/java/util/Map.html#getOrDefault%28java.lang.Object,%20V%29">{@code 824 * Map.getOrDefault}</a>. 825 * 826 * @since 23.5 (but since 21.0 in the JRE <a 827 * href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>). 828 * Note that API Level 24 users can call this method with any version of Guava. 829 */ 830 // @Override under Java 8 / API Level 24 831 @CheckForNull 832 public final V getOrDefault(@CheckForNull Object key, @CheckForNull V defaultValue) { 833 /* 834 * Even though it's weird to pass a defaultValue that is null, some callers do so. Those who 835 * pass a literal "null" should probably just use `get`, but I would expect other callers to 836 * pass an expression that *might* be null. This could happen with: 837 * 838 * - a `getFooOrDefault(@CheckForNull Foo defaultValue)` method that returns 839 * `map.getOrDefault(FOO_KEY, defaultValue)` 840 * 841 * - a call that consults a chain of maps, as in `mapA.getOrDefault(key, mapB.getOrDefault(key, 842 * ...))` 843 * 844 * So it makes sense for the parameter (and thus the return type) to be @CheckForNull. 845 * 846 * Two other points: 847 * 848 * 1. We'll want to use something like @PolyNull once we can make that work for the various 849 * platforms we target. 850 * 851 * 2. Kotlin's Map type has a getOrDefault method that accepts and returns a "plain V," in 852 * contrast to the "V?" type that we're using. As a result, Kotlin sees a conflict between the 853 * nullness annotations in ImmutableMap and those in its own Map type. In response, it considers 854 * the parameter and return type both to be platform types. As a result, Kotlin permits calls 855 * that can lead to NullPointerException. That's unfortunate. But hopefully most Kotlin callers 856 * use `get(key) ?: defaultValue` instead of this method, anyway. 857 */ 858 V result = get(key); 859 // TODO(b/192579700): Use a ternary once it no longer confuses our nullness checker. 860 if (result != null) { 861 return result; 862 } else { 863 return defaultValue; 864 } 865 } 866 867 @LazyInit @RetainedWith @CheckForNull private transient ImmutableSet<Entry<K, V>> entrySet; 868 869 /** 870 * Returns an immutable set of the mappings in this map. The iteration order is specified by the 871 * method used to create this map. Typically, this is insertion order. 872 */ 873 @Override 874 public ImmutableSet<Entry<K, V>> entrySet() { 875 ImmutableSet<Entry<K, V>> result = entrySet; 876 return (result == null) ? entrySet = createEntrySet() : result; 877 } 878 879 abstract ImmutableSet<Entry<K, V>> createEntrySet(); 880 881 @LazyInit @RetainedWith @CheckForNull private transient ImmutableSet<K> keySet; 882 883 /** 884 * Returns an immutable set of the keys in this map, in the same order that they appear in {@link 885 * #entrySet}. 886 */ 887 @Override 888 public ImmutableSet<K> keySet() { 889 ImmutableSet<K> result = keySet; 890 return (result == null) ? keySet = createKeySet() : result; 891 } 892 893 /* 894 * This could have a good default implementation of return new ImmutableKeySet<K, V>(this), 895 * but ProGuard can't figure out how to eliminate that default when RegularImmutableMap 896 * overrides it. 897 */ 898 abstract ImmutableSet<K> createKeySet(); 899 900 UnmodifiableIterator<K> keyIterator() { 901 final UnmodifiableIterator<Entry<K, V>> entryIterator = entrySet().iterator(); 902 return new UnmodifiableIterator<K>() { 903 @Override 904 public boolean hasNext() { 905 return entryIterator.hasNext(); 906 } 907 908 @Override 909 public K next() { 910 return entryIterator.next().getKey(); 911 } 912 }; 913 } 914 915 @LazyInit @RetainedWith @CheckForNull private transient ImmutableCollection<V> values; 916 917 /** 918 * Returns an immutable collection of the values in this map, in the same order that they appear 919 * in {@link #entrySet}. 920 */ 921 @Override 922 public ImmutableCollection<V> values() { 923 ImmutableCollection<V> result = values; 924 return (result == null) ? values = createValues() : result; 925 } 926 927 /* 928 * This could have a good default implementation of {@code return new 929 * ImmutableMapValues<K, V>(this)}, but ProGuard can't figure out how to eliminate that default 930 * when RegularImmutableMap overrides it. 931 */ 932 abstract ImmutableCollection<V> createValues(); 933 934 // cached so that this.multimapView().inverse() only computes inverse once 935 @LazyInit @CheckForNull private transient ImmutableSetMultimap<K, V> multimapView; 936 937 /** 938 * Returns a multimap view of the map. 939 * 940 * @since 14.0 941 */ 942 public ImmutableSetMultimap<K, V> asMultimap() { 943 if (isEmpty()) { 944 return ImmutableSetMultimap.of(); 945 } 946 ImmutableSetMultimap<K, V> result = multimapView; 947 return (result == null) 948 ? (multimapView = 949 new ImmutableSetMultimap<>(new MapViewOfValuesAsSingletonSets(), size(), null)) 950 : result; 951 } 952 953 @WeakOuter 954 private final class MapViewOfValuesAsSingletonSets 955 extends IteratorBasedImmutableMap<K, ImmutableSet<V>> { 956 957 @Override 958 public int size() { 959 return ImmutableMap.this.size(); 960 } 961 962 @Override 963 ImmutableSet<K> createKeySet() { 964 return ImmutableMap.this.keySet(); 965 } 966 967 @Override 968 public boolean containsKey(@CheckForNull Object key) { 969 return ImmutableMap.this.containsKey(key); 970 } 971 972 @Override 973 @CheckForNull 974 public ImmutableSet<V> get(@CheckForNull Object key) { 975 V outerValue = ImmutableMap.this.get(key); 976 return (outerValue == null) ? null : ImmutableSet.of(outerValue); 977 } 978 979 @Override 980 boolean isPartialView() { 981 return ImmutableMap.this.isPartialView(); 982 } 983 984 @Override 985 public int hashCode() { 986 // ImmutableSet.of(value).hashCode() == value.hashCode(), so the hashes are the same 987 return ImmutableMap.this.hashCode(); 988 } 989 990 @Override 991 boolean isHashCodeFast() { 992 return ImmutableMap.this.isHashCodeFast(); 993 } 994 995 @Override 996 UnmodifiableIterator<Entry<K, ImmutableSet<V>>> entryIterator() { 997 final Iterator<Entry<K, V>> backingIterator = ImmutableMap.this.entrySet().iterator(); 998 return new UnmodifiableIterator<Entry<K, ImmutableSet<V>>>() { 999 @Override 1000 public boolean hasNext() { 1001 return backingIterator.hasNext(); 1002 } 1003 1004 @Override 1005 public Entry<K, ImmutableSet<V>> next() { 1006 final Entry<K, V> backingEntry = backingIterator.next(); 1007 return new AbstractMapEntry<K, ImmutableSet<V>>() { 1008 @Override 1009 public K getKey() { 1010 return backingEntry.getKey(); 1011 } 1012 1013 @Override 1014 public ImmutableSet<V> getValue() { 1015 return ImmutableSet.of(backingEntry.getValue()); 1016 } 1017 }; 1018 } 1019 }; 1020 } 1021 } 1022 1023 @Override 1024 public boolean equals(@CheckForNull Object object) { 1025 return Maps.equalsImpl(this, object); 1026 } 1027 1028 abstract boolean isPartialView(); 1029 1030 @Override 1031 public int hashCode() { 1032 return Sets.hashCodeImpl(entrySet()); 1033 } 1034 1035 boolean isHashCodeFast() { 1036 return false; 1037 } 1038 1039 @Override 1040 public String toString() { 1041 return Maps.toStringImpl(this); 1042 } 1043 1044 /** 1045 * Serialized type for all ImmutableMap instances. It captures the logical contents and they are 1046 * reconstructed using public factory methods. This ensures that the implementation types remain 1047 * as implementation details. 1048 */ 1049 static class SerializedForm<K, V> implements Serializable { 1050 // This object retains references to collections returned by keySet() and value(). This saves 1051 // bytes when the both the map and its keySet or value collection are written to the same 1052 // instance of ObjectOutputStream. 1053 1054 // TODO(b/160980469): remove support for the old serialization format after some time 1055 private static final boolean USE_LEGACY_SERIALIZATION = true; 1056 1057 private final Object keys; 1058 private final Object values; 1059 1060 SerializedForm(ImmutableMap<K, V> map) { 1061 if (USE_LEGACY_SERIALIZATION) { 1062 Object[] keys = new Object[map.size()]; 1063 Object[] values = new Object[map.size()]; 1064 int i = 0; 1065 // "extends Object" works around https://github.com/typetools/checker-framework/issues/3013 1066 for (Entry<? extends Object, ? extends Object> entry : map.entrySet()) { 1067 keys[i] = entry.getKey(); 1068 values[i] = entry.getValue(); 1069 i++; 1070 } 1071 this.keys = keys; 1072 this.values = values; 1073 return; 1074 } 1075 this.keys = map.keySet(); 1076 this.values = map.values(); 1077 } 1078 1079 @SuppressWarnings("unchecked") 1080 final Object readResolve() { 1081 if (!(this.keys instanceof ImmutableSet)) { 1082 return legacyReadResolve(); 1083 } 1084 1085 ImmutableSet<K> keySet = (ImmutableSet<K>) this.keys; 1086 ImmutableCollection<V> values = (ImmutableCollection<V>) this.values; 1087 1088 Builder<K, V> builder = makeBuilder(keySet.size()); 1089 1090 UnmodifiableIterator<K> keyIter = keySet.iterator(); 1091 UnmodifiableIterator<V> valueIter = values.iterator(); 1092 1093 while (keyIter.hasNext()) { 1094 builder.put(keyIter.next(), valueIter.next()); 1095 } 1096 1097 return builder.buildOrThrow(); 1098 } 1099 1100 @SuppressWarnings("unchecked") 1101 final Object legacyReadResolve() { 1102 K[] keys = (K[]) this.keys; 1103 V[] values = (V[]) this.values; 1104 1105 Builder<K, V> builder = makeBuilder(keys.length); 1106 1107 for (int i = 0; i < keys.length; i++) { 1108 builder.put(keys[i], values[i]); 1109 } 1110 return builder.buildOrThrow(); 1111 } 1112 1113 /** 1114 * Returns a builder that builds the unserialized type. Subclasses should override this method. 1115 */ 1116 Builder<K, V> makeBuilder(int size) { 1117 return new Builder<>(size); 1118 } 1119 1120 private static final long serialVersionUID = 0; 1121 } 1122 1123 /** 1124 * Returns a serializable form of this object. Non-public subclasses should not override this 1125 * method. Publicly-accessible subclasses must override this method and should return a subclass 1126 * of SerializedForm whose readResolve() method returns objects of the subclass type. 1127 */ 1128 Object writeReplace() { 1129 return new SerializedForm<>(this); 1130 } 1131}