1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21
22 import java.io.IOException;
23 import java.io.InvalidObjectException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.util.Collection;
27 import java.util.Comparator;
28 import java.util.Map.Entry;
29
30 import javax.annotation.Nullable;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 @GwtCompatible(serializable = true, emulated = true)
56 public class ImmutableListMultimap<K, V>
57 extends ImmutableMultimap<K, V>
58 implements ListMultimap<K, V> {
59
60
61
62 @SuppressWarnings("unchecked")
63 public static <K, V> ImmutableListMultimap<K, V> of() {
64 return (ImmutableListMultimap<K, V>) EmptyImmutableListMultimap.INSTANCE;
65 }
66
67
68
69
70 public static <K, V> ImmutableListMultimap<K, V> of(K k1, V v1) {
71 ImmutableListMultimap.Builder<K, V> builder
72 = ImmutableListMultimap.builder();
73 builder.put(k1, v1);
74 return builder.build();
75 }
76
77
78
79
80 public static <K, V> ImmutableListMultimap<K, V> of(K k1, V v1, K k2, V v2) {
81 ImmutableListMultimap.Builder<K, V> builder
82 = ImmutableListMultimap.builder();
83 builder.put(k1, v1);
84 builder.put(k2, v2);
85 return builder.build();
86 }
87
88
89
90
91 public static <K, V> ImmutableListMultimap<K, V> of(
92 K k1, V v1, K k2, V v2, K k3, V v3) {
93 ImmutableListMultimap.Builder<K, V> builder
94 = ImmutableListMultimap.builder();
95 builder.put(k1, v1);
96 builder.put(k2, v2);
97 builder.put(k3, v3);
98 return builder.build();
99 }
100
101
102
103
104 public static <K, V> ImmutableListMultimap<K, V> of(
105 K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
106 ImmutableListMultimap.Builder<K, V> builder
107 = ImmutableListMultimap.builder();
108 builder.put(k1, v1);
109 builder.put(k2, v2);
110 builder.put(k3, v3);
111 builder.put(k4, v4);
112 return builder.build();
113 }
114
115
116
117
118 public static <K, V> ImmutableListMultimap<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 ImmutableListMultimap.Builder<K, V> builder
121 = ImmutableListMultimap.builder();
122 builder.put(k1, v1);
123 builder.put(k2, v2);
124 builder.put(k3, v3);
125 builder.put(k4, v4);
126 builder.put(k5, v5);
127 return builder.build();
128 }
129
130
131
132
133
134
135
136 public static <K, V> Builder<K, V> builder() {
137 return new Builder<K, V>();
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public static final class Builder<K, V>
159 extends ImmutableMultimap.Builder<K, V> {
160
161
162
163
164 public Builder() {}
165
166 @Override public Builder<K, V> put(K key, V value) {
167 super.put(key, value);
168 return this;
169 }
170
171
172
173
174
175
176 @Override public Builder<K, V> put(
177 Entry<? extends K, ? extends V> entry) {
178 super.put(entry);
179 return this;
180 }
181
182 @Override public Builder<K, V> putAll(K key, Iterable<? extends V> values) {
183 super.putAll(key, values);
184 return this;
185 }
186
187 @Override public Builder<K, V> putAll(K key, V... values) {
188 super.putAll(key, values);
189 return this;
190 }
191
192 @Override public Builder<K, V> putAll(
193 Multimap<? extends K, ? extends V> multimap) {
194 super.putAll(multimap);
195 return this;
196 }
197
198
199
200
201
202
203 @Override
204 public Builder<K, V> orderKeysBy(Comparator<? super K> keyComparator) {
205 super.orderKeysBy(keyComparator);
206 return this;
207 }
208
209
210
211
212
213
214 @Override
215 public Builder<K, V> orderValuesBy(Comparator<? super V> valueComparator) {
216 super.orderValuesBy(valueComparator);
217 return this;
218 }
219
220
221
222
223 @Override public ImmutableListMultimap<K, V> build() {
224 return (ImmutableListMultimap<K, V>) super.build();
225 }
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239
240 public static <K, V> ImmutableListMultimap<K, V> copyOf(
241 Multimap<? extends K, ? extends V> multimap) {
242 if (multimap.isEmpty()) {
243 return of();
244 }
245
246
247 if (multimap instanceof ImmutableListMultimap) {
248 @SuppressWarnings("unchecked")
249 ImmutableListMultimap<K, V> kvMultimap
250 = (ImmutableListMultimap<K, V>) multimap;
251 if (!kvMultimap.isPartialView()) {
252 return kvMultimap;
253 }
254 }
255
256 ImmutableMap.Builder<K, ImmutableList<V>> builder = ImmutableMap.builder();
257 int size = 0;
258
259 for (Entry<? extends K, ? extends Collection<? extends V>> entry
260 : multimap.asMap().entrySet()) {
261 ImmutableList<V> list = ImmutableList.copyOf(entry.getValue());
262 if (!list.isEmpty()) {
263 builder.put(entry.getKey(), list);
264 size += list.size();
265 }
266 }
267
268 return new ImmutableListMultimap<K, V>(builder.build(), size);
269 }
270
271 ImmutableListMultimap(ImmutableMap<K, ImmutableList<V>> map, int size) {
272 super(map, size);
273 }
274
275
276
277
278
279
280
281
282
283 @Override public ImmutableList<V> get(@Nullable K key) {
284
285 ImmutableList<V> list = (ImmutableList<V>) map.get(key);
286 return (list == null) ? ImmutableList.<V>of() : list;
287 }
288
289 private transient ImmutableListMultimap<V, K> inverse;
290
291
292
293
294
295
296
297
298
299
300
301 @Override
302 public ImmutableListMultimap<V, K> inverse() {
303 ImmutableListMultimap<V, K> result = inverse;
304 return (result == null) ? (inverse = invert()) : result;
305 }
306
307 private ImmutableListMultimap<V, K> invert() {
308 Builder<V, K> builder = builder();
309 for (Entry<K, V> entry : entries()) {
310 builder.put(entry.getValue(), entry.getKey());
311 }
312 ImmutableListMultimap<V, K> invertedMultimap = builder.build();
313 invertedMultimap.inverse = this;
314 return invertedMultimap;
315 }
316
317
318
319
320
321
322
323 @Deprecated @Override public ImmutableList<V> removeAll(Object key) {
324 throw new UnsupportedOperationException();
325 }
326
327
328
329
330
331
332
333 @Deprecated @Override public ImmutableList<V> replaceValues(
334 K key, Iterable<? extends V> values) {
335 throw new UnsupportedOperationException();
336 }
337
338
339
340
341
342 @GwtIncompatible("java.io.ObjectOutputStream")
343 private void writeObject(ObjectOutputStream stream) throws IOException {
344 stream.defaultWriteObject();
345 Serialization.writeMultimap(this, stream);
346 }
347
348 @GwtIncompatible("java.io.ObjectInputStream")
349 private void readObject(ObjectInputStream stream)
350 throws IOException, ClassNotFoundException {
351 stream.defaultReadObject();
352 int keyCount = stream.readInt();
353 if (keyCount < 0) {
354 throw new InvalidObjectException("Invalid key count " + keyCount);
355 }
356 ImmutableMap.Builder<Object, ImmutableList<Object>> builder
357 = ImmutableMap.builder();
358 int tmpSize = 0;
359
360 for (int i = 0; i < keyCount; i++) {
361 Object key = stream.readObject();
362 int valueCount = stream.readInt();
363 if (valueCount <= 0) {
364 throw new InvalidObjectException("Invalid value count " + valueCount);
365 }
366
367 Object[] array = new Object[valueCount];
368 for (int j = 0; j < valueCount; j++) {
369 array[j] = stream.readObject();
370 }
371 builder.put(key, ImmutableList.copyOf(array));
372 tmpSize += valueCount;
373 }
374
375 ImmutableMap<Object, ImmutableList<Object>> tmpMap;
376 try {
377 tmpMap = builder.build();
378 } catch (IllegalArgumentException e) {
379 throw (InvalidObjectException)
380 new InvalidObjectException(e.getMessage()).initCause(e);
381 }
382
383 FieldSettersHolder.MAP_FIELD_SETTER.set(this, tmpMap);
384 FieldSettersHolder.SIZE_FIELD_SETTER.set(this, tmpSize);
385 }
386
387 @GwtIncompatible("Not needed in emulated source")
388 private static final long serialVersionUID = 0;
389 }