1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2008 The Guava Authors
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtIncompatible;
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.IOException;
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.InvalidObjectException;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.ObjectInputStream;
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.ObjectOutputStream;
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection;
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator;
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Map.Entry;
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * An immutable {@link ListMultimap} with reliable user-specified key and value
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iteration order. Does not permit null keys or values.
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Unlike {@link Multimaps#unmodifiableListMultimap(ListMultimap)}, which is
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a <i>view</i> of a separate multimap which can still change, an instance of
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code ImmutableListMultimap} contains its own data and will <i>never</i>
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * change. {@code ImmutableListMultimap} is convenient for
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code public static final} multimaps ("constant multimaps") and also lets
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you easily make a "defensive copy" of a multimap provided to your class by
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a caller.
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Note:</b> Although this class is not final, it cannot be subclassed as
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * it has no public or protected constructors. Thus, instances of this class
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are guaranteed to be immutable.
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(serializable = true, emulated = true)
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic class ImmutableListMultimap<K, V>
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    extends ImmutableMultimap<K, V>
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    implements ListMultimap<K, V> {
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** Returns the empty multimap. */
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting is safe because the multimap will never hold any elements.
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of() {
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (ImmutableListMultimap<K, V>) EmptyImmutableListMultimap.INSTANCE;
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing a single entry.
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(K k1, V v1) {
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing the given entries, in order.
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(K k1, V v1, K k2, V v2) {
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k2, v2);
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing the given entries, in order.
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3) {
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k2, v2);
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k3, v3);
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing the given entries, in order.
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k2, v2);
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k3, v3);
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k4, v4);
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing the given entries, in order.
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k2, v2);
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k3, v3);
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k4, v4);
123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k5, v5);
124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // looking for of() with > 5 entries? Use the builder instead.
128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a new builder. The generated builder is equivalent to the builder
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * created by the {@link Builder} constructor.
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> Builder<K, V> builder() {
134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Builder<K, V>();
135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * A builder for creating immutable {@code ListMultimap} instances, especially
139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code public static final} multimaps ("constant multimaps"). Example:
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <pre>   {@code
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   static final Multimap<String, Integer> STRING_TO_INTEGER_MULTIMAP =
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *       new ImmutableListMultimap.Builder<String, Integer>()
144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .put("one", 1)
145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .putAll("several", 1, 2, 3)
146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .putAll("many", 1, 2, 3, 4, 5)
147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .build();}</pre>
148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Builder instances can be reused; it is safe to call {@link #build} multiple
1501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * times to build multiple multimaps in series. Each multimap contains the
1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * key-value mappings in the previously created multimaps.
1521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0 (imported from Google Collections Library)
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static final class Builder<K, V>
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      extends ImmutableMultimap.Builder<K, V> {
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Creates a new builder. The returned builder is equivalent to the builder
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * generated by {@link ImmutableListMultimap#builder}.
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Builder() {}
162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Builder<K, V> put(K key, V value) {
164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super.put(key, value);
165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@inheritDoc}
170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * @since 11.0
172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override public Builder<K, V> put(
1741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Entry<? extends K, ? extends V> entry) {
1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.put(entry);
1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return this;
1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Builder<K, V> putAll(K key, Iterable<? extends V> values) {
180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super.putAll(key, values);
181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override public Builder<K, V> putAll(K key, V... values) {
1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.putAll(key, values);
1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return this;
1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override public Builder<K, V> putAll(
1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Multimap<? extends K, ? extends V> multimap) {
1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.putAll(multimap);
1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return this;
1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@inheritDoc}
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * @since 8.0
199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Beta @Override
2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public Builder<K, V> orderKeysBy(Comparator<? super K> keyComparator) {
2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.orderKeysBy(keyComparator);
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@inheritDoc}
208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * @since 8.0
210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Beta @Override
2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public Builder<K, V> orderValuesBy(Comparator<? super V> valueComparator) {
2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.orderValuesBy(valueComparator);
214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Returns a newly-created immutable list multimap.
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public ImmutableListMultimap<K, V> build() {
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return (ImmutableListMultimap<K, V>) super.build();
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns an immutable multimap containing the same mappings as {@code
2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * multimap}. The generated multimap's key and value orderings correspond to
2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the iteration ordering of the {@code multimap.asMap()} view.
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Despite the method name, this method attempts to avoid actually copying
2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the data when it is safe to do so. The exact circumstances under which a
2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * copy will or will not be performed are undocumented and subject to change.
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NullPointerException if any key or value in {@code multimap} is
2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         null
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> copyOf(
238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Multimap<? extends K, ? extends V> multimap) {
239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (multimap.isEmpty()) {
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return of();
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    // TODO(user): copy ImmutableSetMultimap by using asList() on the sets
244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (multimap instanceof ImmutableListMultimap) {
245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      @SuppressWarnings("unchecked") // safe since multimap is not writable
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ImmutableListMultimap<K, V> kvMultimap
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          = (ImmutableListMultimap<K, V>) multimap;
2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (!kvMultimap.isPartialView()) {
2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return kvMultimap;
2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableMap.Builder<K, ImmutableList<V>> builder = ImmutableMap.builder();
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int size = 0;
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (Entry<? extends K, ? extends Collection<? extends V>> entry
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        : multimap.asMap().entrySet()) {
258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ImmutableList<V> list = ImmutableList.copyOf(entry.getValue());
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!list.isEmpty()) {
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        builder.put(entry.getKey(), list);
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        size += list.size();
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new ImmutableListMultimap<K, V>(builder.build(), size);
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  ImmutableListMultimap(ImmutableMap<K, ImmutableList<V>> map, int size) {
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    super(map, size);
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // views
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable list of the values for the given key.  If no mappings
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * in the multimap have the provided key, an empty immutable list is
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * returned. The values are in the same order as the parameters used to build
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * this multimap.
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableList<V> get(@Nullable K key) {
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // This cast is safe as its type is known in constructor.
282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableList<V> list = (ImmutableList<V>) map.get(key);
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (list == null) ? ImmutableList.<V>of() : list;
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private transient ImmutableListMultimap<V, K> inverse;
2871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@inheritDoc}
2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Because an inverse of a list multimap can contain multiple pairs with the same key and
2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * value, this method returns an {@code ImmutableListMultimap} rather than the
2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code ImmutableMultimap} specified in the {@code ImmutableMultimap} class.
2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11
2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public ImmutableListMultimap<V, K> inverse() {
2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    ImmutableListMultimap<V, K> result = inverse;
3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (result == null) ? (inverse = invert()) : result;
3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private ImmutableListMultimap<V, K> invert() {
3041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Builder<V, K> builder = builder();
3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (Entry<K, V> entry : entries()) {
3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      builder.put(entry.getValue(), entry.getKey());
3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    ImmutableListMultimap<V, K> invertedMultimap = builder.build();
3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    invertedMultimap.inverse = this;
3101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return invertedMultimap;
3111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the multimap unmodified.
315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableList<V> removeAll(Object key) {
319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the multimap unmodified.
324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableList<V> replaceValues(
328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K key, Iterable<? extends V> values) {
329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @serialData number of distinct keys, and then for each distinct key: the
334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     key, the number of values for that key, and the key's values
335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
3361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.io.ObjectOutputStream")
337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private void writeObject(ObjectOutputStream stream) throws IOException {
338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    stream.defaultWriteObject();
339090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Serialization.writeMultimap(this, stream);
340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
3421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("java.io.ObjectInputStream")
343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private void readObject(ObjectInputStream stream)
344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      throws IOException, ClassNotFoundException {
345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    stream.defaultReadObject();
346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int keyCount = stream.readInt();
347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (keyCount < 0) {
348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      throw new InvalidObjectException("Invalid key count " + keyCount);
349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableMap.Builder<Object, ImmutableList<Object>> builder
351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableMap.builder();
352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int tmpSize = 0;
353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    for (int i = 0; i < keyCount; i++) {
355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Object key = stream.readObject();
356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      int valueCount = stream.readInt();
357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (valueCount <= 0) {
358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        throw new InvalidObjectException("Invalid value count " + valueCount);
359090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
360090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Object[] array = new Object[valueCount];
362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      for (int j = 0; j < valueCount; j++) {
363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        array[j] = stream.readObject();
364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
3651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      builder.put(key, ImmutableList.copyOf(array));
366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      tmpSize += valueCount;
367090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
369090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableMap<Object, ImmutableList<Object>> tmpMap;
370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    try {
371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      tmpMap = builder.build();
372090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    } catch (IllegalArgumentException e) {
373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      throw (InvalidObjectException)
374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          new InvalidObjectException(e.getMessage()).initCause(e);
375090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    FieldSettersHolder.MAP_FIELD_SETTER.set(this, tmpMap);
378090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    FieldSettersHolder.SIZE_FIELD_SETTER.set(this, tmpSize);
379090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
3811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("Not needed in emulated source")
382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final long serialVersionUID = 0;
383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
384