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