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.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static com.google.common.base.Predicates.compose;
22 import static com.google.common.base.Predicates.equalTo;
23 import static com.google.common.base.Predicates.in;
24 import static com.google.common.base.Predicates.not;
25 import static com.google.common.collect.CollectPreconditions.checkNonnegative;
26
27 import com.google.common.annotations.Beta;
28 import com.google.common.annotations.GwtCompatible;
29 import com.google.common.base.Converter;
30 import com.google.common.base.Equivalence;
31 import com.google.common.base.Function;
32 import com.google.common.base.Joiner.MapJoiner;
33 import com.google.common.base.Objects;
34 import com.google.common.base.Preconditions;
35 import com.google.common.base.Predicate;
36 import com.google.common.base.Predicates;
37 import com.google.common.collect.MapDifference.ValueDifference;
38 import com.google.common.primitives.Ints;
39
40 import java.io.Serializable;
41 import java.util.AbstractCollection;
42 import java.util.AbstractMap;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.Comparator;
46 import java.util.EnumMap;
47 import java.util.HashMap;
48 import java.util.IdentityHashMap;
49 import java.util.Iterator;
50 import java.util.LinkedHashMap;
51 import java.util.Map;
52 import java.util.Map.Entry;
53 import java.util.Set;
54 import java.util.SortedMap;
55 import java.util.SortedSet;
56 import java.util.TreeMap;
57 import java.util.concurrent.ConcurrentMap;
58
59 import javax.annotation.Nullable;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 @GwtCompatible(emulated = true)
77 public final class Maps {
78 private Maps() {}
79
80 private enum EntryFunction implements Function<Entry<?, ?>, Object> {
81 KEY {
82 @Override
83 @Nullable
84 public Object apply(Entry<?, ?> entry) {
85 return entry.getKey();
86 }
87 },
88 VALUE {
89 @Override
90 @Nullable
91 public Object apply(Entry<?, ?> entry) {
92 return entry.getValue();
93 }
94 };
95 }
96
97 @SuppressWarnings("unchecked")
98 static <K> Function<Entry<K, ?>, K> keyFunction() {
99 return (Function) EntryFunction.KEY;
100 }
101
102 @SuppressWarnings("unchecked")
103 static <V> Function<Entry<?, V>, V> valueFunction() {
104 return (Function) EntryFunction.VALUE;
105 }
106
107 static <K, V> Iterator<K> keyIterator(Iterator<Entry<K, V>> entryIterator) {
108 return Iterators.transform(entryIterator, Maps.<K>keyFunction());
109 }
110
111 static <K, V> Iterator<V> valueIterator(Iterator<Entry<K, V>> entryIterator) {
112 return Iterators.transform(entryIterator, Maps.<V>valueFunction());
113 }
114
115 static <K, V> UnmodifiableIterator<V> valueIterator(
116 final UnmodifiableIterator<Entry<K, V>> entryIterator) {
117 return new UnmodifiableIterator<V>() {
118 @Override
119 public boolean hasNext() {
120 return entryIterator.hasNext();
121 }
122
123 @Override
124 public V next() {
125 return entryIterator.next().getValue();
126 }
127 };
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141 @GwtCompatible(serializable = true)
142 @Beta
143 public static <K extends Enum<K>, V> ImmutableMap<K, V> immutableEnumMap(
144 Map<K, ? extends V> map) {
145 if (map instanceof ImmutableEnumMap) {
146 @SuppressWarnings("unchecked")
147 ImmutableEnumMap<K, V> result = (ImmutableEnumMap<K, V>) map;
148 return result;
149 } else if (map.isEmpty()) {
150 return ImmutableMap.of();
151 } else {
152 for (Map.Entry<K, ? extends V> entry : map.entrySet()) {
153 checkNotNull(entry.getKey());
154 checkNotNull(entry.getValue());
155 }
156 return ImmutableEnumMap.asImmutable(new EnumMap<K, V>(map));
157 }
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 public static <K, V> HashMap<K, V> newHashMap() {
172 return new HashMap<K, V>();
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(
189 int expectedSize) {
190 return new HashMap<K, V>(capacity(expectedSize));
191 }
192
193
194
195
196
197
198 static int capacity(int expectedSize) {
199 if (expectedSize < 3) {
200 checkNonnegative(expectedSize, "expectedSize");
201 return expectedSize + 1;
202 }
203 if (expectedSize < Ints.MAX_POWER_OF_TWO) {
204 return expectedSize + expectedSize / 3;
205 }
206 return Integer.MAX_VALUE;
207 }
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 public static <K, V> HashMap<K, V> newHashMap(
224 Map<? extends K, ? extends V> map) {
225 return new HashMap<K, V>(map);
226 }
227
228
229
230
231
232
233
234
235
236
237 public static <K, V> LinkedHashMap<K, V> newLinkedHashMap() {
238 return new LinkedHashMap<K, V>();
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252 public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(
253 Map<? extends K, ? extends V> map) {
254 return new LinkedHashMap<K, V>(map);
255 }
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272 public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
273 return new MapMaker().<K, V>makeMap();
274 }
275
276
277
278
279
280
281
282
283
284
285 public static <K extends Comparable, V> TreeMap<K, V> newTreeMap() {
286 return new TreeMap<K, V>();
287 }
288
289
290
291
292
293
294
295
296
297
298
299
300
301 public static <K, V> TreeMap<K, V> newTreeMap(SortedMap<K, ? extends V> map) {
302 return new TreeMap<K, V>(map);
303 }
304
305
306
307
308
309
310
311
312
313
314
315 public static <C, K extends C, V> TreeMap<K, V> newTreeMap(
316 @Nullable Comparator<C> comparator) {
317
318
319
320
321
322 return new TreeMap<K, V>(comparator);
323 }
324
325
326
327
328
329
330
331 public static <K extends Enum<K>, V> EnumMap<K, V> newEnumMap(Class<K> type) {
332 return new EnumMap<K, V>(checkNotNull(type));
333 }
334
335
336
337
338
339
340
341
342
343
344 public static <K extends Enum<K>, V> EnumMap<K, V> newEnumMap(
345 Map<K, ? extends V> map) {
346 return new EnumMap<K, V>(map);
347 }
348
349
350
351
352
353
354 public static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {
355 return new IdentityHashMap<K, V>();
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374 @SuppressWarnings("unchecked")
375 public static <K, V> MapDifference<K, V> difference(
376 Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
377 if (left instanceof SortedMap) {
378 SortedMap<K, ? extends V> sortedLeft = (SortedMap<K, ? extends V>) left;
379 SortedMapDifference<K, V> result = difference(sortedLeft, right);
380 return result;
381 }
382 return difference(left, right, Equivalence.equals());
383 }
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404 @Beta
405 public static <K, V> MapDifference<K, V> difference(
406 Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right,
407 Equivalence<? super V> valueEquivalence) {
408 Preconditions.checkNotNull(valueEquivalence);
409
410 Map<K, V> onlyOnLeft = newHashMap();
411 Map<K, V> onlyOnRight = new HashMap<K, V>(right);
412 Map<K, V> onBoth = newHashMap();
413 Map<K, MapDifference.ValueDifference<V>> differences = newHashMap();
414 doDifference(left, right, valueEquivalence, onlyOnLeft, onlyOnRight, onBoth, differences);
415 return new MapDifferenceImpl<K, V>(onlyOnLeft, onlyOnRight, onBoth, differences);
416 }
417
418 private static <K, V> void doDifference(
419 Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right,
420 Equivalence<? super V> valueEquivalence,
421 Map<K, V> onlyOnLeft, Map<K, V> onlyOnRight, Map<K, V> onBoth,
422 Map<K, MapDifference.ValueDifference<V>> differences) {
423 for (Entry<? extends K, ? extends V> entry : left.entrySet()) {
424 K leftKey = entry.getKey();
425 V leftValue = entry.getValue();
426 if (right.containsKey(leftKey)) {
427 V rightValue = onlyOnRight.remove(leftKey);
428 if (valueEquivalence.equivalent(leftValue, rightValue)) {
429 onBoth.put(leftKey, leftValue);
430 } else {
431 differences.put(
432 leftKey, ValueDifferenceImpl.create(leftValue, rightValue));
433 }
434 } else {
435 onlyOnLeft.put(leftKey, leftValue);
436 }
437 }
438 }
439
440 private static <K, V> Map<K, V> unmodifiableMap(Map<K, V> map) {
441 if (map instanceof SortedMap) {
442 return Collections.unmodifiableSortedMap((SortedMap<K, ? extends V>) map);
443 } else {
444 return Collections.unmodifiableMap(map);
445 }
446 }
447
448 static class MapDifferenceImpl<K, V> implements MapDifference<K, V> {
449 final Map<K, V> onlyOnLeft;
450 final Map<K, V> onlyOnRight;
451 final Map<K, V> onBoth;
452 final Map<K, ValueDifference<V>> differences;
453
454 MapDifferenceImpl(Map<K, V> onlyOnLeft,
455 Map<K, V> onlyOnRight, Map<K, V> onBoth,
456 Map<K, ValueDifference<V>> differences) {
457 this.onlyOnLeft = unmodifiableMap(onlyOnLeft);
458 this.onlyOnRight = unmodifiableMap(onlyOnRight);
459 this.onBoth = unmodifiableMap(onBoth);
460 this.differences = unmodifiableMap(differences);
461 }
462
463 @Override
464 public boolean areEqual() {
465 return onlyOnLeft.isEmpty() && onlyOnRight.isEmpty() && differences.isEmpty();
466 }
467
468 @Override
469 public Map<K, V> entriesOnlyOnLeft() {
470 return onlyOnLeft;
471 }
472
473 @Override
474 public Map<K, V> entriesOnlyOnRight() {
475 return onlyOnRight;
476 }
477
478 @Override
479 public Map<K, V> entriesInCommon() {
480 return onBoth;
481 }
482
483 @Override
484 public Map<K, ValueDifference<V>> entriesDiffering() {
485 return differences;
486 }
487
488 @Override public boolean equals(Object object) {
489 if (object == this) {
490 return true;
491 }
492 if (object instanceof MapDifference) {
493 MapDifference<?, ?> other = (MapDifference<?, ?>) object;
494 return entriesOnlyOnLeft().equals(other.entriesOnlyOnLeft())
495 && entriesOnlyOnRight().equals(other.entriesOnlyOnRight())
496 && entriesInCommon().equals(other.entriesInCommon())
497 && entriesDiffering().equals(other.entriesDiffering());
498 }
499 return false;
500 }
501
502 @Override public int hashCode() {
503 return Objects.hashCode(entriesOnlyOnLeft(), entriesOnlyOnRight(),
504 entriesInCommon(), entriesDiffering());
505 }
506
507 @Override public String toString() {
508 if (areEqual()) {
509 return "equal";
510 }
511
512 StringBuilder result = new StringBuilder("not equal");
513 if (!onlyOnLeft.isEmpty()) {
514 result.append(": only on left=").append(onlyOnLeft);
515 }
516 if (!onlyOnRight.isEmpty()) {
517 result.append(": only on right=").append(onlyOnRight);
518 }
519 if (!differences.isEmpty()) {
520 result.append(": value differences=").append(differences);
521 }
522 return result.toString();
523 }
524 }
525
526 static class ValueDifferenceImpl<V>
527 implements MapDifference.ValueDifference<V> {
528 private final V left;
529 private final V right;
530
531 static <V> ValueDifference<V> create(@Nullable V left, @Nullable V right) {
532 return new ValueDifferenceImpl<V>(left, right);
533 }
534
535 private ValueDifferenceImpl(@Nullable V left, @Nullable V right) {
536 this.left = left;
537 this.right = right;
538 }
539
540 @Override
541 public V leftValue() {
542 return left;
543 }
544
545 @Override
546 public V rightValue() {
547 return right;
548 }
549
550 @Override public boolean equals(@Nullable Object object) {
551 if (object instanceof MapDifference.ValueDifference) {
552 MapDifference.ValueDifference<?> that =
553 (MapDifference.ValueDifference<?>) object;
554 return Objects.equal(this.left, that.leftValue())
555 && Objects.equal(this.right, that.rightValue());
556 }
557 return false;
558 }
559
560 @Override public int hashCode() {
561 return Objects.hashCode(left, right);
562 }
563
564 @Override public String toString() {
565 return "(" + left + ", " + right + ")";
566 }
567 }
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588 public static <K, V> SortedMapDifference<K, V> difference(
589 SortedMap<K, ? extends V> left, Map<? extends K, ? extends V> right) {
590 checkNotNull(left);
591 checkNotNull(right);
592 Comparator<? super K> comparator = orNaturalOrder(left.comparator());
593 SortedMap<K, V> onlyOnLeft = Maps.newTreeMap(comparator);
594 SortedMap<K, V> onlyOnRight = Maps.newTreeMap(comparator);
595 onlyOnRight.putAll(right);
596 SortedMap<K, V> onBoth = Maps.newTreeMap(comparator);
597 SortedMap<K, MapDifference.ValueDifference<V>> differences =
598 Maps.newTreeMap(comparator);
599 doDifference(left, right, Equivalence.equals(), onlyOnLeft, onlyOnRight, onBoth, differences);
600 return new SortedMapDifferenceImpl<K, V>(onlyOnLeft, onlyOnRight, onBoth, differences);
601 }
602
603 static class SortedMapDifferenceImpl<K, V> extends MapDifferenceImpl<K, V>
604 implements SortedMapDifference<K, V> {
605 SortedMapDifferenceImpl(SortedMap<K, V> onlyOnLeft,
606 SortedMap<K, V> onlyOnRight, SortedMap<K, V> onBoth,
607 SortedMap<K, ValueDifference<V>> differences) {
608 super(onlyOnLeft, onlyOnRight, onBoth, differences);
609 }
610
611 @Override public SortedMap<K, ValueDifference<V>> entriesDiffering() {
612 return (SortedMap<K, ValueDifference<V>>) super.entriesDiffering();
613 }
614
615 @Override public SortedMap<K, V> entriesInCommon() {
616 return (SortedMap<K, V>) super.entriesInCommon();
617 }
618
619 @Override public SortedMap<K, V> entriesOnlyOnLeft() {
620 return (SortedMap<K, V>) super.entriesOnlyOnLeft();
621 }
622
623 @Override public SortedMap<K, V> entriesOnlyOnRight() {
624 return (SortedMap<K, V>) super.entriesOnlyOnRight();
625 }
626 }
627
628
629
630
631
632
633 @SuppressWarnings("unchecked")
634 static <E> Comparator<? super E> orNaturalOrder(
635 @Nullable Comparator<? super E> comparator) {
636 if (comparator != null) {
637 return comparator;
638 }
639 return (Comparator<E>) Ordering.natural();
640 }
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669 @Beta
670 public static <K, V> Map<K, V> asMap(
671 Set<K> set, Function<? super K, V> function) {
672 if (set instanceof SortedSet) {
673 return asMap((SortedSet<K>) set, function);
674 } else {
675 return new AsMapView<K, V>(set, function);
676 }
677 }
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705 @Beta
706 public static <K, V> SortedMap<K, V> asMap(
707 SortedSet<K> set, Function<? super K, V> function) {
708 return Platform.mapsAsMapSortedSet(set, function);
709 }
710
711 static <K, V> SortedMap<K, V> asMapSortedIgnoreNavigable(SortedSet<K> set,
712 Function<? super K, V> function) {
713 return new SortedAsMapView<K, V>(set, function);
714 }
715
716 private static class AsMapView<K, V> extends ImprovedAbstractMap<K, V> {
717
718 private final Set<K> set;
719 final Function<? super K, V> function;
720
721 Set<K> backingSet() {
722 return set;
723 }
724
725 AsMapView(Set<K> set, Function<? super K, V> function) {
726 this.set = checkNotNull(set);
727 this.function = checkNotNull(function);
728 }
729
730 @Override
731 public Set<K> createKeySet() {
732 return removeOnlySet(backingSet());
733 }
734
735 @Override
736 Collection<V> createValues() {
737 return Collections2.transform(set, function);
738 }
739
740 @Override
741 public int size() {
742 return backingSet().size();
743 }
744
745 @Override
746 public boolean containsKey(@Nullable Object key) {
747 return backingSet().contains(key);
748 }
749
750 @Override
751 public V get(@Nullable Object key) {
752 if (Collections2.safeContains(backingSet(), key)) {
753 @SuppressWarnings("unchecked")
754 K k = (K) key;
755 return function.apply(k);
756 } else {
757 return null;
758 }
759 }
760
761 @Override
762 public V remove(@Nullable Object key) {
763 if (backingSet().remove(key)) {
764 @SuppressWarnings("unchecked")
765 K k = (K) key;
766 return function.apply(k);
767 } else {
768 return null;
769 }
770 }
771
772 @Override
773 public void clear() {
774 backingSet().clear();
775 }
776
777 @Override
778 protected Set<Entry<K, V>> createEntrySet() {
779 return new EntrySet<K, V>() {
780 @Override
781 Map<K, V> map() {
782 return AsMapView.this;
783 }
784
785 @Override
786 public Iterator<Entry<K, V>> iterator() {
787 return asMapEntryIterator(backingSet(), function);
788 }
789 };
790 }
791 }
792
793 static <K, V> Iterator<Entry<K, V>> asMapEntryIterator(
794 Set<K> set, final Function<? super K, V> function) {
795 return new TransformedIterator<K, Entry<K,V>>(set.iterator()) {
796 @Override
797 Entry<K, V> transform(final K key) {
798 return immutableEntry(key, function.apply(key));
799 }
800 };
801 }
802
803 private static class SortedAsMapView<K, V> extends AsMapView<K, V>
804 implements SortedMap<K, V> {
805
806 SortedAsMapView(SortedSet<K> set, Function<? super K, V> function) {
807 super(set, function);
808 }
809
810 @Override
811 SortedSet<K> backingSet() {
812 return (SortedSet<K>) super.backingSet();
813 }
814
815 @Override
816 public Comparator<? super K> comparator() {
817 return backingSet().comparator();
818 }
819
820 @Override
821 public Set<K> keySet() {
822 return removeOnlySortedSet(backingSet());
823 }
824
825 @Override
826 public SortedMap<K, V> subMap(K fromKey, K toKey) {
827 return asMap(backingSet().subSet(fromKey, toKey), function);
828 }
829
830 @Override
831 public SortedMap<K, V> headMap(K toKey) {
832 return asMap(backingSet().headSet(toKey), function);
833 }
834
835 @Override
836 public SortedMap<K, V> tailMap(K fromKey) {
837 return asMap(backingSet().tailSet(fromKey), function);
838 }
839
840 @Override
841 public K firstKey() {
842 return backingSet().first();
843 }
844
845 @Override
846 public K lastKey() {
847 return backingSet().last();
848 }
849 }
850
851 private static <E> Set<E> removeOnlySet(final Set<E> set) {
852 return new ForwardingSet<E>() {
853 @Override
854 protected Set<E> delegate() {
855 return set;
856 }
857
858 @Override
859 public boolean add(E element) {
860 throw new UnsupportedOperationException();
861 }
862
863 @Override
864 public boolean addAll(Collection<? extends E> es) {
865 throw new UnsupportedOperationException();
866 }
867 };
868 }
869
870 private static <E> SortedSet<E> removeOnlySortedSet(final SortedSet<E> set) {
871 return new ForwardingSortedSet<E>() {
872 @Override
873 protected SortedSet<E> delegate() {
874 return set;
875 }
876
877 @Override
878 public boolean add(E element) {
879 throw new UnsupportedOperationException();
880 }
881
882 @Override
883 public boolean addAll(Collection<? extends E> es) {
884 throw new UnsupportedOperationException();
885 }
886
887 @Override
888 public SortedSet<E> headSet(E toElement) {
889 return removeOnlySortedSet(super.headSet(toElement));
890 }
891
892 @Override
893 public SortedSet<E> subSet(E fromElement, E toElement) {
894 return removeOnlySortedSet(super.subSet(fromElement, toElement));
895 }
896
897 @Override
898 public SortedSet<E> tailSet(E fromElement) {
899 return removeOnlySortedSet(super.tailSet(fromElement));
900 }
901 };
902 }
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918 @Beta
919 public static <K, V> ImmutableMap<K, V> toMap(Iterable<K> keys,
920 Function<? super K, V> valueFunction) {
921 return toMap(keys.iterator(), valueFunction);
922 }
923
924
925
926
927
928
929
930
931
932
933
934
935 @Beta
936 public static <K, V> ImmutableMap<K, V> toMap(Iterator<K> keys,
937 Function<? super K, V> valueFunction) {
938 checkNotNull(valueFunction);
939
940 Map<K, V> builder = newLinkedHashMap();
941 while (keys.hasNext()) {
942 K key = keys.next();
943 builder.put(key, valueFunction.apply(key));
944 }
945 return ImmutableMap.copyOf(builder);
946 }
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962 public static <K, V> ImmutableMap<K, V> uniqueIndex(
963 Iterable<V> values, Function<? super V, K> keyFunction) {
964 return uniqueIndex(values.iterator(), keyFunction);
965 }
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982 public static <K, V> ImmutableMap<K, V> uniqueIndex(
983 Iterator<V> values, Function<? super V, K> keyFunction) {
984 checkNotNull(keyFunction);
985 ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
986 while (values.hasNext()) {
987 V value = values.next();
988 builder.put(keyFunction.apply(value), value);
989 }
990 return builder.build();
991 }
992
993
994
995
996
997
998
999
1000
1001
1002 @GwtCompatible(serializable = true)
1003 public static <K, V> Entry<K, V> immutableEntry(
1004 @Nullable K key, @Nullable V value) {
1005 return new ImmutableEntry<K, V>(key, value);
1006 }
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016 static <K, V> Set<Entry<K, V>> unmodifiableEntrySet(
1017 Set<Entry<K, V>> entrySet) {
1018 return new UnmodifiableEntrySet<K, V>(
1019 Collections.unmodifiableSet(entrySet));
1020 }
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031 static <K, V> Entry<K, V> unmodifiableEntry(final Entry<? extends K, ? extends V> entry) {
1032 checkNotNull(entry);
1033 return new AbstractMapEntry<K, V>() {
1034 @Override public K getKey() {
1035 return entry.getKey();
1036 }
1037
1038 @Override public V getValue() {
1039 return entry.getValue();
1040 }
1041 };
1042 }
1043
1044
1045 static class UnmodifiableEntries<K, V>
1046 extends ForwardingCollection<Entry<K, V>> {
1047 private final Collection<Entry<K, V>> entries;
1048
1049 UnmodifiableEntries(Collection<Entry<K, V>> entries) {
1050 this.entries = entries;
1051 }
1052
1053 @Override protected Collection<Entry<K, V>> delegate() {
1054 return entries;
1055 }
1056
1057 @Override public Iterator<Entry<K, V>> iterator() {
1058 final Iterator<Entry<K, V>> delegate = super.iterator();
1059 return new UnmodifiableIterator<Entry<K, V>>() {
1060 @Override
1061 public boolean hasNext() {
1062 return delegate.hasNext();
1063 }
1064
1065 @Override public Entry<K, V> next() {
1066 return unmodifiableEntry(delegate.next());
1067 }
1068 };
1069 }
1070
1071
1072
1073 @Override public Object[] toArray() {
1074 return standardToArray();
1075 }
1076
1077 @Override public <T> T[] toArray(T[] array) {
1078 return standardToArray(array);
1079 }
1080 }
1081
1082
1083 static class UnmodifiableEntrySet<K, V>
1084 extends UnmodifiableEntries<K, V> implements Set<Entry<K, V>> {
1085 UnmodifiableEntrySet(Set<Entry<K, V>> entries) {
1086 super(entries);
1087 }
1088
1089
1090
1091 @Override public boolean equals(@Nullable Object object) {
1092 return Sets.equalsImpl(this, object);
1093 }
1094
1095 @Override public int hashCode() {
1096 return Sets.hashCodeImpl(this);
1097 }
1098 }
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111 @Beta
1112 public static <A, B> Converter<A, B> asConverter(final BiMap<A, B> bimap) {
1113 return new BiMapConverter<A, B>(bimap);
1114 }
1115
1116 private static final class BiMapConverter<A, B> extends Converter<A, B> implements Serializable {
1117 private final BiMap<A, B> bimap;
1118
1119 BiMapConverter(BiMap<A, B> bimap) {
1120 this.bimap = checkNotNull(bimap);
1121 }
1122
1123 @Override
1124 protected B doForward(A a) {
1125 return convert(bimap, a);
1126 }
1127
1128 @Override
1129 protected A doBackward(B b) {
1130 return convert(bimap.inverse(), b);
1131 }
1132
1133 private static <X, Y> Y convert(BiMap<X, Y> bimap, X input) {
1134 Y output = bimap.get(input);
1135 checkArgument(output != null, "No non-null mapping present for input: %s", input);
1136 return output;
1137 }
1138
1139 @Override
1140 public boolean equals(@Nullable Object object) {
1141 if (object instanceof BiMapConverter) {
1142 BiMapConverter<?, ?> that = (BiMapConverter<?, ?>) object;
1143 return this.bimap.equals(that.bimap);
1144 }
1145 return false;
1146 }
1147
1148 @Override
1149 public int hashCode() {
1150 return bimap.hashCode();
1151 }
1152
1153
1154 @Override
1155 public String toString() {
1156 return "Maps.asConverter(" + bimap + ")";
1157 }
1158
1159 private static final long serialVersionUID = 0L;
1160 }
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190 public static <K, V> BiMap<K, V> synchronizedBiMap(BiMap<K, V> bimap) {
1191 return Synchronized.biMap(bimap, null);
1192 }
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207 public static <K, V> BiMap<K, V> unmodifiableBiMap(
1208 BiMap<? extends K, ? extends V> bimap) {
1209 return new UnmodifiableBiMap<K, V>(bimap, null);
1210 }
1211
1212
1213 private static class UnmodifiableBiMap<K, V>
1214 extends ForwardingMap<K, V> implements BiMap<K, V>, Serializable {
1215 final Map<K, V> unmodifiableMap;
1216 final BiMap<? extends K, ? extends V> delegate;
1217 BiMap<V, K> inverse;
1218 transient Set<V> values;
1219
1220 UnmodifiableBiMap(BiMap<? extends K, ? extends V> delegate,
1221 @Nullable BiMap<V, K> inverse) {
1222 unmodifiableMap = Collections.unmodifiableMap(delegate);
1223 this.delegate = delegate;
1224 this.inverse = inverse;
1225 }
1226
1227 @Override protected Map<K, V> delegate() {
1228 return unmodifiableMap;
1229 }
1230
1231 @Override
1232 public V forcePut(K key, V value) {
1233 throw new UnsupportedOperationException();
1234 }
1235
1236 @Override
1237 public BiMap<V, K> inverse() {
1238 BiMap<V, K> result = inverse;
1239 return (result == null)
1240 ? inverse = new UnmodifiableBiMap<V, K>(delegate.inverse(), this)
1241 : result;
1242 }
1243
1244 @Override public Set<V> values() {
1245 Set<V> result = values;
1246 return (result == null)
1247 ? values = Collections.unmodifiableSet(delegate.values())
1248 : result;
1249 }
1250
1251 private static final long serialVersionUID = 0;
1252 }
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290 public static <K, V1, V2> Map<K, V2> transformValues(
1291 Map<K, V1> fromMap, Function<? super V1, V2> function) {
1292 return transformEntries(fromMap, asEntryTransformer(function));
1293 }
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334 public static <K, V1, V2> SortedMap<K, V2> transformValues(
1335 SortedMap<K, V1> fromMap, Function<? super V1, V2> function) {
1336 return transformEntries(fromMap, asEntryTransformer(function));
1337 }
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390 public static <K, V1, V2> Map<K, V2> transformEntries(
1391 Map<K, V1> fromMap,
1392 EntryTransformer<? super K, ? super V1, V2> transformer) {
1393 if (fromMap instanceof SortedMap) {
1394 return transformEntries((SortedMap<K, V1>) fromMap, transformer);
1395 }
1396 return new TransformedEntriesMap<K, V1, V2>(fromMap, transformer);
1397 }
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451 public static <K, V1, V2> SortedMap<K, V2> transformEntries(
1452 SortedMap<K, V1> fromMap,
1453 EntryTransformer<? super K, ? super V1, V2> transformer) {
1454 return Platform.mapsTransformEntriesSortedMap(fromMap, transformer);
1455 }
1456
1457 static <K, V1, V2> SortedMap<K, V2> transformEntriesIgnoreNavigable(
1458 SortedMap<K, V1> fromMap,
1459 EntryTransformer<? super K, ? super V1, V2> transformer) {
1460 return new TransformedEntriesSortedMap<K, V1, V2>(fromMap, transformer);
1461 }
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473 public interface EntryTransformer<K, V1, V2> {
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491 V2 transformEntry(@Nullable K key, @Nullable V1 value);
1492 }
1493
1494
1495
1496
1497 static <K, V1, V2> EntryTransformer<K, V1, V2>
1498 asEntryTransformer(final Function<? super V1, V2> function) {
1499 checkNotNull(function);
1500 return new EntryTransformer<K, V1, V2>() {
1501 @Override
1502 public V2 transformEntry(K key, V1 value) {
1503 return function.apply(value);
1504 }
1505 };
1506 }
1507
1508 static <K, V1, V2> Function<V1, V2> asValueToValueFunction(
1509 final EntryTransformer<? super K, V1, V2> transformer, final K key) {
1510 checkNotNull(transformer);
1511 return new Function<V1, V2>() {
1512 @Override
1513 public V2 apply(@Nullable V1 v1) {
1514 return transformer.transformEntry(key, v1);
1515 }
1516 };
1517 }
1518
1519
1520
1521
1522 static <K, V1, V2> Function<Entry<K, V1>, V2> asEntryToValueFunction(
1523 final EntryTransformer<? super K, ? super V1, V2> transformer) {
1524 checkNotNull(transformer);
1525 return new Function<Entry<K, V1>, V2>() {
1526 @Override
1527 public V2 apply(Entry<K, V1> entry) {
1528 return transformer.transformEntry(entry.getKey(), entry.getValue());
1529 }
1530 };
1531 }
1532
1533
1534
1535
1536 static <V2, K, V1> Entry<K, V2> transformEntry(
1537 final EntryTransformer<? super K, ? super V1, V2> transformer, final Entry<K, V1> entry) {
1538 checkNotNull(transformer);
1539 checkNotNull(entry);
1540 return new AbstractMapEntry<K, V2>() {
1541 @Override
1542 public K getKey() {
1543 return entry.getKey();
1544 }
1545
1546 @Override
1547 public V2 getValue() {
1548 return transformer.transformEntry(entry.getKey(), entry.getValue());
1549 }
1550 };
1551 }
1552
1553
1554
1555
1556 static <K, V1, V2> Function<Entry<K, V1>, Entry<K, V2>> asEntryToEntryFunction(
1557 final EntryTransformer<? super K, ? super V1, V2> transformer) {
1558 checkNotNull(transformer);
1559 return new Function<Entry<K, V1>, Entry<K, V2>>() {
1560 @Override
1561 public Entry<K, V2> apply(final Entry<K, V1> entry) {
1562 return transformEntry(transformer, entry);
1563 }
1564 };
1565 }
1566
1567 static class TransformedEntriesMap<K, V1, V2>
1568 extends ImprovedAbstractMap<K, V2> {
1569 final Map<K, V1> fromMap;
1570 final EntryTransformer<? super K, ? super V1, V2> transformer;
1571
1572 TransformedEntriesMap(
1573 Map<K, V1> fromMap,
1574 EntryTransformer<? super K, ? super V1, V2> transformer) {
1575 this.fromMap = checkNotNull(fromMap);
1576 this.transformer = checkNotNull(transformer);
1577 }
1578
1579 @Override public int size() {
1580 return fromMap.size();
1581 }
1582
1583 @Override public boolean containsKey(Object key) {
1584 return fromMap.containsKey(key);
1585 }
1586
1587
1588 @SuppressWarnings("unchecked")
1589 @Override public V2 get(Object key) {
1590 V1 value = fromMap.get(key);
1591 return (value != null || fromMap.containsKey(key))
1592 ? transformer.transformEntry((K) key, value)
1593 : null;
1594 }
1595
1596
1597 @SuppressWarnings("unchecked")
1598 @Override public V2 remove(Object key) {
1599 return fromMap.containsKey(key)
1600 ? transformer.transformEntry((K) key, fromMap.remove(key))
1601 : null;
1602 }
1603
1604 @Override public void clear() {
1605 fromMap.clear();
1606 }
1607
1608 @Override public Set<K> keySet() {
1609 return fromMap.keySet();
1610 }
1611
1612 @Override
1613 protected Set<Entry<K, V2>> createEntrySet() {
1614 return new EntrySet<K, V2>() {
1615 @Override Map<K, V2> map() {
1616 return TransformedEntriesMap.this;
1617 }
1618
1619 @Override public Iterator<Entry<K, V2>> iterator() {
1620 return Iterators.transform(fromMap.entrySet().iterator(),
1621 Maps.<K, V1, V2>asEntryToEntryFunction(transformer));
1622 }
1623 };
1624 }
1625 }
1626
1627 static class TransformedEntriesSortedMap<K, V1, V2>
1628 extends TransformedEntriesMap<K, V1, V2> implements SortedMap<K, V2> {
1629
1630 protected SortedMap<K, V1> fromMap() {
1631 return (SortedMap<K, V1>) fromMap;
1632 }
1633
1634 TransformedEntriesSortedMap(SortedMap<K, V1> fromMap,
1635 EntryTransformer<? super K, ? super V1, V2> transformer) {
1636 super(fromMap, transformer);
1637 }
1638
1639 @Override public Comparator<? super K> comparator() {
1640 return fromMap().comparator();
1641 }
1642
1643 @Override public K firstKey() {
1644 return fromMap().firstKey();
1645 }
1646
1647 @Override public SortedMap<K, V2> headMap(K toKey) {
1648 return transformEntries(fromMap().headMap(toKey), transformer);
1649 }
1650
1651 @Override public K lastKey() {
1652 return fromMap().lastKey();
1653 }
1654
1655 @Override public SortedMap<K, V2> subMap(K fromKey, K toKey) {
1656 return transformEntries(
1657 fromMap().subMap(fromKey, toKey), transformer);
1658 }
1659
1660 @Override public SortedMap<K, V2> tailMap(K fromKey) {
1661 return transformEntries(fromMap().tailMap(fromKey), transformer);
1662 }
1663 }
1664
1665 static <K> Predicate<Entry<K, ?>> keyPredicateOnEntries(Predicate<? super K> keyPredicate) {
1666 return compose(keyPredicate, Maps.<K>keyFunction());
1667 }
1668
1669 static <V> Predicate<Entry<?, V>> valuePredicateOnEntries(Predicate<? super V> valuePredicate) {
1670 return compose(valuePredicate, Maps.<V>valueFunction());
1671 }
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701 public static <K, V> Map<K, V> filterKeys(
1702 Map<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
1703 if (unfiltered instanceof SortedMap) {
1704 return filterKeys((SortedMap<K, V>) unfiltered, keyPredicate);
1705 } else if (unfiltered instanceof BiMap) {
1706 return filterKeys((BiMap<K, V>) unfiltered, keyPredicate);
1707 }
1708 checkNotNull(keyPredicate);
1709 Predicate<Entry<K, ?>> entryPredicate = keyPredicateOnEntries(keyPredicate);
1710 return (unfiltered instanceof AbstractFilteredMap)
1711 ? filterFiltered((AbstractFilteredMap<K, V>) unfiltered, entryPredicate)
1712 : new FilteredKeyMap<K, V>(
1713 checkNotNull(unfiltered), keyPredicate, entryPredicate);
1714 }
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746 public static <K, V> SortedMap<K, V> filterKeys(
1747 SortedMap<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
1748
1749
1750 return filterEntries(unfiltered, Maps.<K>keyPredicateOnEntries(keyPredicate));
1751 }
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778 public static <K, V> BiMap<K, V> filterKeys(
1779 BiMap<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
1780 checkNotNull(keyPredicate);
1781 return filterEntries(unfiltered, Maps.<K>keyPredicateOnEntries(keyPredicate));
1782 }
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813 public static <K, V> Map<K, V> filterValues(
1814 Map<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
1815 if (unfiltered instanceof SortedMap) {
1816 return filterValues((SortedMap<K, V>) unfiltered, valuePredicate);
1817 } else if (unfiltered instanceof BiMap) {
1818 return filterValues((BiMap<K, V>) unfiltered, valuePredicate);
1819 }
1820 return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
1821 }
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854 public static <K, V> SortedMap<K, V> filterValues(
1855 SortedMap<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
1856 return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
1857 }
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887 public static <K, V> BiMap<K, V> filterValues(
1888 BiMap<K, V> unfiltered, final Predicate<? super V> valuePredicate) {
1889 return filterEntries(unfiltered, Maps.<V>valuePredicateOnEntries(valuePredicate));
1890 }
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921 public static <K, V> Map<K, V> filterEntries(
1922 Map<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
1923 if (unfiltered instanceof SortedMap) {
1924 return filterEntries((SortedMap<K, V>) unfiltered, entryPredicate);
1925 } else if (unfiltered instanceof BiMap) {
1926 return filterEntries((BiMap<K, V>) unfiltered, entryPredicate);
1927 }
1928 checkNotNull(entryPredicate);
1929 return (unfiltered instanceof AbstractFilteredMap)
1930 ? filterFiltered((AbstractFilteredMap<K, V>) unfiltered, entryPredicate)
1931 : new FilteredEntryMap<K, V>(checkNotNull(unfiltered), entryPredicate);
1932 }
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965 public static <K, V> SortedMap<K, V> filterEntries(
1966 SortedMap<K, V> unfiltered,
1967 Predicate<? super Entry<K, V>> entryPredicate) {
1968 return Platform.mapsFilterSortedMap(unfiltered, entryPredicate);
1969 }
1970
1971 static <K, V> SortedMap<K, V> filterSortedIgnoreNavigable(
1972 SortedMap<K, V> unfiltered,
1973 Predicate<? super Entry<K, V>> entryPredicate) {
1974 checkNotNull(entryPredicate);
1975 return (unfiltered instanceof FilteredEntrySortedMap)
1976 ? filterFiltered((FilteredEntrySortedMap<K, V>) unfiltered, entryPredicate)
1977 : new FilteredEntrySortedMap<K, V>(checkNotNull(unfiltered), entryPredicate);
1978 }
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007 public static <K, V> BiMap<K, V> filterEntries(
2008 BiMap<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
2009 checkNotNull(unfiltered);
2010 checkNotNull(entryPredicate);
2011 return (unfiltered instanceof FilteredEntryBiMap)
2012 ? filterFiltered((FilteredEntryBiMap<K, V>) unfiltered, entryPredicate)
2013 : new FilteredEntryBiMap<K, V>(unfiltered, entryPredicate);
2014 }
2015
2016
2017
2018
2019
2020 private static <K, V> Map<K, V> filterFiltered(AbstractFilteredMap<K, V> map,
2021 Predicate<? super Entry<K, V>> entryPredicate) {
2022 return new FilteredEntryMap<K, V>(map.unfiltered,
2023 Predicates.<Entry<K, V>>and(map.predicate, entryPredicate));
2024 }
2025
2026 private abstract static class AbstractFilteredMap<K, V>
2027 extends ImprovedAbstractMap<K, V> {
2028 final Map<K, V> unfiltered;
2029 final Predicate<? super Entry<K, V>> predicate;
2030
2031 AbstractFilteredMap(
2032 Map<K, V> unfiltered, Predicate<? super Entry<K, V>> predicate) {
2033 this.unfiltered = unfiltered;
2034 this.predicate = predicate;
2035 }
2036
2037 boolean apply(@Nullable Object key, @Nullable V value) {
2038
2039
2040 @SuppressWarnings("unchecked")
2041 K k = (K) key;
2042 return predicate.apply(Maps.immutableEntry(k, value));
2043 }
2044
2045 @Override public V put(K key, V value) {
2046 checkArgument(apply(key, value));
2047 return unfiltered.put(key, value);
2048 }
2049
2050 @Override public void putAll(Map<? extends K, ? extends V> map) {
2051 for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
2052 checkArgument(apply(entry.getKey(), entry.getValue()));
2053 }
2054 unfiltered.putAll(map);
2055 }
2056
2057 @Override public boolean containsKey(Object key) {
2058 return unfiltered.containsKey(key) && apply(key, unfiltered.get(key));
2059 }
2060
2061 @Override public V get(Object key) {
2062 V value = unfiltered.get(key);
2063 return ((value != null) && apply(key, value)) ? value : null;
2064 }
2065
2066 @Override public boolean isEmpty() {
2067 return entrySet().isEmpty();
2068 }
2069
2070 @Override public V remove(Object key) {
2071 return containsKey(key) ? unfiltered.remove(key) : null;
2072 }
2073
2074 @Override
2075 Collection<V> createValues() {
2076 return new FilteredMapValues<K, V>(this, unfiltered, predicate);
2077 }
2078 }
2079
2080 private static final class FilteredMapValues<K, V> extends Maps.Values<K, V> {
2081 Map<K, V> unfiltered;
2082 Predicate<? super Entry<K, V>> predicate;
2083
2084 FilteredMapValues(Map<K, V> filteredMap, Map<K, V> unfiltered,
2085 Predicate<? super Entry<K, V>> predicate) {
2086 super(filteredMap);
2087 this.unfiltered = unfiltered;
2088 this.predicate = predicate;
2089 }
2090
2091 @Override public boolean remove(Object o) {
2092 return Iterables.removeFirstMatching(unfiltered.entrySet(),
2093 Predicates.<Entry<K, V>>and(predicate, Maps.<V>valuePredicateOnEntries(equalTo(o))))
2094 != null;
2095 }
2096
2097 private boolean removeIf(Predicate<? super V> valuePredicate) {
2098 return Iterables.removeIf(unfiltered.entrySet(), Predicates.<Entry<K, V>>and(
2099 predicate, Maps.<V>valuePredicateOnEntries(valuePredicate)));
2100 }
2101
2102 @Override public boolean removeAll(Collection<?> collection) {
2103 return removeIf(in(collection));
2104 }
2105
2106 @Override public boolean retainAll(Collection<?> collection) {
2107 return removeIf(not(in(collection)));
2108 }
2109
2110 @Override public Object[] toArray() {
2111
2112 return Lists.newArrayList(iterator()).toArray();
2113 }
2114
2115 @Override public <T> T[] toArray(T[] array) {
2116 return Lists.newArrayList(iterator()).toArray(array);
2117 }
2118 }
2119
2120 private static class FilteredKeyMap<K, V> extends AbstractFilteredMap<K, V> {
2121 Predicate<? super K> keyPredicate;
2122
2123 FilteredKeyMap(Map<K, V> unfiltered, Predicate<? super K> keyPredicate,
2124 Predicate<? super Entry<K, V>> entryPredicate) {
2125 super(unfiltered, entryPredicate);
2126 this.keyPredicate = keyPredicate;
2127 }
2128
2129 @Override
2130 protected Set<Entry<K, V>> createEntrySet() {
2131 return Sets.filter(unfiltered.entrySet(), predicate);
2132 }
2133
2134 @Override
2135 Set<K> createKeySet() {
2136 return Sets.filter(unfiltered.keySet(), keyPredicate);
2137 }
2138
2139
2140
2141 @Override
2142 @SuppressWarnings("unchecked")
2143 public boolean containsKey(Object key) {
2144 return unfiltered.containsKey(key) && keyPredicate.apply((K) key);
2145 }
2146 }
2147
2148 static class FilteredEntryMap<K, V> extends AbstractFilteredMap<K, V> {
2149
2150
2151
2152
2153 final Set<Entry<K, V>> filteredEntrySet;
2154
2155 FilteredEntryMap(
2156 Map<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) {
2157 super(unfiltered, entryPredicate);
2158 filteredEntrySet = Sets.filter(unfiltered.entrySet(), predicate);
2159 }
2160
2161 @Override
2162 protected Set<Entry<K, V>> createEntrySet() {
2163 return new EntrySet();
2164 }
2165
2166 private class EntrySet extends ForwardingSet<Entry<K, V>> {
2167 @Override protected Set<Entry<K, V>> delegate() {
2168 return filteredEntrySet;
2169 }
2170
2171 @Override public Iterator<Entry<K, V>> iterator() {
2172 return new TransformedIterator<Entry<K, V>, Entry<K, V>>(filteredEntrySet.iterator()) {
2173 @Override
2174 Entry<K, V> transform(final Entry<K, V> entry) {
2175 return new ForwardingMapEntry<K, V>() {
2176 @Override
2177 protected Entry<K, V> delegate() {
2178 return entry;
2179 }
2180
2181 @Override
2182 public V setValue(V newValue) {
2183 checkArgument(apply(getKey(), newValue));
2184 return super.setValue(newValue);
2185 }
2186 };
2187 }
2188 };
2189 }
2190 }
2191
2192 @Override
2193 Set<K> createKeySet() {
2194 return new KeySet();
2195 }
2196
2197 class KeySet extends Maps.KeySet<K, V> {
2198 KeySet() {
2199 super(FilteredEntryMap.this);
2200 }
2201
2202 @Override public boolean remove(Object o) {
2203 if (containsKey(o)) {
2204 unfiltered.remove(o);
2205 return true;
2206 }
2207 return false;
2208 }
2209
2210 private boolean removeIf(Predicate<? super K> keyPredicate) {
2211 return Iterables.removeIf(unfiltered.entrySet(), Predicates.<Entry<K, V>>and(
2212 predicate, Maps.<K>keyPredicateOnEntries(keyPredicate)));
2213 }
2214
2215 @Override
2216 public boolean removeAll(Collection<?> c) {
2217 return removeIf(in(c));
2218 }
2219
2220 @Override
2221 public boolean retainAll(Collection<?> c) {
2222 return removeIf(not(in(c)));
2223 }
2224
2225 @Override public Object[] toArray() {
2226
2227 return Lists.newArrayList(iterator()).toArray();
2228 }
2229
2230 @Override public <T> T[] toArray(T[] array) {
2231 return Lists.newArrayList(iterator()).toArray(array);
2232 }
2233 }
2234 }
2235
2236
2237
2238
2239
2240 private static <K, V> SortedMap<K, V> filterFiltered(
2241 FilteredEntrySortedMap<K, V> map,
2242 Predicate<? super Entry<K, V>> entryPredicate) {
2243 Predicate<Entry<K, V>> predicate
2244 = Predicates.and(map.predicate, entryPredicate);
2245 return new FilteredEntrySortedMap<K, V>(map.sortedMap(), predicate);
2246 }
2247
2248 private static class FilteredEntrySortedMap<K, V>
2249 extends FilteredEntryMap<K, V> implements SortedMap<K, V> {
2250
2251 FilteredEntrySortedMap(SortedMap<K, V> unfiltered,
2252 Predicate<? super Entry<K, V>> entryPredicate) {
2253 super(unfiltered, entryPredicate);
2254 }
2255
2256 SortedMap<K, V> sortedMap() {
2257 return (SortedMap<K, V>) unfiltered;
2258 }
2259
2260 @Override public SortedSet<K> keySet() {
2261 return (SortedSet<K>) super.keySet();
2262 }
2263
2264 @Override
2265 SortedSet<K> createKeySet() {
2266 return new SortedKeySet();
2267 }
2268
2269 class SortedKeySet extends KeySet implements SortedSet<K> {
2270 @Override
2271 public Comparator<? super K> comparator() {
2272 return sortedMap().comparator();
2273 }
2274
2275 @Override
2276 public SortedSet<K> subSet(K fromElement, K toElement) {
2277 return (SortedSet<K>) subMap(fromElement, toElement).keySet();
2278 }
2279
2280 @Override
2281 public SortedSet<K> headSet(K toElement) {
2282 return (SortedSet<K>) headMap(toElement).keySet();
2283 }
2284
2285 @Override
2286 public SortedSet<K> tailSet(K fromElement) {
2287 return (SortedSet<K>) tailMap(fromElement).keySet();
2288 }
2289
2290 @Override
2291 public K first() {
2292 return firstKey();
2293 }
2294
2295 @Override
2296 public K last() {
2297 return lastKey();
2298 }
2299 }
2300
2301 @Override public Comparator<? super K> comparator() {
2302 return sortedMap().comparator();
2303 }
2304
2305 @Override public K firstKey() {
2306
2307 return keySet().iterator().next();
2308 }
2309
2310 @Override public K lastKey() {
2311 SortedMap<K, V> headMap = sortedMap();
2312 while (true) {
2313
2314 K key = headMap.lastKey();
2315 if (apply(key, unfiltered.get(key))) {
2316 return key;
2317 }
2318 headMap = sortedMap().headMap(key);
2319 }
2320 }
2321
2322 @Override public SortedMap<K, V> headMap(K toKey) {
2323 return new FilteredEntrySortedMap<K, V>(sortedMap().headMap(toKey), predicate);
2324 }
2325
2326 @Override public SortedMap<K, V> subMap(K fromKey, K toKey) {
2327 return new FilteredEntrySortedMap<K, V>(
2328 sortedMap().subMap(fromKey, toKey), predicate);
2329 }
2330
2331 @Override public SortedMap<K, V> tailMap(K fromKey) {
2332 return new FilteredEntrySortedMap<K, V>(
2333 sortedMap().tailMap(fromKey), predicate);
2334 }
2335 }
2336
2337
2338
2339
2340
2341 private static <K, V> BiMap<K, V> filterFiltered(
2342 FilteredEntryBiMap<K, V> map, Predicate<? super Entry<K, V>> entryPredicate) {
2343 Predicate<Entry<K, V>> predicate = Predicates.and(map.predicate, entryPredicate);
2344 return new FilteredEntryBiMap<K, V>(map.unfiltered(), predicate);
2345 }
2346
2347 static final class FilteredEntryBiMap<K, V> extends FilteredEntryMap<K, V>
2348 implements BiMap<K, V> {
2349 private final BiMap<V, K> inverse;
2350
2351 private static <K, V> Predicate<Entry<V, K>> inversePredicate(
2352 final Predicate<? super Entry<K, V>> forwardPredicate) {
2353 return new Predicate<Entry<V, K>>() {
2354 @Override
2355 public boolean apply(Entry<V, K> input) {
2356 return forwardPredicate.apply(
2357 Maps.immutableEntry(input.getValue(), input.getKey()));
2358 }
2359 };
2360 }
2361
2362 FilteredEntryBiMap(BiMap<K, V> delegate,
2363 Predicate<? super Entry<K, V>> predicate) {
2364 super(delegate, predicate);
2365 this.inverse = new FilteredEntryBiMap<V, K>(
2366 delegate.inverse(), inversePredicate(predicate), this);
2367 }
2368
2369 private FilteredEntryBiMap(
2370 BiMap<K, V> delegate, Predicate<? super Entry<K, V>> predicate,
2371 BiMap<V, K> inverse) {
2372 super(delegate, predicate);
2373 this.inverse = inverse;
2374 }
2375
2376 BiMap<K, V> unfiltered() {
2377 return (BiMap<K, V>) unfiltered;
2378 }
2379
2380 @Override
2381 public V forcePut(@Nullable K key, @Nullable V value) {
2382 checkArgument(apply(key, value));
2383 return unfiltered().forcePut(key, value);
2384 }
2385
2386 @Override
2387 public BiMap<V, K> inverse() {
2388 return inverse;
2389 }
2390
2391 @Override
2392 public Set<V> values() {
2393 return inverse.keySet();
2394 }
2395 }
2396
2397 @Nullable private static <K, V> Entry<K, V> unmodifiableOrNull(@Nullable Entry<K, V> entry) {
2398 return (entry == null) ? null : Maps.unmodifiableEntry(entry);
2399 }
2400
2401
2402
2403
2404
2405
2406
2407
2408 @GwtCompatible
2409 abstract static class ImprovedAbstractMap<K, V> extends AbstractMap<K, V> {
2410
2411
2412
2413
2414
2415 abstract Set<Entry<K, V>> createEntrySet();
2416
2417 private transient Set<Entry<K, V>> entrySet;
2418
2419 @Override public Set<Entry<K, V>> entrySet() {
2420 Set<Entry<K, V>> result = entrySet;
2421 return (result == null) ? entrySet = createEntrySet() : result;
2422 }
2423
2424 private transient Set<K> keySet;
2425
2426 @Override public Set<K> keySet() {
2427 Set<K> result = keySet;
2428 return (result == null) ? keySet = createKeySet() : result;
2429 }
2430
2431 Set<K> createKeySet() {
2432 return new KeySet<K, V>(this);
2433 }
2434
2435 private transient Collection<V> values;
2436
2437 @Override public Collection<V> values() {
2438 Collection<V> result = values;
2439 return (result == null) ? values = createValues() : result;
2440 }
2441
2442 Collection<V> createValues() {
2443 return new Values<K, V>(this);
2444 }
2445 }
2446
2447
2448
2449
2450
2451 static <V> V safeGet(Map<?, V> map, @Nullable Object key) {
2452 checkNotNull(map);
2453 try {
2454 return map.get(key);
2455 } catch (ClassCastException e) {
2456 return null;
2457 } catch (NullPointerException e) {
2458 return null;
2459 }
2460 }
2461
2462
2463
2464
2465
2466 static boolean safeContainsKey(Map<?, ?> map, Object key) {
2467 checkNotNull(map);
2468 try {
2469 return map.containsKey(key);
2470 } catch (ClassCastException e) {
2471 return false;
2472 } catch (NullPointerException e) {
2473 return false;
2474 }
2475 }
2476
2477
2478
2479
2480
2481 static <V> V safeRemove(Map<?, V> map, Object key) {
2482 checkNotNull(map);
2483 try {
2484 return map.remove(key);
2485 } catch (ClassCastException e) {
2486 return null;
2487 } catch (NullPointerException e) {
2488 return null;
2489 }
2490 }
2491
2492
2493
2494
2495 static boolean containsKeyImpl(Map<?, ?> map, @Nullable Object key) {
2496 return Iterators.contains(keyIterator(map.entrySet().iterator()), key);
2497 }
2498
2499
2500
2501
2502 static boolean containsValueImpl(Map<?, ?> map, @Nullable Object value) {
2503 return Iterators.contains(valueIterator(map.entrySet().iterator()), value);
2504 }
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519 static <K, V> boolean containsEntryImpl(Collection<Entry<K, V>> c, Object o) {
2520 if (!(o instanceof Entry)) {
2521 return false;
2522 }
2523 return c.contains(unmodifiableEntry((Entry<?, ?>) o));
2524 }
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539 static <K, V> boolean removeEntryImpl(Collection<Entry<K, V>> c, Object o) {
2540 if (!(o instanceof Entry)) {
2541 return false;
2542 }
2543 return c.remove(unmodifiableEntry((Entry<?, ?>) o));
2544 }
2545
2546
2547
2548
2549 static boolean equalsImpl(Map<?, ?> map, Object object) {
2550 if (map == object) {
2551 return true;
2552 } else if (object instanceof Map) {
2553 Map<?, ?> o = (Map<?, ?>) object;
2554 return map.entrySet().equals(o.entrySet());
2555 }
2556 return false;
2557 }
2558
2559 static final MapJoiner STANDARD_JOINER =
2560 Collections2.STANDARD_JOINER.withKeyValueSeparator("=");
2561
2562
2563
2564
2565 static String toStringImpl(Map<?, ?> map) {
2566 StringBuilder sb
2567 = Collections2.newStringBuilderForCollection(map.size()).append('{');
2568 STANDARD_JOINER.appendTo(sb, map);
2569 return sb.append('}').toString();
2570 }
2571
2572
2573
2574
2575 static <K, V> void putAllImpl(
2576 Map<K, V> self, Map<? extends K, ? extends V> map) {
2577 for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
2578 self.put(entry.getKey(), entry.getValue());
2579 }
2580 }
2581
2582 static class KeySet<K, V> extends Sets.ImprovedAbstractSet<K> {
2583 final Map<K, V> map;
2584
2585 KeySet(Map<K, V> map) {
2586 this.map = checkNotNull(map);
2587 }
2588
2589 Map<K, V> map() {
2590 return map;
2591 }
2592
2593 @Override public Iterator<K> iterator() {
2594 return keyIterator(map().entrySet().iterator());
2595 }
2596
2597 @Override public int size() {
2598 return map().size();
2599 }
2600
2601 @Override public boolean isEmpty() {
2602 return map().isEmpty();
2603 }
2604
2605 @Override public boolean contains(Object o) {
2606 return map().containsKey(o);
2607 }
2608
2609 @Override public boolean remove(Object o) {
2610 if (contains(o)) {
2611 map().remove(o);
2612 return true;
2613 }
2614 return false;
2615 }
2616
2617 @Override public void clear() {
2618 map().clear();
2619 }
2620 }
2621
2622 @Nullable
2623 static <K> K keyOrNull(@Nullable Entry<K, ?> entry) {
2624 return (entry == null) ? null : entry.getKey();
2625 }
2626
2627 @Nullable
2628 static <V> V valueOrNull(@Nullable Entry<?, V> entry) {
2629 return (entry == null) ? null : entry.getValue();
2630 }
2631
2632 static class SortedKeySet<K, V> extends KeySet<K, V> implements SortedSet<K> {
2633 SortedKeySet(SortedMap<K, V> map) {
2634 super(map);
2635 }
2636
2637 @Override
2638 SortedMap<K, V> map() {
2639 return (SortedMap<K, V>) super.map();
2640 }
2641
2642 @Override
2643 public Comparator<? super K> comparator() {
2644 return map().comparator();
2645 }
2646
2647 @Override
2648 public SortedSet<K> subSet(K fromElement, K toElement) {
2649 return new SortedKeySet<K, V>(map().subMap(fromElement, toElement));
2650 }
2651
2652 @Override
2653 public SortedSet<K> headSet(K toElement) {
2654 return new SortedKeySet<K, V>(map().headMap(toElement));
2655 }
2656
2657 @Override
2658 public SortedSet<K> tailSet(K fromElement) {
2659 return new SortedKeySet<K, V>(map().tailMap(fromElement));
2660 }
2661
2662 @Override
2663 public K first() {
2664 return map().firstKey();
2665 }
2666
2667 @Override
2668 public K last() {
2669 return map().lastKey();
2670 }
2671 }
2672
2673 static class Values<K, V> extends AbstractCollection<V> {
2674 final Map<K, V> map;
2675
2676 Values(Map<K, V> map) {
2677 this.map = checkNotNull(map);
2678 }
2679
2680 final Map<K, V> map() {
2681 return map;
2682 }
2683
2684 @Override public Iterator<V> iterator() {
2685 return valueIterator(map().entrySet().iterator());
2686 }
2687
2688 @Override public boolean remove(Object o) {
2689 try {
2690 return super.remove(o);
2691 } catch (UnsupportedOperationException e) {
2692 for (Entry<K, V> entry : map().entrySet()) {
2693 if (Objects.equal(o, entry.getValue())) {
2694 map().remove(entry.getKey());
2695 return true;
2696 }
2697 }
2698 return false;
2699 }
2700 }
2701
2702 @Override public boolean removeAll(Collection<?> c) {
2703 try {
2704 return super.removeAll(checkNotNull(c));
2705 } catch (UnsupportedOperationException e) {
2706 Set<K> toRemove = Sets.newHashSet();
2707 for (Entry<K, V> entry : map().entrySet()) {
2708 if (c.contains(entry.getValue())) {
2709 toRemove.add(entry.getKey());
2710 }
2711 }
2712 return map().keySet().removeAll(toRemove);
2713 }
2714 }
2715
2716 @Override public boolean retainAll(Collection<?> c) {
2717 try {
2718 return super.retainAll(checkNotNull(c));
2719 } catch (UnsupportedOperationException e) {
2720 Set<K> toRetain = Sets.newHashSet();
2721 for (Entry<K, V> entry : map().entrySet()) {
2722 if (c.contains(entry.getValue())) {
2723 toRetain.add(entry.getKey());
2724 }
2725 }
2726 return map().keySet().retainAll(toRetain);
2727 }
2728 }
2729
2730 @Override public int size() {
2731 return map().size();
2732 }
2733
2734 @Override public boolean isEmpty() {
2735 return map().isEmpty();
2736 }
2737
2738 @Override public boolean contains(@Nullable Object o) {
2739 return map().containsValue(o);
2740 }
2741
2742 @Override public void clear() {
2743 map().clear();
2744 }
2745 }
2746
2747 abstract static class EntrySet<K, V>
2748 extends Sets.ImprovedAbstractSet<Entry<K, V>> {
2749 abstract Map<K, V> map();
2750
2751 @Override public int size() {
2752 return map().size();
2753 }
2754
2755 @Override public void clear() {
2756 map().clear();
2757 }
2758
2759 @Override public boolean contains(Object o) {
2760 if (o instanceof Entry) {
2761 Entry<?, ?> entry = (Entry<?, ?>) o;
2762 Object key = entry.getKey();
2763 V value = Maps.safeGet(map(), key);
2764 return Objects.equal(value, entry.getValue())
2765 && (value != null || map().containsKey(key));
2766 }
2767 return false;
2768 }
2769
2770 @Override public boolean isEmpty() {
2771 return map().isEmpty();
2772 }
2773
2774 @Override public boolean remove(Object o) {
2775 if (contains(o)) {
2776 Entry<?, ?> entry = (Entry<?, ?>) o;
2777 return map().keySet().remove(entry.getKey());
2778 }
2779 return false;
2780 }
2781
2782 @Override public boolean removeAll(Collection<?> c) {
2783 try {
2784 return super.removeAll(checkNotNull(c));
2785 } catch (UnsupportedOperationException e) {
2786
2787 return Sets.removeAllImpl(this, c.iterator());
2788 }
2789 }
2790
2791 @Override public boolean retainAll(Collection<?> c) {
2792 try {
2793 return super.retainAll(checkNotNull(c));
2794 } catch (UnsupportedOperationException e) {
2795
2796 Set<Object> keys = Sets.newHashSetWithExpectedSize(c.size());
2797 for (Object o : c) {
2798 if (contains(o)) {
2799 Entry<?, ?> entry = (Entry<?, ?>) o;
2800 keys.add(entry.getKey());
2801 }
2802 }
2803 return map().keySet().retainAll(keys);
2804 }
2805 }
2806 }
2807 }