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    private static final long serialVersionUID = 0;
582  }
583
584  @Override
585  UnmodifiableIterator<Entry<K, V>> entryIterator() {
586    return new UnmodifiableIterator<Entry<K, V>>() {
587      final Iterator<? extends Entry<K, ? extends ImmutableCollection<V>>> asMapItr =
588          map.entrySet().iterator();
589      @CheckForNull K currentKey = null;
590      Iterator<V> valueItr = Iterators.emptyIterator();
591
592      @Override
593      public boolean hasNext() {
594        return valueItr.hasNext() || asMapItr.hasNext();
595      }
596
597      @Override
598      public Entry<K, V> next() {
599        if (!valueItr.hasNext()) {
600          Entry<K, ? extends ImmutableCollection<V>> entry = asMapItr.next();
601          currentKey = entry.getKey();
602          valueItr = entry.getValue().iterator();
603        }
604        /*
605         * requireNonNull is safe: The first call to this method always enters the !hasNext() case
606         * and populates currentKey, after which it's never cleared.
607         */
608        return immutableEntry(requireNonNull(currentKey), valueItr.next());
609      }
610    };
611  }
612
613  @Override
614  Spliterator<Entry<K, V>> entrySpliterator() {
615    return CollectSpliterators.flatMap(
616        asMap().entrySet().spliterator(),
617        keyToValueCollectionEntry -> {
618          K key = keyToValueCollectionEntry.getKey();
619          Collection<V> valueCollection = keyToValueCollectionEntry.getValue();
620          return CollectSpliterators.map(
621              valueCollection.spliterator(), (V value) -> Maps.immutableEntry(key, value));
622        },
623        Spliterator.SIZED | (this instanceof SetMultimap ? Spliterator.DISTINCT : 0),
624        size());
625  }
626
627  @Override
628  public void forEach(BiConsumer<? super K, ? super V> action) {
629    checkNotNull(action);
630    asMap()
631        .forEach(
632            (key, valueCollection) -> valueCollection.forEach(value -> action.accept(key, value)));
633  }
634
635  /**
636   * Returns an immutable multiset containing all the keys in this multimap, in the same order and
637   * with the same frequencies as they appear in this multimap; to get only a single occurrence of
638   * each key, use {@link #keySet}.
639   */
640  @Override
641  public ImmutableMultiset<K> keys() {
642    return (ImmutableMultiset<K>) super.keys();
643  }
644
645  @Override
646  ImmutableMultiset<K> createKeys() {
647    return new Keys();
648  }
649
650  @SuppressWarnings("serial") // Uses writeReplace, not default serialization
651  @WeakOuter
652  class Keys extends ImmutableMultiset<K> {
653    @Override
654    public boolean contains(@CheckForNull Object object) {
655      return containsKey(object);
656    }
657
658    @Override
659    public int count(@CheckForNull Object element) {
660      Collection<V> values = map.get(element);
661      return (values == null) ? 0 : values.size();
662    }
663
664    @Override
665    public ImmutableSet<K> elementSet() {
666      return keySet();
667    }
668
669    @Override
670    public int size() {
671      return ImmutableMultimap.this.size();
672    }
673
674    @Override
675    Multiset.Entry<K> getEntry(int index) {
676      Map.Entry<K, ? extends Collection<V>> entry = map.entrySet().asList().get(index);
677      return Multisets.immutableEntry(entry.getKey(), entry.getValue().size());
678    }
679
680    @Override
681    boolean isPartialView() {
682      return true;
683    }
684
685    @GwtIncompatible
686    @J2ktIncompatible
687    @Override
688    Object writeReplace() {
689      return new KeysSerializedForm(ImmutableMultimap.this);
690    }
691
692    @GwtIncompatible
693    @J2ktIncompatible
694    private void readObject(ObjectInputStream stream) throws InvalidObjectException {
695      throw new InvalidObjectException("Use KeysSerializedForm");
696    }
697  }
698
699  @GwtIncompatible
700  @J2ktIncompatible
701  private static final class KeysSerializedForm implements Serializable {
702    final ImmutableMultimap<?, ?> multimap;
703
704    KeysSerializedForm(ImmutableMultimap<?, ?> multimap) {
705      this.multimap = multimap;
706    }
707
708    Object readResolve() {
709      return multimap.keys();
710    }
711  }
712
713  /**
714   * Returns an immutable collection of the values in this multimap. Its iterator traverses the
715   * values for the first key, the values for the second key, and so on.
716   */
717  @Override
718  public ImmutableCollection<V> values() {
719    return (ImmutableCollection<V>) super.values();
720  }
721
722  @Override
723  ImmutableCollection<V> createValues() {
724    return new Values<>(this);
725  }
726
727  @Override
728  UnmodifiableIterator<V> valueIterator() {
729    return new UnmodifiableIterator<V>() {
730      Iterator<? extends ImmutableCollection<V>> valueCollectionItr = map.values().iterator();
731      Iterator<V> valueItr = Iterators.emptyIterator();
732
733      @Override
734      public boolean hasNext() {
735        return valueItr.hasNext() || valueCollectionItr.hasNext();
736      }
737
738      @Override
739      public V next() {
740        if (!valueItr.hasNext()) {
741          valueItr = valueCollectionItr.next().iterator();
742        }
743        return valueItr.next();
744      }
745    };
746  }
747
748  private static final class Values<K, V> extends ImmutableCollection<V> {
749    @Weak private final transient ImmutableMultimap<K, V> multimap;
750
751    Values(ImmutableMultimap<K, V> multimap) {
752      this.multimap = multimap;
753    }
754
755    @Override
756    public boolean contains(@CheckForNull Object object) {
757      return multimap.containsValue(object);
758    }
759
760    @Override
761    public UnmodifiableIterator<V> iterator() {
762      return multimap.valueIterator();
763    }
764
765    @GwtIncompatible // not present in emulated superclass
766    @Override
767    int copyIntoArray(@Nullable Object[] dst, int offset) {
768      for (ImmutableCollection<V> valueCollection : multimap.map.values()) {
769        offset = valueCollection.copyIntoArray(dst, offset);
770      }
771      return offset;
772    }
773
774    @Override
775    public int size() {
776      return multimap.size();
777    }
778
779    @Override
780    boolean isPartialView() {
781      return true;
782    }
783
784    @J2ktIncompatible // serialization
785    private static final long serialVersionUID = 0;
786  }
787
788  @J2ktIncompatible // serialization
789  private static final long serialVersionUID = 0;
790}