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;
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection;
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator;
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Map.Entry;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * An immutable {@link ListMultimap} with reliable user-specified key and value
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iteration order. Does not permit null keys or values.
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Unlike {@link Multimaps#unmodifiableListMultimap(ListMultimap)}, which is
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a <i>view</i> of a separate multimap which can still change, an instance of
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code ImmutableListMultimap} contains its own data and will <i>never</i>
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * change. {@code ImmutableListMultimap} is convenient for
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code public static final} multimaps ("constant multimaps") and also lets
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you easily make a "defensive copy" of a multimap provided to your class by
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * a caller.
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Note:</b> Although this class is not final, it cannot be subclassed as
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * it has no public or protected constructors. Thus, instances of this class
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are guaranteed to be immutable.
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(serializable = true, emulated = true)
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic class ImmutableListMultimap<K, V>
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    extends ImmutableMultimap<K, V>
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    implements ListMultimap<K, V> {
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** Returns the empty multimap. */
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting is safe because the multimap will never hold any elements.
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of() {
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (ImmutableListMultimap<K, V>) EmptyImmutableListMultimap.INSTANCE;
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing a single entry.
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(K k1, V v1) {
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing the given entries, in order.
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(K k1, V v1, K k2, V v2) {
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k2, v2);
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing the given entries, in order.
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3) {
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k2, v2);
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k3, v3);
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing the given entries, in order.
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k2, v2);
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k3, v3);
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k4, v4);
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable multimap containing the given entries, in order.
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> of(
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableListMultimap.Builder<K, V> builder
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        = ImmutableListMultimap.builder();
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k1, v1);
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k2, v2);
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k3, v3);
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k4, v4);
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.put(k5, v5);
119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // looking for of() with > 5 entries? Use the builder instead.
123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a new builder. The generated builder is equivalent to the builder
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * created by the {@link Builder} constructor.
127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> Builder<K, V> builder() {
129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Builder<K, V>();
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * A builder for creating immutable {@code ListMultimap} instances, especially
134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code public static final} multimaps ("constant multimaps"). Example:
135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <pre>   {@code
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   static final Multimap<String, Integer> STRING_TO_INTEGER_MULTIMAP =
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *       new ImmutableListMultimap.Builder<String, Integer>()
139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .put("one", 1)
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .putAll("several", 1, 2, 3)
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .putAll("many", 1, 2, 3, 4, 5)
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *           .build();}</pre>
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Builder instances can be reused; it is safe to call {@link #build} multiple
1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * times to build multiple multimaps in series. Each multimap contains the
1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * key-value mappings in the previously created multimaps.
1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0 (imported from Google Collections Library)
149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static final class Builder<K, V>
151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      extends ImmutableMultimap.Builder<K, V> {
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * Creates a new builder. The returned builder is equivalent to the builder
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     * generated by {@link ImmutableListMultimap#builder}.
155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Builder() {}
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Builder<K, V> put(K key, V value) {
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super.put(key, value);
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@inheritDoc}
165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * @since 11.0
167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override public Builder<K, V> put(
1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Entry<? extends K, ? extends V> entry) {
1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.put(entry);
1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return this;
1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Builder<K, V> putAll(K key, Iterable<? extends V> values) {
175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      super.putAll(key, values);
176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override public Builder<K, V> putAll(K key, V... values) {
1801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.putAll(key, values);
1811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return this;
1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override public Builder<K, V> putAll(
1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Multimap<? extends K, ? extends V> multimap) {
1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.putAll(multimap);
1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return this;
1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@inheritDoc}
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * @since 8.0
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Beta @Override
1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public Builder<K, V> orderKeysBy(Comparator<? super K> keyComparator) {
1971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.orderKeysBy(keyComparator);
198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * {@inheritDoc}
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     *
2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * @since 8.0
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Beta @Override
2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public Builder<K, V> orderValuesBy(Comparator<? super V> valueComparator) {
2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      super.orderValuesBy(valueComparator);
209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this;
210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    /**
2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Returns a newly-created immutable list multimap.
214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson     */
215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public ImmutableListMultimap<K, V> build() {
216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return (ImmutableListMultimap<K, V>) super.build();
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns an immutable multimap containing the same mappings as {@code
2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * multimap}. The generated multimap's key and value orderings correspond to
2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the iteration ordering of the {@code multimap.asMap()} view.
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Despite the method name, this method attempts to avoid actually copying
2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the data when it is safe to do so. The exact circumstances under which a
2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * copy will or will not be performed are undocumented and subject to change.
228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NullPointerException if any key or value in {@code multimap} is
2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *         null
231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> ImmutableListMultimap<K, V> copyOf(
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Multimap<? extends K, ? extends V> multimap) {
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (multimap.isEmpty()) {
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return of();
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    // TODO(user): copy ImmutableSetMultimap by using asList() on the sets
239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (multimap instanceof ImmutableListMultimap) {
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      @SuppressWarnings("unchecked") // safe since multimap is not writable
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ImmutableListMultimap<K, V> kvMultimap
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          = (ImmutableListMultimap<K, V>) multimap;
2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (!kvMultimap.isPartialView()) {
2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return kvMultimap;
2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableMap.Builder<K, ImmutableList<V>> builder = ImmutableMap.builder();
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int size = 0;
250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (Entry<? extends K, ? extends Collection<? extends V>> entry
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        : multimap.asMap().entrySet()) {
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ImmutableList<V> list = ImmutableList.copyOf(entry.getValue());
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!list.isEmpty()) {
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        builder.put(entry.getKey(), list);
256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        size += list.size();
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new ImmutableListMultimap<K, V>(builder.build(), size);
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  ImmutableListMultimap(ImmutableMap<K, ImmutableList<V>> map, int size) {
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    super(map, size);
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // views
268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an immutable list of the values for the given key.  If no mappings
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * in the multimap have the provided key, an empty immutable list is
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * returned. The values are in the same order as the parameters used to build
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * this multimap.
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableList<V> get(@Nullable K key) {
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // This cast is safe as its type is known in constructor.
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableList<V> list = (ImmutableList<V>) map.get(key);
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (list == null) ? ImmutableList.<V>of() : list;
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private transient ImmutableListMultimap<V, K> inverse;
2821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@inheritDoc}
2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Because an inverse of a list multimap can contain multiple pairs with the same key and
2871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * value, this method returns an {@code ImmutableListMultimap} rather than the
2881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code ImmutableMultimap} specified in the {@code ImmutableMultimap} class.
2891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11
2911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public ImmutableListMultimap<V, K> inverse() {
2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    ImmutableListMultimap<V, K> result = inverse;
2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (result == null) ? (inverse = invert()) : result;
2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private ImmutableListMultimap<V, K> invert() {
2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Builder<V, K> builder = builder();
3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (Entry<K, V> entry : entries()) {
3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      builder.put(entry.getValue(), entry.getKey());
3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
3031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    ImmutableListMultimap<V, K> invertedMultimap = builder.build();
3041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    invertedMultimap.inverse = this;
3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return invertedMultimap;
3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the multimap unmodified.
310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableList<V> removeAll(Object key) {
314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Guaranteed to throw an exception and leave the multimap unmodified.
319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws UnsupportedOperationException always
321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public ImmutableList<V> replaceValues(
323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      K key, Iterable<? extends V> values) {
324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new UnsupportedOperationException();
325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
327