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