1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.map;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.AbstractCollection;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.apache.commons.collections4.Factory;
33 import org.apache.commons.collections4.FunctorException;
34 import org.apache.commons.collections4.MultiMap;
35 import org.apache.commons.collections4.Transformer;
36 import org.apache.commons.collections4.iterators.EmptyIterator;
37 import org.apache.commons.collections4.iterators.IteratorChain;
38 import org.apache.commons.collections4.iterators.LazyIteratorChain;
39 import org.apache.commons.collections4.iterators.TransformIterator;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 @Deprecated
72 public class MultiValueMap<K, V> extends AbstractMapDecorator<K, Object> implements MultiMap<K, V>, Serializable {
73
74
75
76
77
78
79 private static final class ReflectionFactory<T extends Collection<?>> implements Factory<T>, Serializable {
80
81
82 private static final long serialVersionUID = 2986114157496788874L;
83
84 private final Class<T> clazz;
85
86 ReflectionFactory(final Class<T> clazz) {
87 this.clazz = clazz;
88 }
89
90 @Override
91 public T create() {
92 try {
93 return clazz.getDeclaredConstructor().newInstance();
94 } catch (final Exception ex) {
95 throw new FunctorException("Cannot instantiate class: " + clazz, ex);
96 }
97 }
98
99
100
101
102
103
104
105
106 private void readObject(final ObjectInputStream is) throws IOException, ClassNotFoundException {
107 is.defaultReadObject();
108
109 if (clazz != null && !Collection.class.isAssignableFrom(clazz)) {
110 throw new UnsupportedOperationException();
111 }
112 }
113 }
114
115
116
117
118 private final class Values extends AbstractCollection<V> {
119 @Override
120 public void clear() {
121 MultiValueMap.this.clear();
122 }
123
124 @Override
125 public Iterator<V> iterator() {
126 final IteratorChain<V> chain = new IteratorChain<>();
127 for (final K k : keySet()) {
128 chain.addIterator(new ValuesIterator(k));
129 }
130 return chain;
131 }
132
133 @Override
134 public int size() {
135 return totalSize();
136 }
137 }
138
139
140
141 private final class ValuesIterator implements Iterator<V> {
142 private final Object key;
143 private final Collection<V> values;
144 private final Iterator<V> iterator;
145
146 ValuesIterator(final Object key) {
147 this.key = key;
148 this.values = getCollection(key);
149 this.iterator = values.iterator();
150 }
151
152 @Override
153 public boolean hasNext() {
154 return iterator.hasNext();
155 }
156
157 @Override
158 public V next() {
159 return iterator.next();
160 }
161
162 @Override
163 public void remove() {
164 iterator.remove();
165 if (values.isEmpty()) {
166 MultiValueMap.this.remove(key);
167 }
168 }
169 }
170
171
172 private static final long serialVersionUID = -2214159910087182007L;
173
174
175
176
177
178
179
180
181
182
183
184
185
186 public static <K, V, C extends Collection<V>> MultiValueMap<K, V> multiValueMap(final Map<K, ? super C> map,
187 final Class<C> collectionClass) {
188 return new MultiValueMap<>(map, new ReflectionFactory<>(collectionClass));
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public static <K, V, C extends Collection<V>> MultiValueMap<K, V> multiValueMap(final Map<K, ? super C> map,
204 final Factory<C> collectionFactory) {
205 return new MultiValueMap<>(map, collectionFactory);
206 }
207
208
209
210
211
212
213
214
215
216
217
218 @SuppressWarnings({ "unchecked", "rawtypes" })
219 public static <K, V> MultiValueMap<K, V> multiValueMap(final Map<K, ? super Collection<V>> map) {
220 return MultiValueMap.<K, V, ArrayList>multiValueMap((Map<K, ? super Collection>) map, ArrayList.class);
221 }
222
223
224 private final Factory<? extends Collection<V>> collectionFactory;
225
226
227 private transient Collection<V> valuesView;
228
229
230
231
232
233 @SuppressWarnings({ "unchecked", "rawtypes" })
234 public MultiValueMap() {
235 this(new HashMap<>(), new ReflectionFactory(ArrayList.class));
236 }
237
238
239
240
241
242
243
244
245
246 @SuppressWarnings("unchecked")
247 protected <C extends Collection<V>> MultiValueMap(final Map<K, ? super C> map,
248 final Factory<C> collectionFactory) {
249 super((Map<K, Object>) map);
250 if (collectionFactory == null) {
251 throw new IllegalArgumentException("The factory must not be null");
252 }
253 this.collectionFactory = collectionFactory;
254 }
255
256
257
258
259 @Override
260 public void clear() {
261
262
263
264
265
266
267
268
269 decorated().clear();
270 }
271
272
273
274
275
276
277
278
279
280
281 @Override
282 @SuppressWarnings("unchecked")
283 public boolean containsValue(final Object value) {
284 final Set<Map.Entry<K, Object>> pairs = decorated().entrySet();
285 if (pairs != null) {
286 for (final Map.Entry<K, Object> entry : pairs) {
287 if (((Collection<V>) entry.getValue()).contains(value)) {
288 return true;
289 }
290 }
291 }
292 return false;
293 }
294
295
296
297
298
299
300
301
302 public boolean containsValue(final Object key, final Object value) {
303 final Collection<V> coll = getCollection(key);
304 if (coll == null) {
305 return false;
306 }
307 return coll.contains(value);
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321 protected Collection<V> createCollection(final int size) {
322 return collectionFactory.get();
323 }
324
325
326
327
328
329
330
331
332
333
334
335 @Override
336 public Set<Entry<K, Object>> entrySet() {
337 return super.entrySet();
338 }
339
340
341
342
343
344
345
346
347 @SuppressWarnings("unchecked")
348 public Collection<V> getCollection(final Object key) {
349 return (Collection<V>) decorated().get(key);
350 }
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 public Iterator<Entry<K, V>> iterator() {
367 final Collection<K> allKeys = new ArrayList<>(keySet());
368 final Iterator<K> keyIterator = allKeys.iterator();
369
370 return new LazyIteratorChain<Entry<K, V>>() {
371 @Override
372 protected Iterator<? extends Entry<K, V>> nextIterator(final int count) {
373 if (!keyIterator.hasNext()) {
374 return null;
375 }
376 final K key = keyIterator.next();
377 final Transformer<V, Entry<K, V>> transformer = input -> new Entry<K, V>() {
378 @Override
379 public K getKey() {
380 return key;
381 }
382
383 @Override
384 public V getValue() {
385 return input;
386 }
387
388 @Override
389 public V setValue(final V value) {
390 throw new UnsupportedOperationException();
391 }
392 };
393 return new TransformIterator<>(new ValuesIterator(key), transformer);
394 }
395 };
396 }
397
398
399
400
401
402
403
404 public Iterator<V> iterator(final Object key) {
405 if (!containsKey(key)) {
406 return EmptyIterator.<V>emptyIterator();
407 }
408 return new ValuesIterator(key);
409 }
410
411
412
413
414
415
416
417
418
419
420
421
422 @Override
423 @SuppressWarnings("unchecked")
424 public Object put(final K key, final Object value) {
425 boolean result = false;
426 Collection<V> coll = getCollection(key);
427 if (coll == null) {
428 coll = createCollection(1);
429 coll.add((V) value);
430 if (!coll.isEmpty()) {
431
432 decorated().put(key, coll);
433 result = true;
434 }
435 } else {
436 result = coll.add((V) value);
437 }
438 return result ? value : null;
439 }
440
441
442
443
444
445
446
447
448
449 public boolean putAll(final K key, final Collection<V> values) {
450 if (values == null || values.isEmpty()) {
451 return false;
452 }
453 boolean result = false;
454 Collection<V> coll = getCollection(key);
455 if (coll == null) {
456 coll = createCollection(values.size());
457 coll.addAll(values);
458 if (!coll.isEmpty()) {
459
460 decorated().put(key, coll);
461 result = true;
462 }
463 } else {
464 result = coll.addAll(values);
465 }
466 return result;
467 }
468
469
470
471
472
473
474
475
476
477
478
479
480
481 @Override
482 @SuppressWarnings("unchecked")
483 public void putAll(final Map<? extends K, ?> map) {
484 if (map instanceof MultiMap) {
485 for (final Map.Entry<? extends K, Object> entry : ((MultiMap<? extends K, V>) map).entrySet()) {
486 putAll(entry.getKey(), (Collection<V>) entry.getValue());
487 }
488 } else {
489 for (final Map.Entry<? extends K, ?> entry : map.entrySet()) {
490 put(entry.getKey(), entry.getValue());
491 }
492 }
493 }
494
495
496
497
498
499
500
501
502
503 @SuppressWarnings("unchecked")
504 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
505 in.defaultReadObject();
506 map = (Map<K, Object>) in.readObject();
507 }
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524 @Override
525 public boolean removeMapping(final Object key, final Object value) {
526 final Collection<V> valuesForKey = getCollection(key);
527 if (valuesForKey == null) {
528 return false;
529 }
530 final boolean removed = valuesForKey.remove(value);
531 if (!removed) {
532 return false;
533 }
534 if (valuesForKey.isEmpty()) {
535 remove(key);
536 }
537 return true;
538 }
539
540
541
542
543
544
545
546 public int size(final Object key) {
547 final Collection<V> coll = getCollection(key);
548 if (coll == null) {
549 return 0;
550 }
551 return coll.size();
552 }
553
554
555
556
557
558
559 public int totalSize() {
560 int total = 0;
561 for (final Object v : decorated().values()) {
562 total += CollectionUtils.size(v);
563 }
564 return total;
565 }
566
567
568
569
570
571
572
573
574
575 @Override
576 @SuppressWarnings("unchecked")
577 public Collection<Object> values() {
578 final Collection<V> vs = valuesView;
579 return (Collection<Object>) (vs != null ? vs : (valuesView = new Values()));
580 }
581
582
583
584
585
586
587
588
589 private void writeObject(final ObjectOutputStream out) throws IOException {
590 out.defaultWriteObject();
591 out.writeObject(map);
592 }
593
594 }