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; 023 024import com.google.common.annotations.Beta; 025import com.google.common.annotations.GwtCompatible; 026import com.google.errorprone.annotations.CanIgnoreReturnValue; 027import com.google.errorprone.annotations.DoNotMock; 028import com.google.errorprone.annotations.concurrent.LazyInit; 029import com.google.j2objc.annotations.RetainedWith; 030import com.google.j2objc.annotations.WeakOuter; 031import java.io.Serializable; 032import java.util.AbstractMap; 033import java.util.Arrays; 034import java.util.Collection; 035import java.util.Collections; 036import java.util.Comparator; 037import java.util.Iterator; 038import java.util.Map; 039import java.util.Map.Entry; 040import java.util.SortedMap; 041import org.checkerframework.checker.nullness.compatqual.MonotonicNonNullDecl; 042import org.checkerframework.checker.nullness.compatqual.NullableDecl; 043 044/** 045 * A {@link Map} whose contents will never change, with many other important properties detailed at 046 * {@link ImmutableCollection}. 047 * 048 * <p>See the Guava User Guide article on <a href= 049 * "https://github.com/google/guava/wiki/ImmutableCollectionsExplained"> immutable collections</a>. 050 * 051 * @author Jesse Wilson 052 * @author Kevin Bourrillion 053 * @since 2.0 054 */ 055@DoNotMock("Use ImmutableMap.of or another implementation") 056@GwtCompatible(serializable = true, emulated = true) 057@SuppressWarnings("serial") // we're overriding default serialization 058public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable { 059 060 /** 061 * Returns the empty map. This map behaves and performs comparably to {@link 062 * Collections#emptyMap}, and is preferable mainly for consistency and maintainability of your 063 * code. 064 */ 065 @SuppressWarnings("unchecked") 066 public static <K, V> ImmutableMap<K, V> of() { 067 return (ImmutableMap<K, V>) RegularImmutableMap.EMPTY; 068 } 069 070 /** 071 * Returns an immutable map containing a single entry. This map behaves and performs comparably to 072 * {@link Collections#singletonMap} but will not accept a null key or value. It is preferable 073 * mainly for consistency and maintainability of your code. 074 */ 075 public static <K, V> ImmutableMap<K, V> of(K k1, V v1) { 076 checkEntryNotNull(k1, v1); 077 return RegularImmutableMap.create(1, new Object[] {k1, v1}); 078 } 079 080 /** 081 * Returns an immutable map containing the given entries, in order. 082 * 083 * @throws IllegalArgumentException if duplicate keys are provided 084 */ 085 public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2) { 086 checkEntryNotNull(k1, v1); 087 checkEntryNotNull(k2, v2); 088 return RegularImmutableMap.create(2, new Object[] {k1, v1, k2, v2}); 089 } 090 091 /** 092 * Returns an immutable map containing the given entries, in order. 093 * 094 * @throws IllegalArgumentException if duplicate keys are provided 095 */ 096 public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) { 097 checkEntryNotNull(k1, v1); 098 checkEntryNotNull(k2, v2); 099 checkEntryNotNull(k3, v3); 100 return RegularImmutableMap.create(3, new Object[] {k1, v1, k2, v2, k3, v3}); 101 } 102 103 /** 104 * Returns an immutable map containing the given entries, in order. 105 * 106 * @throws IllegalArgumentException if duplicate keys are provided 107 */ 108 public static <K, V> ImmutableMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { 109 checkEntryNotNull(k1, v1); 110 checkEntryNotNull(k2, v2); 111 checkEntryNotNull(k3, v3); 112 checkEntryNotNull(k4, v4); 113 return RegularImmutableMap.create(4, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4}); 114 } 115 116 /** 117 * Returns an immutable map containing the given entries, in order. 118 * 119 * @throws IllegalArgumentException if duplicate keys are provided 120 */ 121 public static <K, V> ImmutableMap<K, V> of( 122 K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { 123 checkEntryNotNull(k1, v1); 124 checkEntryNotNull(k2, v2); 125 checkEntryNotNull(k3, v3); 126 checkEntryNotNull(k4, v4); 127 checkEntryNotNull(k5, v5); 128 return RegularImmutableMap.create(5, new Object[] {k1, v1, k2, v2, k3, v3, k4, v4, k5, v5}); 129 } 130 131 // looking for of() with > 5 entries? Use the builder instead. 132 133 /** 134 * Verifies that {@code key} and {@code value} are non-null, and returns a new immutable entry 135 * with those values. 136 * 137 * <p>A call to {@link Entry#setValue} on the returned entry will always throw {@link 138 * UnsupportedOperationException}. 139 */ 140 static <K, V> Entry<K, V> entryOf(K key, V value) { 141 checkEntryNotNull(key, value); 142 return new AbstractMap.SimpleImmutableEntry<>(key, value); 143 } 144 145 /** 146 * Returns a new builder. The generated builder is equivalent to the builder created by the {@link 147 * Builder} constructor. 148 */ 149 public static <K, V> Builder<K, V> builder() { 150 return new Builder<>(); 151 } 152 153 /** 154 * Returns a new builder, expecting the specified number of entries to be added. 155 * 156 * <p>If {@code expectedSize} is exactly the number of entries added to the builder before {@link 157 * Builder#build} is called, the builder is likely to perform better than an unsized {@link 158 * #builder()} would have. 159 * 160 * <p>It is not specified if any performance benefits apply if {@code expectedSize} is close to, 161 * but not exactly, the number of entries added to the builder. 162 * 163 * @since 23.1 164 */ 165 @Beta 166 public static <K, V> Builder<K, V> builderWithExpectedSize(int expectedSize) { 167 checkNonnegative(expectedSize, "expectedSize"); 168 return new Builder<>(expectedSize); 169 } 170 171 static void checkNoConflict( 172 boolean safe, String conflictDescription, Entry<?, ?> entry1, Entry<?, ?> entry2) { 173 if (!safe) { 174 throw conflictException(conflictDescription, entry1, entry2); 175 } 176 } 177 178 static IllegalArgumentException conflictException( 179 String conflictDescription, Object entry1, Object entry2) { 180 return new IllegalArgumentException( 181 "Multiple entries with same " + conflictDescription + ": " + entry1 + " and " + entry2); 182 } 183 184 /** 185 * A builder for creating immutable map instances, especially {@code public static final} maps 186 * ("constant maps"). Example: 187 * 188 * <pre>{@code 189 * static final ImmutableMap<String, Integer> WORD_TO_INT = 190 * new ImmutableMap.Builder<String, Integer>() 191 * .put("one", 1) 192 * .put("two", 2) 193 * .put("three", 3) 194 * .build(); 195 * }</pre> 196 * 197 * <p>For <i>small</i> immutable maps, the {@code ImmutableMap.of()} methods are even more 198 * convenient. 199 * 200 * <p>By default, a {@code Builder} will generate maps that iterate over entries in the order they 201 * were inserted into the builder, equivalently to {@code LinkedHashMap}. For example, in the 202 * above example, {@code WORD_TO_INT.entrySet()} is guaranteed to iterate over the entries in the 203 * order {@code "one"=1, "two"=2, "three"=3}, and {@code keySet()} and {@code values()} respect 204 * the same order. If you want a different order, consider using {@link ImmutableSortedMap} to 205 * sort by keys, or call {@link #orderEntriesByValue(Comparator)}, which changes this builder to 206 * sort entries by value. 207 * 208 * <p>Builder instances can be reused - it is safe to call {@link #build} multiple times to build 209 * multiple maps in series. Each map is a superset of the maps created before it. 210 * 211 * @since 2.0 212 */ 213 @DoNotMock 214 public static class Builder<K, V> { 215 @MonotonicNonNullDecl Comparator<? super V> valueComparator; 216 Object[] alternatingKeysAndValues; 217 int size; 218 boolean entriesUsed; 219 220 /** 221 * Creates a new builder. The returned builder is equivalent to the builder generated by {@link 222 * ImmutableMap#builder}. 223 */ 224 public Builder() { 225 this(ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY); 226 } 227 228 @SuppressWarnings("unchecked") 229 Builder(int initialCapacity) { 230 this.alternatingKeysAndValues = new Object[2 * initialCapacity]; 231 this.size = 0; 232 this.entriesUsed = false; 233 } 234 235 private void ensureCapacity(int minCapacity) { 236 if (minCapacity * 2 > alternatingKeysAndValues.length) { 237 alternatingKeysAndValues = 238 Arrays.copyOf( 239 alternatingKeysAndValues, 240 ImmutableCollection.Builder.expandedCapacity( 241 alternatingKeysAndValues.length, minCapacity * 2)); 242 entriesUsed = false; 243 } 244 } 245 246 /** 247 * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed, 248 * and will cause {@link #build} to fail. 249 */ 250 @CanIgnoreReturnValue 251 public Builder<K, V> put(K key, V value) { 252 ensureCapacity(size + 1); 253 checkEntryNotNull(key, value); 254 alternatingKeysAndValues[2 * size] = key; 255 alternatingKeysAndValues[2 * size + 1] = value; 256 size++; 257 return this; 258 } 259 260 /** 261 * Adds the given {@code entry} to the map, making it immutable if necessary. Duplicate keys are 262 * not allowed, and will cause {@link #build} to fail. 263 * 264 * @since 11.0 265 */ 266 @CanIgnoreReturnValue 267 public Builder<K, V> put(Entry<? extends K, ? extends V> entry) { 268 return put(entry.getKey(), entry.getValue()); 269 } 270 271 /** 272 * Associates all of the given map's keys and values in the built map. Duplicate keys are not 273 * allowed, and will cause {@link #build} to fail. 274 * 275 * @throws NullPointerException if any key or value in {@code map} is null 276 */ 277 @CanIgnoreReturnValue 278 public Builder<K, V> putAll(Map<? extends K, ? extends V> map) { 279 return putAll(map.entrySet()); 280 } 281 282 /** 283 * Adds all of the given entries to the built map. Duplicate keys are not allowed, and will 284 * cause {@link #build} to fail. 285 * 286 * @throws NullPointerException if any key, value, or entry is null 287 * @since 19.0 288 */ 289 @CanIgnoreReturnValue 290 @Beta 291 public Builder<K, V> putAll(Iterable<? extends Entry<? extends K, ? extends V>> entries) { 292 if (entries instanceof Collection) { 293 ensureCapacity(size + ((Collection<?>) entries).size()); 294 } 295 for (Entry<? extends K, ? extends V> entry : entries) { 296 put(entry); 297 } 298 return this; 299 } 300 301 /** 302 * Configures this {@code Builder} to order entries by value according to the specified 303 * comparator. 304 * 305 * <p>The sort order is stable, that is, if two entries have values that compare as equivalent, 306 * the entry that was inserted first will be first in the built map's iteration order. 307 * 308 * @throws IllegalStateException if this method was already called 309 * @since 19.0 310 */ 311 @CanIgnoreReturnValue 312 @Beta 313 public Builder<K, V> orderEntriesByValue(Comparator<? super V> valueComparator) { 314 checkState(this.valueComparator == null, "valueComparator was already set"); 315 this.valueComparator = checkNotNull(valueComparator, "valueComparator"); 316 return this; 317 } 318 319 /* 320 * TODO(kevinb): Should build() and the ImmutableBiMap & ImmutableSortedMap 321 * versions throw an IllegalStateException instead? 322 */ 323 324 /** 325 * Returns a newly-created immutable map. The iteration order of the returned map is the order 326 * in which entries were inserted into the builder, unless {@link #orderEntriesByValue} was 327 * called, in which case entries are sorted by value. 328 * 329 * @throws IllegalArgumentException if duplicate keys were added 330 */ 331 @SuppressWarnings("unchecked") 332 public ImmutableMap<K, V> build() { 333 /* 334 * If entries is full, then this implementation may end up using the entries array 335 * directly and writing over the entry objects with non-terminal entries, but this is 336 * safe; if this Builder is used further, it will grow the entries array (so it can't 337 * affect the original array), and future build() calls will always copy any entry 338 * objects that cannot be safely reused. 339 */ 340 sortEntries(); 341 entriesUsed = true; 342 return RegularImmutableMap.create(size, alternatingKeysAndValues); 343 } 344 345 void sortEntries() { 346 if (valueComparator != null) { 347 if (entriesUsed) { 348 alternatingKeysAndValues = Arrays.copyOf(alternatingKeysAndValues, 2 * size); 349 } 350 Entry<K, V>[] entries = new Entry[size]; 351 for (int i = 0; i < size; i++) { 352 entries[i] = 353 new AbstractMap.SimpleImmutableEntry<K, V>( 354 (K) alternatingKeysAndValues[2 * i], (V) alternatingKeysAndValues[2 * i + 1]); 355 } 356 Arrays.sort( 357 entries, 0, size, Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction())); 358 for (int i = 0; i < size; i++) { 359 alternatingKeysAndValues[2 * i] = entries[i].getKey(); 360 alternatingKeysAndValues[2 * i + 1] = entries[i].getValue(); 361 } 362 } 363 } 364 } 365 366 /** 367 * Returns an immutable map containing the same entries as {@code map}. The returned map iterates 368 * over entries in the same order as the {@code entrySet} of the original map. If {@code map} 369 * somehow contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose 370 * comparator is not <i>consistent with equals</i>), the results of this method are undefined. 371 * 372 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is 373 * safe to do so. The exact circumstances under which a copy will or will not be performed are 374 * undocumented and subject to change. 375 * 376 * @throws NullPointerException if any key or value in {@code map} is null 377 */ 378 public static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V> map) { 379 if ((map instanceof ImmutableMap) && !(map instanceof SortedMap)) { 380 @SuppressWarnings("unchecked") // safe since map is not writable 381 ImmutableMap<K, V> kvMap = (ImmutableMap<K, V>) map; 382 if (!kvMap.isPartialView()) { 383 return kvMap; 384 } 385 } 386 return copyOf(map.entrySet()); 387 } 388 389 /** 390 * Returns an immutable map containing the specified entries. The returned map iterates over 391 * entries in the same order as the original iterable. 392 * 393 * @throws NullPointerException if any key, value, or entry is null 394 * @throws IllegalArgumentException if two entries have the same key 395 * @since 19.0 396 */ 397 @Beta 398 public static <K, V> ImmutableMap<K, V> copyOf( 399 Iterable<? extends Entry<? extends K, ? extends V>> entries) { 400 int initialCapacity = 401 (entries instanceof Collection) 402 ? ((Collection<?>) entries).size() 403 : ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY; 404 ImmutableMap.Builder<K, V> builder = new ImmutableMap.Builder<K, V>(initialCapacity); 405 builder.putAll(entries); 406 return builder.build(); 407 } 408 409 static final Entry<?, ?>[] EMPTY_ENTRY_ARRAY = new Entry<?, ?>[0]; 410 411 abstract static class IteratorBasedImmutableMap<K, V> extends ImmutableMap<K, V> { 412 abstract UnmodifiableIterator<Entry<K, V>> entryIterator(); 413 414 @Override 415 ImmutableSet<K> createKeySet() { 416 return new ImmutableMapKeySet<>(this); 417 } 418 419 @Override 420 ImmutableSet<Entry<K, V>> createEntrySet() { 421 class EntrySetImpl extends ImmutableMapEntrySet<K, V> { 422 @Override 423 ImmutableMap<K, V> map() { 424 return IteratorBasedImmutableMap.this; 425 } 426 427 @Override 428 public UnmodifiableIterator<Entry<K, V>> iterator() { 429 return entryIterator(); 430 } 431 } 432 return new EntrySetImpl(); 433 } 434 435 @Override 436 ImmutableCollection<V> createValues() { 437 return new ImmutableMapValues<>(this); 438 } 439 } 440 441 ImmutableMap() {} 442 443 /** 444 * Guaranteed to throw an exception and leave the map unmodified. 445 * 446 * @throws UnsupportedOperationException always 447 * @deprecated Unsupported operation. 448 */ 449 @CanIgnoreReturnValue 450 @Deprecated 451 @Override 452 public final V put(K k, V v) { 453 throw new UnsupportedOperationException(); 454 } 455 456 /** 457 * Guaranteed to throw an exception and leave the map unmodified. 458 * 459 * @throws UnsupportedOperationException always 460 * @deprecated Unsupported operation. 461 */ 462 @CanIgnoreReturnValue 463 @Deprecated 464 @Override 465 public final V remove(Object o) { 466 throw new UnsupportedOperationException(); 467 } 468 469 /** 470 * Guaranteed to throw an exception and leave the map unmodified. 471 * 472 * @throws UnsupportedOperationException always 473 * @deprecated Unsupported operation. 474 */ 475 @Deprecated 476 @Override 477 public final void putAll(Map<? extends K, ? extends V> map) { 478 throw new UnsupportedOperationException(); 479 } 480 481 /** 482 * Guaranteed to throw an exception and leave the map unmodified. 483 * 484 * @throws UnsupportedOperationException always 485 * @deprecated Unsupported operation. 486 */ 487 @Deprecated 488 @Override 489 public final void clear() { 490 throw new UnsupportedOperationException(); 491 } 492 493 @Override 494 public boolean isEmpty() { 495 return size() == 0; 496 } 497 498 @Override 499 public boolean containsKey(@NullableDecl Object key) { 500 return get(key) != null; 501 } 502 503 @Override 504 public boolean containsValue(@NullableDecl Object value) { 505 return values().contains(value); 506 } 507 508 // Overriding to mark it Nullable 509 @Override 510 public abstract V get(@NullableDecl Object key); 511 512 /** 513 * {@inheritDoc} 514 * 515 * <p>See <a 516 * href="https://developer.android.com/reference/java/util/Map.html#getOrDefault%28java.lang.Object,%20V%29">{@code 517 * Map.getOrDefault}</a>. 518 * 519 * @since 23.5 (but since 21.0 in the JRE <a 520 * href="https://github.com/google/guava#guava-google-core-libraries-for-java">flavor</a>). 521 * Note that API Level 24 users can call this method with any version of Guava. 522 */ 523 // @Override under Java 8 / API Level 24 524 public final V getOrDefault(@NullableDecl Object key, @NullableDecl V defaultValue) { 525 V result = get(key); 526 return (result != null) ? result : defaultValue; 527 } 528 529 @LazyInit @RetainedWith private transient ImmutableSet<Entry<K, V>> entrySet; 530 531 /** 532 * Returns an immutable set of the mappings in this map. The iteration order is specified by the 533 * method used to create this map. Typically, this is insertion order. 534 */ 535 @Override 536 public ImmutableSet<Entry<K, V>> entrySet() { 537 ImmutableSet<Entry<K, V>> result = entrySet; 538 return (result == null) ? entrySet = createEntrySet() : result; 539 } 540 541 abstract ImmutableSet<Entry<K, V>> createEntrySet(); 542 543 @LazyInit @RetainedWith private transient ImmutableSet<K> keySet; 544 545 /** 546 * Returns an immutable set of the keys in this map, in the same order that they appear in {@link 547 * #entrySet}. 548 */ 549 @Override 550 public ImmutableSet<K> keySet() { 551 ImmutableSet<K> result = keySet; 552 return (result == null) ? keySet = createKeySet() : result; 553 } 554 555 /* 556 * This could have a good default implementation of return new ImmutableKeySet<K, V>(this), 557 * but ProGuard can't figure out how to eliminate that default when RegularImmutableMap 558 * overrides it. 559 */ 560 abstract ImmutableSet<K> createKeySet(); 561 562 UnmodifiableIterator<K> keyIterator() { 563 final UnmodifiableIterator<Entry<K, V>> entryIterator = entrySet().iterator(); 564 return new UnmodifiableIterator<K>() { 565 @Override 566 public boolean hasNext() { 567 return entryIterator.hasNext(); 568 } 569 570 @Override 571 public K next() { 572 return entryIterator.next().getKey(); 573 } 574 }; 575 } 576 577 @LazyInit @RetainedWith private transient ImmutableCollection<V> values; 578 579 /** 580 * Returns an immutable collection of the values in this map, in the same order that they appear 581 * in {@link #entrySet}. 582 */ 583 @Override 584 public ImmutableCollection<V> values() { 585 ImmutableCollection<V> result = values; 586 return (result == null) ? values = createValues() : result; 587 } 588 589 /* 590 * This could have a good default implementation of {@code return new 591 * ImmutableMapValues<K, V>(this)}, but ProGuard can't figure out how to eliminate that default 592 * when RegularImmutableMap overrides it. 593 */ 594 abstract ImmutableCollection<V> createValues(); 595 596 // cached so that this.multimapView().inverse() only computes inverse once 597 @LazyInit private transient ImmutableSetMultimap<K, V> multimapView; 598 599 /** 600 * Returns a multimap view of the map. 601 * 602 * @since 14.0 603 */ 604 public ImmutableSetMultimap<K, V> asMultimap() { 605 if (isEmpty()) { 606 return ImmutableSetMultimap.of(); 607 } 608 ImmutableSetMultimap<K, V> result = multimapView; 609 return (result == null) 610 ? (multimapView = 611 new ImmutableSetMultimap<>(new MapViewOfValuesAsSingletonSets(), size(), null)) 612 : result; 613 } 614 615 @WeakOuter 616 private final class MapViewOfValuesAsSingletonSets 617 extends IteratorBasedImmutableMap<K, ImmutableSet<V>> { 618 619 @Override 620 public int size() { 621 return ImmutableMap.this.size(); 622 } 623 624 @Override 625 ImmutableSet<K> createKeySet() { 626 return ImmutableMap.this.keySet(); 627 } 628 629 @Override 630 public boolean containsKey(@NullableDecl Object key) { 631 return ImmutableMap.this.containsKey(key); 632 } 633 634 @Override 635 public ImmutableSet<V> get(@NullableDecl Object key) { 636 V outerValue = ImmutableMap.this.get(key); 637 return (outerValue == null) ? null : ImmutableSet.of(outerValue); 638 } 639 640 @Override 641 boolean isPartialView() { 642 return ImmutableMap.this.isPartialView(); 643 } 644 645 @Override 646 public int hashCode() { 647 // ImmutableSet.of(value).hashCode() == value.hashCode(), so the hashes are the same 648 return ImmutableMap.this.hashCode(); 649 } 650 651 @Override 652 boolean isHashCodeFast() { 653 return ImmutableMap.this.isHashCodeFast(); 654 } 655 656 @Override 657 UnmodifiableIterator<Entry<K, ImmutableSet<V>>> entryIterator() { 658 final Iterator<Entry<K, V>> backingIterator = ImmutableMap.this.entrySet().iterator(); 659 return new UnmodifiableIterator<Entry<K, ImmutableSet<V>>>() { 660 @Override 661 public boolean hasNext() { 662 return backingIterator.hasNext(); 663 } 664 665 @Override 666 public Entry<K, ImmutableSet<V>> next() { 667 final Entry<K, V> backingEntry = backingIterator.next(); 668 return new AbstractMapEntry<K, ImmutableSet<V>>() { 669 @Override 670 public K getKey() { 671 return backingEntry.getKey(); 672 } 673 674 @Override 675 public ImmutableSet<V> getValue() { 676 return ImmutableSet.of(backingEntry.getValue()); 677 } 678 }; 679 } 680 }; 681 } 682 } 683 684 @Override 685 public boolean equals(@NullableDecl Object object) { 686 return Maps.equalsImpl(this, object); 687 } 688 689 abstract boolean isPartialView(); 690 691 @Override 692 public int hashCode() { 693 return Sets.hashCodeImpl(entrySet()); 694 } 695 696 boolean isHashCodeFast() { 697 return false; 698 } 699 700 @Override 701 public String toString() { 702 return Maps.toStringImpl(this); 703 } 704 705 /** 706 * Serialized type for all ImmutableMap instances. It captures the logical contents and they are 707 * reconstructed using public factory methods. This ensures that the implementation types remain 708 * as implementation details. 709 */ 710 static class SerializedForm implements Serializable { 711 private final Object[] keys; 712 private final Object[] values; 713 714 SerializedForm(ImmutableMap<?, ?> map) { 715 keys = new Object[map.size()]; 716 values = new Object[map.size()]; 717 int i = 0; 718 for (Entry<?, ?> entry : map.entrySet()) { 719 keys[i] = entry.getKey(); 720 values[i] = entry.getValue(); 721 i++; 722 } 723 } 724 725 Object readResolve() { 726 Builder<Object, Object> builder = new Builder<>(keys.length); 727 return createMap(builder); 728 } 729 730 Object createMap(Builder<Object, Object> builder) { 731 for (int i = 0; i < keys.length; i++) { 732 builder.put(keys[i], values[i]); 733 } 734 return builder.build(); 735 } 736 737 private static final long serialVersionUID = 0; 738 } 739 740 Object writeReplace() { 741 return new SerializedForm(this); 742 } 743}