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