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