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