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.j2objc.annotations.WeakOuter; 026 027import java.io.Serializable; 028import java.util.Arrays; 029import java.util.Collection; 030import java.util.Collections; 031import java.util.Comparator; 032import java.util.EnumMap; 033import java.util.Iterator; 034import java.util.Map; 035 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 public Builder<K, V> put(K key, V value) { 196 ensureCapacity(size + 1); 197 ImmutableMapEntry<K, V> entry = entryOf(key, value); 198 // don't inline this: we want to fail atomically if key or value is null 199 entries[size++] = entry; 200 return this; 201 } 202 203 /** 204 * Adds the given {@code entry} to the map, making it immutable if 205 * necessary. Duplicate keys are not allowed, and will cause {@link #build} 206 * to fail. 207 * 208 * @since 11.0 209 */ 210 public Builder<K, V> put(Entry<? extends K, ? extends V> entry) { 211 return put(entry.getKey(), entry.getValue()); 212 } 213 214 /** 215 * Associates all of the given map's keys and values in the built map. 216 * Duplicate keys are not allowed, and will cause {@link #build} to fail. 217 * 218 * @throws NullPointerException if any key or value in {@code map} is null 219 */ 220 public Builder<K, V> putAll(Map<? extends K, ? extends V> map) { 221 return putAll(map.entrySet()); 222 } 223 224 /** 225 * Adds all of the given entries to the built map. Duplicate keys are not 226 * allowed, and will cause {@link #build} to fail. 227 * 228 * @throws NullPointerException if any key, value, or entry is null 229 * @since 19.0 230 */ 231 @Beta 232 public Builder<K, V> putAll(Iterable<? extends Entry<? extends K, ? extends V>> entries) { 233 if (entries instanceof Collection) { 234 ensureCapacity(size + ((Collection<?>) entries).size()); 235 } 236 for (Entry<? extends K, ? extends V> entry : entries) { 237 put(entry); 238 } 239 return this; 240 } 241 242 /** 243 * Configures this {@code Builder} to order entries by value according to the specified 244 * comparator. 245 * 246 * <p>The sort order is stable, that is, if two entries have values that compare 247 * as equivalent, the entry that was inserted first will be first in the built map's 248 * iteration order. 249 * 250 * @throws IllegalStateException if this method was already called 251 * @since 19.0 252 */ 253 @Beta 254 public Builder<K, V> orderEntriesByValue(Comparator<? super V> valueComparator) { 255 checkState(this.valueComparator == null, "valueComparator was already set"); 256 this.valueComparator = checkNotNull(valueComparator, "valueComparator"); 257 return this; 258 } 259 260 /* 261 * TODO(kevinb): Should build() and the ImmutableBiMap & ImmutableSortedMap 262 * versions throw an IllegalStateException instead? 263 */ 264 265 /** 266 * Returns a newly-created immutable map. 267 * 268 * @throws IllegalArgumentException if duplicate keys were added 269 */ 270 public ImmutableMap<K, V> build() { 271 switch (size) { 272 case 0: 273 return of(); 274 case 1: 275 return of(entries[0].getKey(), entries[0].getValue()); 276 default: 277 /* 278 * If entries is full, then this implementation may end up using the entries array 279 * directly and writing over the entry objects with non-terminal entries, but this is 280 * safe; if this Builder is used further, it will grow the entries array (so it can't 281 * affect the original array), and future build() calls will always copy any entry 282 * objects that cannot be safely reused. 283 */ 284 if (valueComparator != null) { 285 if (entriesUsed) { 286 entries = ObjectArrays.arraysCopyOf(entries, size); 287 } 288 Arrays.sort( 289 entries, 290 0, 291 size, 292 Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction())); 293 } 294 entriesUsed = size == entries.length; 295 return RegularImmutableMap.fromEntryArray(size, entries); 296 } 297 } 298 } 299 300 /** 301 * Returns an immutable map containing the same entries as {@code map}. If 302 * {@code map} somehow contains entries with duplicate keys (for example, if 303 * it is a {@code SortedMap} whose comparator is not <i>consistent with 304 * equals</i>), the results of this method are undefined. 305 * 306 * <p>Despite the method name, this method attempts to avoid actually copying 307 * the data when it is safe to do so. The exact circumstances under which a 308 * copy will or will not be performed are undocumented and subject to change. 309 * 310 * @throws NullPointerException if any key or value in {@code map} is null 311 */ 312 public static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V> map) { 313 if ((map instanceof ImmutableMap) && !(map instanceof ImmutableSortedMap)) { 314 // TODO(lowasser): Make ImmutableMap.copyOf(immutableBiMap) call copyOf() 315 // on the ImmutableMap delegate(), rather than the bimap itself 316 317 @SuppressWarnings("unchecked") // safe since map is not writable 318 ImmutableMap<K, V> kvMap = (ImmutableMap<K, V>) map; 319 if (!kvMap.isPartialView()) { 320 return kvMap; 321 } 322 } else if (map instanceof EnumMap) { 323 @SuppressWarnings("unchecked") // safe since map is not writable 324 ImmutableMap<K, V> kvMap = (ImmutableMap<K, V>) copyOfEnumMap((EnumMap<?, ?>) map); 325 return kvMap; 326 } 327 return copyOf(map.entrySet()); 328 } 329 330 /** 331 * Returns an immutable map containing the specified entries. The returned 332 * map iterates over entries in the same order as the original iterable. 333 * 334 * @throws NullPointerException if any key, value, or entry is null 335 * @throws IllegalArgumentException if two entries have the same key 336 * @since 19.0 337 */ 338 @Beta 339 public static <K, V> ImmutableMap<K, V> copyOf( 340 Iterable<? extends Entry<? extends K, ? extends V>> entries) { 341 @SuppressWarnings("unchecked") // we'll only be using getKey and getValue, which are covariant 342 Entry<K, V>[] entryArray = (Entry<K, V>[]) Iterables.toArray(entries, EMPTY_ENTRY_ARRAY); 343 switch (entryArray.length) { 344 case 0: 345 return of(); 346 case 1: 347 Entry<K, V> onlyEntry = entryArray[0]; 348 return of(onlyEntry.getKey(), onlyEntry.getValue()); 349 default: 350 /* 351 * The current implementation will end up using entryArray directly, though it will write 352 * over the (arbitrary, potentially mutable) Entry objects actually stored in entryArray. 353 */ 354 return RegularImmutableMap.fromEntries(entryArray); 355 } 356 } 357 358 private static <K extends Enum<K>, V> ImmutableMap<K, V> copyOfEnumMap( 359 EnumMap<K, ? extends V> original) { 360 EnumMap<K, V> copy = new EnumMap<K, V>(original); 361 for (Map.Entry<?, ?> entry : copy.entrySet()) { 362 checkEntryNotNull(entry.getKey(), entry.getValue()); 363 } 364 return ImmutableEnumMap.asImmutable(copy); 365 } 366 367 static final Entry<?, ?>[] EMPTY_ENTRY_ARRAY = new Entry<?, ?>[0]; 368 369 abstract static class IteratorBasedImmutableMap<K, V> extends ImmutableMap<K, V> { 370 abstract UnmodifiableIterator<Entry<K, V>> entryIterator(); 371 372 @Override 373 ImmutableSet<Entry<K, V>> createEntrySet() { 374 @WeakOuter 375 class EntrySetImpl extends ImmutableMapEntrySet<K, V> { 376 @Override 377 ImmutableMap<K, V> map() { 378 return IteratorBasedImmutableMap.this; 379 } 380 381 @Override 382 public UnmodifiableIterator<Entry<K, V>> iterator() { 383 return entryIterator(); 384 } 385 } 386 return new EntrySetImpl(); 387 } 388 } 389 390 ImmutableMap() {} 391 392 /** 393 * Guaranteed to throw an exception and leave the map unmodified. 394 * 395 * @throws UnsupportedOperationException always 396 * @deprecated Unsupported operation. 397 */ 398 @Deprecated 399 @Override 400 public final V put(K k, V v) { 401 throw new UnsupportedOperationException(); 402 } 403 404 /** 405 * Guaranteed to throw an exception and leave the map unmodified. 406 * 407 * @throws UnsupportedOperationException always 408 * @deprecated Unsupported operation. 409 */ 410 @Deprecated 411 @Override 412 public final V remove(Object o) { 413 throw new UnsupportedOperationException(); 414 } 415 416 /** 417 * Guaranteed to throw an exception and leave the map unmodified. 418 * 419 * @throws UnsupportedOperationException always 420 * @deprecated Unsupported operation. 421 */ 422 @Deprecated 423 @Override 424 public final void putAll(Map<? extends K, ? extends V> map) { 425 throw new UnsupportedOperationException(); 426 } 427 428 /** 429 * Guaranteed to throw an exception and leave the map unmodified. 430 * 431 * @throws UnsupportedOperationException always 432 * @deprecated Unsupported operation. 433 */ 434 @Deprecated 435 @Override 436 public final void clear() { 437 throw new UnsupportedOperationException(); 438 } 439 440 @Override 441 public boolean isEmpty() { 442 return size() == 0; 443 } 444 445 @Override 446 public boolean containsKey(@Nullable Object key) { 447 return get(key) != null; 448 } 449 450 @Override 451 public boolean containsValue(@Nullable Object value) { 452 return values().contains(value); 453 } 454 455 // Overriding to mark it Nullable 456 @Override 457 public abstract V get(@Nullable Object key); 458 459 private transient ImmutableSet<Entry<K, V>> entrySet; 460 461 /** 462 * Returns an immutable set of the mappings in this map. The entries are in 463 * the same order as the parameters used to build this map. 464 */ 465 @Override 466 public ImmutableSet<Entry<K, V>> entrySet() { 467 ImmutableSet<Entry<K, V>> result = entrySet; 468 return (result == null) ? entrySet = createEntrySet() : result; 469 } 470 471 abstract ImmutableSet<Entry<K, V>> createEntrySet(); 472 473 private transient ImmutableSet<K> keySet; 474 475 /** 476 * Returns an immutable set of the keys in this map. These keys are in 477 * the same order as the parameters used to build this map. 478 */ 479 @Override 480 public ImmutableSet<K> keySet() { 481 ImmutableSet<K> result = keySet; 482 return (result == null) ? keySet = createKeySet() : result; 483 } 484 485 ImmutableSet<K> createKeySet() { 486 return isEmpty() ? ImmutableSet.<K>of() : new ImmutableMapKeySet<K, V>(this); 487 } 488 489 UnmodifiableIterator<K> keyIterator() { 490 final UnmodifiableIterator<Entry<K, V>> entryIterator = entrySet().iterator(); 491 return new UnmodifiableIterator<K>() { 492 @Override 493 public boolean hasNext() { 494 return entryIterator.hasNext(); 495 } 496 497 @Override 498 public K next() { 499 return entryIterator.next().getKey(); 500 } 501 }; 502 } 503 504 private transient ImmutableCollection<V> values; 505 506 /** 507 * Returns an immutable collection of the values in this map. The values are 508 * in the same order as the parameters used to build this map. 509 */ 510 @Override 511 public ImmutableCollection<V> values() { 512 ImmutableCollection<V> result = values; 513 return (result == null) ? values = new ImmutableMapValues<K, V>(this) : result; 514 } 515 516 // cached so that this.multimapView().inverse() only computes inverse once 517 private transient ImmutableSetMultimap<K, V> multimapView; 518 519 /** 520 * Returns a multimap view of the map. 521 * 522 * @since 14.0 523 */ 524 @Beta 525 public ImmutableSetMultimap<K, V> asMultimap() { 526 if (isEmpty()) { 527 return ImmutableSetMultimap.of(); 528 } 529 ImmutableSetMultimap<K, V> result = multimapView; 530 return (result == null) 531 ? (multimapView = 532 new ImmutableSetMultimap<K, V>(new MapViewOfValuesAsSingletonSets(), size(), null)) 533 : result; 534 } 535 536 @WeakOuter 537 private final class MapViewOfValuesAsSingletonSets 538 extends IteratorBasedImmutableMap<K, ImmutableSet<V>> { 539 540 @Override 541 public int size() { 542 return ImmutableMap.this.size(); 543 } 544 545 @Override 546 public ImmutableSet<K> keySet() { 547 return ImmutableMap.this.keySet(); 548 } 549 550 @Override 551 public boolean containsKey(@Nullable Object key) { 552 return ImmutableMap.this.containsKey(key); 553 } 554 555 @Override 556 public ImmutableSet<V> get(@Nullable Object key) { 557 V outerValue = ImmutableMap.this.get(key); 558 return (outerValue == null) ? null : ImmutableSet.of(outerValue); 559 } 560 561 @Override 562 boolean isPartialView() { 563 return ImmutableMap.this.isPartialView(); 564 } 565 566 @Override 567 public int hashCode() { 568 // ImmutableSet.of(value).hashCode() == value.hashCode(), so the hashes are the same 569 return ImmutableMap.this.hashCode(); 570 } 571 572 @Override 573 boolean isHashCodeFast() { 574 return ImmutableMap.this.isHashCodeFast(); 575 } 576 577 @Override 578 UnmodifiableIterator<Entry<K, ImmutableSet<V>>> entryIterator() { 579 final Iterator<Entry<K, V>> backingIterator = ImmutableMap.this.entrySet().iterator(); 580 return new UnmodifiableIterator<Entry<K, ImmutableSet<V>>>() { 581 @Override 582 public boolean hasNext() { 583 return backingIterator.hasNext(); 584 } 585 586 @Override 587 public Entry<K, ImmutableSet<V>> next() { 588 final Entry<K, V> backingEntry = backingIterator.next(); 589 return new AbstractMapEntry<K, ImmutableSet<V>>() { 590 @Override 591 public K getKey() { 592 return backingEntry.getKey(); 593 } 594 595 @Override 596 public ImmutableSet<V> getValue() { 597 return ImmutableSet.of(backingEntry.getValue()); 598 } 599 }; 600 } 601 }; 602 } 603 } 604 605 @Override 606 public boolean equals(@Nullable Object object) { 607 return Maps.equalsImpl(this, object); 608 } 609 610 abstract boolean isPartialView(); 611 612 @Override 613 public int hashCode() { 614 return Sets.hashCodeImpl(entrySet()); 615 } 616 617 boolean isHashCodeFast() { 618 return false; 619 } 620 621 @Override 622 public String toString() { 623 return Maps.toStringImpl(this); 624 } 625 626 /** 627 * Serialized type for all ImmutableMap instances. It captures the logical 628 * contents and they are reconstructed using public factory methods. This 629 * ensures that the implementation types remain as implementation details. 630 */ 631 static class SerializedForm implements Serializable { 632 private final Object[] keys; 633 private final Object[] values; 634 635 SerializedForm(ImmutableMap<?, ?> map) { 636 keys = new Object[map.size()]; 637 values = new Object[map.size()]; 638 int i = 0; 639 for (Entry<?, ?> entry : map.entrySet()) { 640 keys[i] = entry.getKey(); 641 values[i] = entry.getValue(); 642 i++; 643 } 644 } 645 646 Object readResolve() { 647 Builder<Object, Object> builder = new Builder<Object, Object>(keys.length); 648 return createMap(builder); 649 } 650 651 Object createMap(Builder<Object, Object> builder) { 652 for (int i = 0; i < keys.length; i++) { 653 builder.put(keys[i], values[i]); 654 } 655 return builder.build(); 656 } 657 658 private static final long serialVersionUID = 0; 659 } 660 661 Object writeReplace() { 662 return new SerializedForm(this); 663 } 664}