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 static com.google.common.base.Preconditions.checkPositionIndex;
20 import static com.google.common.base.Preconditions.checkState;
21 import static com.google.common.collect.CollectPreconditions.checkRemove;
22 import static java.util.Collections.unmodifiableList;
23
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.annotations.GwtIncompatible;
26
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.io.ObjectOutputStream;
30 import java.io.Serializable;
31 import java.util.AbstractSequentialList;
32 import java.util.Collection;
33 import java.util.ConcurrentModificationException;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.ListIterator;
38 import java.util.Map;
39 import java.util.Map.Entry;
40 import java.util.NoSuchElementException;
41 import java.util.Set;
42
43 import javax.annotation.Nullable;
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 @GwtCompatible(serializable = true, emulated = true)
102 public class LinkedListMultimap<K, V> extends AbstractMultimap<K, V>
103 implements ListMultimap<K, V>, Serializable {
104
105
106
107
108
109
110
111 private static final class Node<K, V> extends AbstractMapEntry<K, V> {
112 final K key;
113 V value;
114 Node<K, V> next;
115 Node<K, V> previous;
116 Node<K, V> nextSibling;
117 Node<K, V> previousSibling;
118
119 Node(@Nullable K key, @Nullable V value) {
120 this.key = key;
121 this.value = value;
122 }
123
124 @Override
125 public K getKey() {
126 return key;
127 }
128
129 @Override
130 public V getValue() {
131 return value;
132 }
133
134 @Override
135 public V setValue(@Nullable V newValue) {
136 V result = value;
137 this.value = newValue;
138 return result;
139 }
140 }
141
142 private static class KeyList<K, V> {
143 Node<K, V> head;
144 Node<K, V> tail;
145 int count;
146
147 KeyList(Node<K, V> firstNode) {
148 this.head = firstNode;
149 this.tail = firstNode;
150 firstNode.previousSibling = null;
151 firstNode.nextSibling = null;
152 this.count = 1;
153 }
154 }
155
156 private transient Node<K, V> head;
157 private transient Node<K, V> tail;
158 private transient Map<K, KeyList<K, V>> keyToKeyList;
159 private transient int size;
160
161
162
163
164
165
166 private transient int modCount;
167
168
169
170
171
172 public static <K, V> LinkedListMultimap<K, V> create() {
173 return new LinkedListMultimap<K, V>();
174 }
175
176
177
178
179
180
181
182
183 public static <K, V> LinkedListMultimap<K, V> create(int expectedKeys) {
184 return new LinkedListMultimap<K, V>(expectedKeys);
185 }
186
187
188
189
190
191
192
193
194 public static <K, V> LinkedListMultimap<K, V> create(
195 Multimap<? extends K, ? extends V> multimap) {
196 return new LinkedListMultimap<K, V>(multimap);
197 }
198
199 LinkedListMultimap() {
200 keyToKeyList = Maps.newHashMap();
201 }
202
203 private LinkedListMultimap(int expectedKeys) {
204 keyToKeyList = new HashMap<K, KeyList<K, V>>(expectedKeys);
205 }
206
207 private LinkedListMultimap(Multimap<? extends K, ? extends V> multimap) {
208 this(multimap.keySet().size());
209 putAll(multimap);
210 }
211
212
213
214
215
216
217
218 private Node<K, V> addNode(
219 @Nullable K key, @Nullable V value, @Nullable Node<K, V> nextSibling) {
220 Node<K, V> node = new Node<K, V>(key, value);
221 if (head == null) {
222 head = tail = node;
223 keyToKeyList.put(key, new KeyList<K, V>(node));
224 modCount++;
225 } else if (nextSibling == null) {
226 tail.next = node;
227 node.previous = tail;
228 tail = node;
229 KeyList<K, V> keyList = keyToKeyList.get(key);
230 if (keyList == null) {
231 keyToKeyList.put(key, keyList = new KeyList<K, V>(node));
232 modCount++;
233 } else {
234 keyList.count++;
235 Node<K, V> keyTail = keyList.tail;
236 keyTail.nextSibling = node;
237 node.previousSibling = keyTail;
238 keyList.tail = node;
239 }
240 } else {
241 KeyList<K, V> keyList = keyToKeyList.get(key);
242 keyList.count++;
243 node.previous = nextSibling.previous;
244 node.previousSibling = nextSibling.previousSibling;
245 node.next = nextSibling;
246 node.nextSibling = nextSibling;
247 if (nextSibling.previousSibling == null) {
248 keyToKeyList.get(key).head = node;
249 } else {
250 nextSibling.previousSibling.nextSibling = node;
251 }
252 if (nextSibling.previous == null) {
253 head = node;
254 } else {
255 nextSibling.previous.next = node;
256 }
257 nextSibling.previous = node;
258 nextSibling.previousSibling = node;
259 }
260 size++;
261 return node;
262 }
263
264
265
266
267
268
269 private void removeNode(Node<K, V> node) {
270 if (node.previous != null) {
271 node.previous.next = node.next;
272 } else {
273 head = node.next;
274 }
275 if (node.next != null) {
276 node.next.previous = node.previous;
277 } else {
278 tail = node.previous;
279 }
280 if (node.previousSibling == null && node.nextSibling == null) {
281 KeyList<K, V> keyList = keyToKeyList.remove(node.key);
282 keyList.count = 0;
283 modCount++;
284 } else {
285 KeyList<K, V> keyList = keyToKeyList.get(node.key);
286 keyList.count--;
287
288 if (node.previousSibling == null) {
289 keyList.head = node.nextSibling;
290 } else {
291 node.previousSibling.nextSibling = node.nextSibling;
292 }
293
294 if (node.nextSibling == null) {
295 keyList.tail = node.previousSibling;
296 } else {
297 node.nextSibling.previousSibling = node.previousSibling;
298 }
299 }
300 size--;
301 }
302
303
304 private void removeAllNodes(@Nullable Object key) {
305 Iterators.clear(new ValueForKeyIterator(key));
306 }
307
308
309 private static void checkElement(@Nullable Object node) {
310 if (node == null) {
311 throw new NoSuchElementException();
312 }
313 }
314
315
316 private class NodeIterator implements ListIterator<Entry<K, V>> {
317 int nextIndex;
318 Node<K, V> next;
319 Node<K, V> current;
320 Node<K, V> previous;
321 int expectedModCount = modCount;
322
323 NodeIterator(int index) {
324 int size = size();
325 checkPositionIndex(index, size);
326 if (index >= (size / 2)) {
327 previous = tail;
328 nextIndex = size;
329 while (index++ < size) {
330 previous();
331 }
332 } else {
333 next = head;
334 while (index-- > 0) {
335 next();
336 }
337 }
338 current = null;
339 }
340 private void checkForConcurrentModification() {
341 if (modCount != expectedModCount) {
342 throw new ConcurrentModificationException();
343 }
344 }
345 @Override
346 public boolean hasNext() {
347 checkForConcurrentModification();
348 return next != null;
349 }
350 @Override
351 public Node<K, V> next() {
352 checkForConcurrentModification();
353 checkElement(next);
354 previous = current = next;
355 next = next.next;
356 nextIndex++;
357 return current;
358 }
359 @Override
360 public void remove() {
361 checkForConcurrentModification();
362 checkRemove(current != null);
363 if (current != next) {
364 previous = current.previous;
365 nextIndex--;
366 } else {
367 next = current.next;
368 }
369 removeNode(current);
370 current = null;
371 expectedModCount = modCount;
372 }
373 @Override
374 public boolean hasPrevious() {
375 checkForConcurrentModification();
376 return previous != null;
377 }
378 @Override
379 public Node<K, V> previous() {
380 checkForConcurrentModification();
381 checkElement(previous);
382 next = current = previous;
383 previous = previous.previous;
384 nextIndex--;
385 return current;
386 }
387 @Override
388 public int nextIndex() {
389 return nextIndex;
390 }
391 @Override
392 public int previousIndex() {
393 return nextIndex - 1;
394 }
395 @Override
396 public void set(Entry<K, V> e) {
397 throw new UnsupportedOperationException();
398 }
399 @Override
400 public void add(Entry<K, V> e) {
401 throw new UnsupportedOperationException();
402 }
403 void setValue(V value) {
404 checkState(current != null);
405 current.value = value;
406 }
407 }
408
409
410 private class DistinctKeyIterator implements Iterator<K> {
411 final Set<K> seenKeys = Sets.<K>newHashSetWithExpectedSize(keySet().size());
412 Node<K, V> next = head;
413 Node<K, V> current;
414 int expectedModCount = modCount;
415
416 private void checkForConcurrentModification() {
417 if (modCount != expectedModCount) {
418 throw new ConcurrentModificationException();
419 }
420 }
421 @Override
422 public boolean hasNext() {
423 checkForConcurrentModification();
424 return next != null;
425 }
426 @Override
427 public K next() {
428 checkForConcurrentModification();
429 checkElement(next);
430 current = next;
431 seenKeys.add(current.key);
432 do {
433 next = next.next;
434 } while ((next != null) && !seenKeys.add(next.key));
435 return current.key;
436 }
437 @Override
438 public void remove() {
439 checkForConcurrentModification();
440 checkRemove(current != null);
441 removeAllNodes(current.key);
442 current = null;
443 expectedModCount = modCount;
444 }
445 }
446
447
448 private class ValueForKeyIterator implements ListIterator<V> {
449 final Object key;
450 int nextIndex;
451 Node<K, V> next;
452 Node<K, V> current;
453 Node<K, V> previous;
454
455
456 ValueForKeyIterator(@Nullable Object key) {
457 this.key = key;
458 KeyList<K, V> keyList = keyToKeyList.get(key);
459 next = (keyList == null) ? null : keyList.head;
460 }
461
462
463
464
465
466
467
468
469
470
471 public ValueForKeyIterator(@Nullable Object key, int index) {
472 KeyList<K, V> keyList = keyToKeyList.get(key);
473 int size = (keyList == null) ? 0 : keyList.count;
474 checkPositionIndex(index, size);
475 if (index >= (size / 2)) {
476 previous = (keyList == null) ? null : keyList.tail;
477 nextIndex = size;
478 while (index++ < size) {
479 previous();
480 }
481 } else {
482 next = (keyList == null) ? null : keyList.head;
483 while (index-- > 0) {
484 next();
485 }
486 }
487 this.key = key;
488 current = null;
489 }
490
491 @Override
492 public boolean hasNext() {
493 return next != null;
494 }
495
496 @Override
497 public V next() {
498 checkElement(next);
499 previous = current = next;
500 next = next.nextSibling;
501 nextIndex++;
502 return current.value;
503 }
504
505 @Override
506 public boolean hasPrevious() {
507 return previous != null;
508 }
509
510 @Override
511 public V previous() {
512 checkElement(previous);
513 next = current = previous;
514 previous = previous.previousSibling;
515 nextIndex--;
516 return current.value;
517 }
518
519 @Override
520 public int nextIndex() {
521 return nextIndex;
522 }
523
524 @Override
525 public int previousIndex() {
526 return nextIndex - 1;
527 }
528
529 @Override
530 public void remove() {
531 checkRemove(current != null);
532 if (current != next) {
533 previous = current.previousSibling;
534 nextIndex--;
535 } else {
536 next = current.nextSibling;
537 }
538 removeNode(current);
539 current = null;
540 }
541
542 @Override
543 public void set(V value) {
544 checkState(current != null);
545 current.value = value;
546 }
547
548 @Override
549 @SuppressWarnings("unchecked")
550 public void add(V value) {
551 previous = addNode((K) key, value, next);
552 nextIndex++;
553 current = null;
554 }
555 }
556
557
558
559 @Override
560 public int size() {
561 return size;
562 }
563
564 @Override
565 public boolean isEmpty() {
566 return head == null;
567 }
568
569 @Override
570 public boolean containsKey(@Nullable Object key) {
571 return keyToKeyList.containsKey(key);
572 }
573
574 @Override
575 public boolean containsValue(@Nullable Object value) {
576 return values().contains(value);
577 }
578
579
580
581
582
583
584
585
586
587
588 @Override
589 public boolean put(@Nullable K key, @Nullable V value) {
590 addNode(key, value, null);
591 return true;
592 }
593
594
595
596
597
598
599
600
601
602
603
604
605
606 @Override
607 public List<V> replaceValues(@Nullable K key, Iterable<? extends V> values) {
608 List<V> oldValues = getCopy(key);
609 ListIterator<V> keyValues = new ValueForKeyIterator(key);
610 Iterator<? extends V> newValues = values.iterator();
611
612
613 while (keyValues.hasNext() && newValues.hasNext()) {
614 keyValues.next();
615 keyValues.set(newValues.next());
616 }
617
618
619 while (keyValues.hasNext()) {
620 keyValues.next();
621 keyValues.remove();
622 }
623
624
625 while (newValues.hasNext()) {
626 keyValues.add(newValues.next());
627 }
628
629 return oldValues;
630 }
631
632 private List<V> getCopy(@Nullable Object key) {
633 return unmodifiableList(Lists.newArrayList(new ValueForKeyIterator(key)));
634 }
635
636
637
638
639
640
641
642 @Override
643 public List<V> removeAll(@Nullable Object key) {
644 List<V> oldValues = getCopy(key);
645 removeAllNodes(key);
646 return oldValues;
647 }
648
649 @Override
650 public void clear() {
651 head = null;
652 tail = null;
653 keyToKeyList.clear();
654 size = 0;
655 modCount++;
656 }
657
658
659
660
661
662
663
664
665
666
667
668
669 @Override
670 public List<V> get(final @Nullable K key) {
671 return new AbstractSequentialList<V>() {
672 @Override public int size() {
673 KeyList<K, V> keyList = keyToKeyList.get(key);
674 return (keyList == null) ? 0 : keyList.count;
675 }
676 @Override public ListIterator<V> listIterator(int index) {
677 return new ValueForKeyIterator(key, index);
678 }
679 };
680 }
681
682 @Override
683 Set<K> createKeySet() {
684 return new Sets.ImprovedAbstractSet<K>() {
685 @Override public int size() {
686 return keyToKeyList.size();
687 }
688 @Override public Iterator<K> iterator() {
689 return new DistinctKeyIterator();
690 }
691 @Override public boolean contains(Object key) {
692 return containsKey(key);
693 }
694 @Override
695 public boolean remove(Object o) {
696 return !LinkedListMultimap.this.removeAll(o).isEmpty();
697 }
698 };
699 }
700
701
702
703
704
705
706
707
708
709
710 @Override
711 public List<V> values() {
712 return (List<V>) super.values();
713 }
714
715 @Override
716 List<V> createValues() {
717 return new AbstractSequentialList<V>() {
718 @Override public int size() {
719 return size;
720 }
721
722 @Override public ListIterator<V> listIterator(int index) {
723 final NodeIterator nodeItr = new NodeIterator(index);
724 return new TransformedListIterator<Entry<K, V>, V>(nodeItr) {
725 @Override
726 V transform(Entry<K, V> entry) {
727 return entry.getValue();
728 }
729
730 @Override
731 public void set(V value) {
732 nodeItr.setValue(value);
733 }
734 };
735 }
736 };
737 }
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757 @Override
758 public List<Entry<K, V>> entries() {
759 return (List<Entry<K, V>>) super.entries();
760 }
761
762 @Override
763 List<Entry<K, V>> createEntries() {
764 return new AbstractSequentialList<Entry<K, V>>() {
765 @Override public int size() {
766 return size;
767 }
768
769 @Override public ListIterator<Entry<K, V>> listIterator(int index) {
770 return new NodeIterator(index);
771 }
772 };
773 }
774
775 @Override
776 Iterator<Entry<K, V>> entryIterator() {
777 throw new AssertionError("should never be called");
778 }
779
780 @Override
781 Map<K, Collection<V>> createAsMap() {
782 return new Multimaps.AsMap<K, V>(this);
783 }
784
785
786
787
788
789
790 @GwtIncompatible("java.io.ObjectOutputStream")
791 private void writeObject(ObjectOutputStream stream) throws IOException {
792 stream.defaultWriteObject();
793 stream.writeInt(size());
794 for (Entry<K, V> entry : entries()) {
795 stream.writeObject(entry.getKey());
796 stream.writeObject(entry.getValue());
797 }
798 }
799
800 @GwtIncompatible("java.io.ObjectInputStream")
801 private void readObject(ObjectInputStream stream)
802 throws IOException, ClassNotFoundException {
803 stream.defaultReadObject();
804 keyToKeyList = Maps.newLinkedHashMap();
805 int size = stream.readInt();
806 for (int i = 0; i < size; i++) {
807 @SuppressWarnings("unchecked")
808 K key = (K) stream.readObject();
809 @SuppressWarnings("unchecked")
810 V value = (V) stream.readObject();
811 put(key, value);
812 }
813 }
814
815 @GwtIncompatible("java serialization not supported")
816 private static final long serialVersionUID = 0;
817 }