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.collect.CollectPreconditions.checkEntryNotNull; 021 022import com.google.common.annotations.Beta; 023import com.google.common.annotations.GwtCompatible; 024import com.google.common.annotations.GwtIncompatible; 025import com.google.errorprone.annotations.CanIgnoreReturnValue; 026import com.google.j2objc.annotations.Weak; 027import com.google.j2objc.annotations.WeakOuter; 028import java.io.Serializable; 029import java.util.ArrayList; 030import java.util.Arrays; 031import java.util.Collection; 032import java.util.Comparator; 033import java.util.Iterator; 034import java.util.Map; 035import java.util.Map.Entry; 036import java.util.Set; 037import org.checkerframework.checker.nullness.compatqual.NullableDecl; 038 039/** 040 * A {@link Multimap} whose contents will never change, with many other important properties 041 * detailed at {@link ImmutableCollection}. 042 * 043 * <p><b>Warning:</b> avoid <i>direct</i> usage of {@link ImmutableMultimap} as a type (as with 044 * {@link Multimap} itself). Prefer subtypes such as {@link ImmutableSetMultimap} or {@link 045 * ImmutableListMultimap}, which have well-defined {@link #equals} semantics, thus avoiding a common 046 * source of bugs and confusion. 047 * 048 * <p><b>Note:</b> every {@link ImmutableMultimap} offers an {@link #inverse} view, so there is no 049 * need for a distinct {@code ImmutableBiMultimap} type. 050 * 051 * <p><a name="iteration"></a> 052 * 053 * <p><b>Key-grouped iteration.</b> All view collections follow the same iteration order. In all 054 * current implementations, the iteration order always keeps multiple entries with the same key 055 * together. Any creation method that would customarily respect insertion order (such as {@link 056 * #copyOf(Multimap)}) instead preserves key-grouped order by inserting entries for an existing key 057 * immediately after the last entry having that key. 058 * 059 * <p>See the Guava User Guide article on <a href= 060 * "https://github.com/google/guava/wiki/ImmutableCollectionsExplained"> immutable collections</a>. 061 * 062 * @author Jared Levy 063 * @since 2.0 064 */ 065@GwtCompatible(emulated = true) 066public abstract class ImmutableMultimap<K, V> extends AbstractMultimap<K, V> 067 implements Serializable { 068 069 /** Returns an empty multimap. */ 070 public static <K, V> ImmutableMultimap<K, V> of() { 071 return ImmutableListMultimap.of(); 072 } 073 074 /** Returns an immutable multimap containing a single entry. */ 075 public static <K, V> ImmutableMultimap<K, V> of(K k1, V v1) { 076 return ImmutableListMultimap.of(k1, v1); 077 } 078 079 /** Returns an immutable multimap containing the given entries, in order. */ 080 public static <K, V> ImmutableMultimap<K, V> of(K k1, V v1, K k2, V v2) { 081 return ImmutableListMultimap.of(k1, v1, k2, v2); 082 } 083 084 /** 085 * Returns an immutable multimap containing the given entries, in the "key-grouped" insertion 086 * order described in the <a href="#iteration">class documentation</a>. 087 */ 088 public static <K, V> ImmutableMultimap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) { 089 return ImmutableListMultimap.of(k1, v1, k2, v2, k3, v3); 090 } 091 092 /** 093 * Returns an immutable multimap containing the given entries, in the "key-grouped" insertion 094 * order described in the <a href="#iteration">class documentation</a>. 095 */ 096 public static <K, V> ImmutableMultimap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { 097 return ImmutableListMultimap.of(k1, v1, k2, v2, k3, v3, k4, v4); 098 } 099 100 /** 101 * Returns an immutable multimap containing the given entries, in the "key-grouped" insertion 102 * order described in the <a href="#iteration">class documentation</a>. 103 */ 104 public static <K, V> ImmutableMultimap<K, V> of( 105 K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { 106 return ImmutableListMultimap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); 107 } 108 109 // looking for of() with > 5 entries? Use the builder instead. 110 111 /** 112 * Returns a new builder. The generated builder is equivalent to the builder created by the {@link 113 * Builder} constructor. 114 */ 115 public static <K, V> Builder<K, V> builder() { 116 return new Builder<>(); 117 } 118 119 /** 120 * A builder for creating immutable multimap instances, especially {@code public static final} 121 * multimaps ("constant multimaps"). Example: 122 * 123 * <pre>{@code 124 * static final Multimap<String, Integer> STRING_TO_INTEGER_MULTIMAP = 125 * new ImmutableMultimap.Builder<String, Integer>() 126 * .put("one", 1) 127 * .putAll("several", 1, 2, 3) 128 * .putAll("many", 1, 2, 3, 4, 5) 129 * .build(); 130 * }</pre> 131 * 132 * <p>Builder instances can be reused; it is safe to call {@link #build} multiple times to build 133 * multiple multimaps in series. Each multimap contains the key-value mappings in the previously 134 * created multimaps. 135 * 136 * @since 2.0 137 */ 138 public static class Builder<K, V> { 139 Map<K, Collection<V>> builderMap; 140 Comparator<? super K> keyComparator; 141 Comparator<? super V> valueComparator; 142 143 /** 144 * Creates a new builder. The returned builder is equivalent to the builder generated by {@link 145 * ImmutableMultimap#builder}. 146 */ 147 public Builder() { 148 this.builderMap = Platform.preservesInsertionOrderOnPutsMap(); 149 } 150 151 Collection<V> newMutableValueCollection() { 152 return new ArrayList<>(); 153 } 154 155 /** Adds a key-value mapping to the built multimap. */ 156 @CanIgnoreReturnValue 157 public Builder<K, V> put(K key, V value) { 158 checkEntryNotNull(key, value); 159 Collection<V> valueCollection = builderMap.get(key); 160 if (valueCollection == null) { 161 builderMap.put(key, valueCollection = newMutableValueCollection()); 162 } 163 valueCollection.add(value); 164 return this; 165 } 166 167 /** 168 * Adds an entry to the built multimap. 169 * 170 * @since 11.0 171 */ 172 @CanIgnoreReturnValue 173 public Builder<K, V> put(Entry<? extends K, ? extends V> entry) { 174 return put(entry.getKey(), entry.getValue()); 175 } 176 177 /** 178 * Adds entries to the built multimap. 179 * 180 * @since 19.0 181 */ 182 @CanIgnoreReturnValue 183 @Beta 184 public Builder<K, V> putAll(Iterable<? extends Entry<? extends K, ? extends V>> entries) { 185 for (Entry<? extends K, ? extends V> entry : entries) { 186 put(entry); 187 } 188 return this; 189 } 190 191 /** 192 * Stores a collection of values with the same key in the built multimap. 193 * 194 * @throws NullPointerException if {@code key}, {@code values}, or any element in {@code values} 195 * is null. The builder is left in an invalid state. 196 */ 197 @CanIgnoreReturnValue 198 public Builder<K, V> putAll(K key, Iterable<? extends V> values) { 199 if (key == null) { 200 throw new NullPointerException("null key in entry: null=" + Iterables.toString(values)); 201 } 202 Collection<V> valueCollection = builderMap.get(key); 203 if (valueCollection != null) { 204 for (V value : values) { 205 checkEntryNotNull(key, value); 206 valueCollection.add(value); 207 } 208 return this; 209 } 210 Iterator<? extends V> valuesItr = values.iterator(); 211 if (!valuesItr.hasNext()) { 212 return this; 213 } 214 valueCollection = newMutableValueCollection(); 215 while (valuesItr.hasNext()) { 216 V value = valuesItr.next(); 217 checkEntryNotNull(key, value); 218 valueCollection.add(value); 219 } 220 builderMap.put(key, valueCollection); 221 return this; 222 } 223 224 /** 225 * Stores an array of values with the same key in the built multimap. 226 * 227 * @throws NullPointerException if the key or any value is null. The builder is left in an 228 * invalid state. 229 */ 230 @CanIgnoreReturnValue 231 public Builder<K, V> putAll(K key, V... values) { 232 return putAll(key, Arrays.asList(values)); 233 } 234 235 /** 236 * Stores another multimap's entries in the built multimap. The generated multimap's key and 237 * value orderings correspond to the iteration ordering of the {@code multimap.asMap()} view, 238 * with new keys and values following any existing keys and values. 239 * 240 * @throws NullPointerException if any key or value in {@code multimap} is null. The builder is 241 * left in an invalid state. 242 */ 243 @CanIgnoreReturnValue 244 public Builder<K, V> putAll(Multimap<? extends K, ? extends V> multimap) { 245 for (Entry<? extends K, ? extends Collection<? extends V>> entry : 246 multimap.asMap().entrySet()) { 247 putAll(entry.getKey(), entry.getValue()); 248 } 249 return this; 250 } 251 252 /** 253 * Specifies the ordering of the generated multimap's keys. 254 * 255 * @since 8.0 256 */ 257 @CanIgnoreReturnValue 258 public Builder<K, V> orderKeysBy(Comparator<? super K> keyComparator) { 259 this.keyComparator = checkNotNull(keyComparator); 260 return this; 261 } 262 263 /** 264 * Specifies the ordering of the generated multimap's values for each key. 265 * 266 * @since 8.0 267 */ 268 @CanIgnoreReturnValue 269 public Builder<K, V> orderValuesBy(Comparator<? super V> valueComparator) { 270 this.valueComparator = checkNotNull(valueComparator); 271 return this; 272 } 273 274 @CanIgnoreReturnValue 275 Builder<K, V> combine(Builder<K, V> other) { 276 for (Map.Entry<K, Collection<V>> entry : other.builderMap.entrySet()) { 277 putAll(entry.getKey(), entry.getValue()); 278 } 279 return this; 280 } 281 282 /** Returns a newly-created immutable multimap. */ 283 public ImmutableMultimap<K, V> build() { 284 Collection<Map.Entry<K, Collection<V>>> mapEntries = builderMap.entrySet(); 285 if (keyComparator != null) { 286 mapEntries = Ordering.from(keyComparator).<K>onKeys().immutableSortedCopy(mapEntries); 287 } 288 return ImmutableListMultimap.fromMapEntries(mapEntries, valueComparator); 289 } 290 } 291 292 /** 293 * Returns an immutable multimap containing the same mappings as {@code multimap}, in the 294 * "key-grouped" iteration order described in the class documentation. 295 * 296 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is 297 * safe to do so. The exact circumstances under which a copy will or will not be performed are 298 * undocumented and subject to change. 299 * 300 * @throws NullPointerException if any key or value in {@code multimap} is null 301 */ 302 public static <K, V> ImmutableMultimap<K, V> copyOf(Multimap<? extends K, ? extends V> multimap) { 303 if (multimap instanceof ImmutableMultimap) { 304 @SuppressWarnings("unchecked") // safe since multimap is not writable 305 ImmutableMultimap<K, V> kvMultimap = (ImmutableMultimap<K, V>) multimap; 306 if (!kvMultimap.isPartialView()) { 307 return kvMultimap; 308 } 309 } 310 return ImmutableListMultimap.copyOf(multimap); 311 } 312 313 /** 314 * Returns an immutable multimap containing the specified entries. The returned multimap iterates 315 * over keys in the order they were first encountered in the input, and the values for each key 316 * are iterated in the order they were encountered. 317 * 318 * @throws NullPointerException if any key, value, or entry is null 319 * @since 19.0 320 */ 321 @Beta 322 public static <K, V> ImmutableMultimap<K, V> copyOf( 323 Iterable<? extends Entry<? extends K, ? extends V>> entries) { 324 return ImmutableListMultimap.copyOf(entries); 325 } 326 327 final transient ImmutableMap<K, ? extends ImmutableCollection<V>> map; 328 final transient int size; 329 330 // These constants allow the deserialization code to set final fields. This 331 // holder class makes sure they are not initialized unless an instance is 332 // deserialized. 333 @GwtIncompatible // java serialization is not supported 334 static class FieldSettersHolder { 335 static final Serialization.FieldSetter<ImmutableMultimap> MAP_FIELD_SETTER = 336 Serialization.getFieldSetter(ImmutableMultimap.class, "map"); 337 static final Serialization.FieldSetter<ImmutableMultimap> SIZE_FIELD_SETTER = 338 Serialization.getFieldSetter(ImmutableMultimap.class, "size"); 339 } 340 341 ImmutableMultimap(ImmutableMap<K, ? extends ImmutableCollection<V>> map, int size) { 342 this.map = map; 343 this.size = size; 344 } 345 346 // mutators (not supported) 347 348 /** 349 * Guaranteed to throw an exception and leave the multimap unmodified. 350 * 351 * @throws UnsupportedOperationException always 352 * @deprecated Unsupported operation. 353 */ 354 @CanIgnoreReturnValue 355 @Deprecated 356 @Override 357 public ImmutableCollection<V> removeAll(Object key) { 358 throw new UnsupportedOperationException(); 359 } 360 361 /** 362 * Guaranteed to throw an exception and leave the multimap unmodified. 363 * 364 * @throws UnsupportedOperationException always 365 * @deprecated Unsupported operation. 366 */ 367 @CanIgnoreReturnValue 368 @Deprecated 369 @Override 370 public ImmutableCollection<V> replaceValues(K key, Iterable<? extends V> values) { 371 throw new UnsupportedOperationException(); 372 } 373 374 /** 375 * Guaranteed to throw an exception and leave the multimap unmodified. 376 * 377 * @throws UnsupportedOperationException always 378 * @deprecated Unsupported operation. 379 */ 380 @Deprecated 381 @Override 382 public void clear() { 383 throw new UnsupportedOperationException(); 384 } 385 386 /** 387 * Returns an immutable collection of the values for the given key. If no mappings in the multimap 388 * have the provided key, an empty immutable collection is returned. The values are in the same 389 * order as the parameters used to build this multimap. 390 */ 391 @Override 392 public abstract ImmutableCollection<V> get(K key); 393 394 /** 395 * Returns an immutable multimap which is the inverse of this one. For every key-value mapping in 396 * the original, the result will have a mapping with key and value reversed. 397 * 398 * @since 11.0 399 */ 400 public abstract ImmutableMultimap<V, K> inverse(); 401 402 /** 403 * Guaranteed to throw an exception and leave the multimap unmodified. 404 * 405 * @throws UnsupportedOperationException always 406 * @deprecated Unsupported operation. 407 */ 408 @CanIgnoreReturnValue 409 @Deprecated 410 @Override 411 public boolean put(K key, V value) { 412 throw new UnsupportedOperationException(); 413 } 414 415 /** 416 * Guaranteed to throw an exception and leave the multimap unmodified. 417 * 418 * @throws UnsupportedOperationException always 419 * @deprecated Unsupported operation. 420 */ 421 @CanIgnoreReturnValue 422 @Deprecated 423 @Override 424 public boolean putAll(K key, Iterable<? extends V> values) { 425 throw new UnsupportedOperationException(); 426 } 427 428 /** 429 * Guaranteed to throw an exception and leave the multimap unmodified. 430 * 431 * @throws UnsupportedOperationException always 432 * @deprecated Unsupported operation. 433 */ 434 @CanIgnoreReturnValue 435 @Deprecated 436 @Override 437 public boolean putAll(Multimap<? extends K, ? extends V> multimap) { 438 throw new UnsupportedOperationException(); 439 } 440 441 /** 442 * Guaranteed to throw an exception and leave the multimap unmodified. 443 * 444 * @throws UnsupportedOperationException always 445 * @deprecated Unsupported operation. 446 */ 447 @CanIgnoreReturnValue 448 @Deprecated 449 @Override 450 public boolean remove(Object key, Object value) { 451 throw new UnsupportedOperationException(); 452 } 453 454 /** 455 * Returns {@code true} if this immutable multimap's implementation contains references to 456 * user-created objects that aren't accessible via this multimap's methods. This is generally used 457 * to determine whether {@code copyOf} implementations should make an explicit copy to avoid 458 * memory leaks. 459 */ 460 boolean isPartialView() { 461 return map.isPartialView(); 462 } 463 464 // accessors 465 466 @Override 467 public boolean containsKey(@NullableDecl Object key) { 468 return map.containsKey(key); 469 } 470 471 @Override 472 public boolean containsValue(@NullableDecl Object value) { 473 return value != null && super.containsValue(value); 474 } 475 476 @Override 477 public int size() { 478 return size; 479 } 480 481 // views 482 483 /** 484 * Returns an immutable set of the distinct keys in this multimap, in the same order as they 485 * appear in this multimap. 486 */ 487 @Override 488 public ImmutableSet<K> keySet() { 489 return map.keySet(); 490 } 491 492 @Override 493 Set<K> createKeySet() { 494 throw new AssertionError("unreachable"); 495 } 496 497 /** 498 * Returns an immutable map that associates each key with its corresponding values in the 499 * multimap. Keys and values appear in the same order as in this multimap. 500 */ 501 @Override 502 @SuppressWarnings("unchecked") // a widening cast 503 public ImmutableMap<K, Collection<V>> asMap() { 504 return (ImmutableMap) map; 505 } 506 507 @Override 508 Map<K, Collection<V>> createAsMap() { 509 throw new AssertionError("should never be called"); 510 } 511 512 /** Returns an immutable collection of all key-value pairs in the multimap. */ 513 @Override 514 public ImmutableCollection<Entry<K, V>> entries() { 515 return (ImmutableCollection<Entry<K, V>>) super.entries(); 516 } 517 518 @Override 519 ImmutableCollection<Entry<K, V>> createEntries() { 520 return new EntryCollection<>(this); 521 } 522 523 private static class EntryCollection<K, V> extends ImmutableCollection<Entry<K, V>> { 524 @Weak final ImmutableMultimap<K, V> multimap; 525 526 EntryCollection(ImmutableMultimap<K, V> multimap) { 527 this.multimap = multimap; 528 } 529 530 @Override 531 public UnmodifiableIterator<Entry<K, V>> iterator() { 532 return multimap.entryIterator(); 533 } 534 535 @Override 536 boolean isPartialView() { 537 return multimap.isPartialView(); 538 } 539 540 @Override 541 public int size() { 542 return multimap.size(); 543 } 544 545 @Override 546 public boolean contains(Object object) { 547 if (object instanceof Entry) { 548 Entry<?, ?> entry = (Entry<?, ?>) object; 549 return multimap.containsEntry(entry.getKey(), entry.getValue()); 550 } 551 return false; 552 } 553 554 private static final long serialVersionUID = 0; 555 } 556 557 @Override 558 UnmodifiableIterator<Entry<K, V>> entryIterator() { 559 return new UnmodifiableIterator<Entry<K, V>>() { 560 final Iterator<? extends Entry<K, ? extends ImmutableCollection<V>>> asMapItr = 561 map.entrySet().iterator(); 562 K currentKey = null; 563 Iterator<V> valueItr = Iterators.emptyIterator(); 564 565 @Override 566 public boolean hasNext() { 567 return valueItr.hasNext() || asMapItr.hasNext(); 568 } 569 570 @Override 571 public Entry<K, V> next() { 572 if (!valueItr.hasNext()) { 573 Entry<K, ? extends ImmutableCollection<V>> entry = asMapItr.next(); 574 currentKey = entry.getKey(); 575 valueItr = entry.getValue().iterator(); 576 } 577 return Maps.immutableEntry(currentKey, valueItr.next()); 578 } 579 }; 580 } 581 582 /** 583 * Returns an immutable multiset containing all the keys in this multimap, in the same order and 584 * with the same frequencies as they appear in this multimap; to get only a single occurrence of 585 * each key, use {@link #keySet}. 586 */ 587 @Override 588 public ImmutableMultiset<K> keys() { 589 return (ImmutableMultiset<K>) super.keys(); 590 } 591 592 @Override 593 ImmutableMultiset<K> createKeys() { 594 return new Keys(); 595 } 596 597 @SuppressWarnings("serial") // Uses writeReplace, not default serialization 598 @WeakOuter 599 class Keys extends ImmutableMultiset<K> { 600 @Override 601 public boolean contains(@NullableDecl Object object) { 602 return containsKey(object); 603 } 604 605 @Override 606 public int count(@NullableDecl Object element) { 607 Collection<V> values = map.get(element); 608 return (values == null) ? 0 : values.size(); 609 } 610 611 @Override 612 public ImmutableSet<K> elementSet() { 613 return keySet(); 614 } 615 616 @Override 617 public int size() { 618 return ImmutableMultimap.this.size(); 619 } 620 621 @Override 622 Multiset.Entry<K> getEntry(int index) { 623 Map.Entry<K, ? extends Collection<V>> entry = map.entrySet().asList().get(index); 624 return Multisets.immutableEntry(entry.getKey(), entry.getValue().size()); 625 } 626 627 @Override 628 boolean isPartialView() { 629 return true; 630 } 631 632 // We can't label this with @Override, because it doesn't override anything 633 // in the GWT emulated version. 634 Object writeReplace() { 635 return new KeysSerializedForm(ImmutableMultimap.this); 636 } 637 } 638 639 private static final class KeysSerializedForm implements Serializable { 640 final ImmutableMultimap<?, ?> multimap; 641 642 KeysSerializedForm(ImmutableMultimap<?, ?> multimap) { 643 this.multimap = multimap; 644 } 645 646 Object readResolve() { 647 return multimap.keys(); 648 } 649 } 650 651 /** 652 * Returns an immutable collection of the values in this multimap. Its iterator traverses the 653 * values for the first key, the values for the second key, and so on. 654 */ 655 @Override 656 public ImmutableCollection<V> values() { 657 return (ImmutableCollection<V>) super.values(); 658 } 659 660 @Override 661 ImmutableCollection<V> createValues() { 662 return new Values<>(this); 663 } 664 665 @Override 666 UnmodifiableIterator<V> valueIterator() { 667 return new UnmodifiableIterator<V>() { 668 Iterator<? extends ImmutableCollection<V>> valueCollectionItr = map.values().iterator(); 669 Iterator<V> valueItr = Iterators.emptyIterator(); 670 671 @Override 672 public boolean hasNext() { 673 return valueItr.hasNext() || valueCollectionItr.hasNext(); 674 } 675 676 @Override 677 public V next() { 678 if (!valueItr.hasNext()) { 679 valueItr = valueCollectionItr.next().iterator(); 680 } 681 return valueItr.next(); 682 } 683 }; 684 } 685 686 private static final class Values<K, V> extends ImmutableCollection<V> { 687 @Weak private final transient ImmutableMultimap<K, V> multimap; 688 689 Values(ImmutableMultimap<K, V> multimap) { 690 this.multimap = multimap; 691 } 692 693 @Override 694 public boolean contains(@NullableDecl Object object) { 695 return multimap.containsValue(object); 696 } 697 698 @Override 699 public UnmodifiableIterator<V> iterator() { 700 return multimap.valueIterator(); 701 } 702 703 @GwtIncompatible // not present in emulated superclass 704 @Override 705 int copyIntoArray(Object[] dst, int offset) { 706 for (ImmutableCollection<V> valueCollection : multimap.map.values()) { 707 offset = valueCollection.copyIntoArray(dst, offset); 708 } 709 return offset; 710 } 711 712 @Override 713 public int size() { 714 return multimap.size(); 715 } 716 717 @Override 718 boolean isPartialView() { 719 return true; 720 } 721 722 private static final long serialVersionUID = 0; 723 } 724 725 private static final long serialVersionUID = 0; 726}