1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.multimap;
18
19 import java.util.Iterator;
20 import java.util.Map;
21 import java.util.Objects;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.FluentIterable;
25 import org.apache.commons.collections4.MultiValuedMap;
26 import org.apache.commons.collections4.Transformer;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class TransformedMultiValuedMap<K, V> extends AbstractMultiValuedMapDecorator<K, V> {
45
46
47 private static final long serialVersionUID = 20150612L;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public static <K, V> TransformedMultiValuedMap<K, V> transformedMap(final MultiValuedMap<K, V> map,
66 final Transformer<? super K, ? extends K> keyTransformer,
67 final Transformer<? super V, ? extends V> valueTransformer) {
68 final TransformedMultiValuedMap<K, V> decorated =
69 new TransformedMultiValuedMap<>(map, keyTransformer, valueTransformer);
70 if (!map.isEmpty()) {
71 final MultiValuedMap<K, V> mapCopy = new ArrayListValuedHashMap<>(map);
72 decorated.clear();
73 decorated.putAll(mapCopy);
74 }
75 return decorated;
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public static <K, V> TransformedMultiValuedMap<K, V> transformingMap(final MultiValuedMap<K, V> map,
94 final Transformer<? super K, ? extends K> keyTransformer,
95 final Transformer<? super V, ? extends V> valueTransformer) {
96 return new TransformedMultiValuedMap<>(map, keyTransformer, valueTransformer);
97 }
98
99
100 private final Transformer<? super K, ? extends K> keyTransformer;
101
102
103 private final Transformer<? super V, ? extends V> valueTransformer;
104
105
106
107
108
109
110
111
112
113
114
115
116 protected TransformedMultiValuedMap(final MultiValuedMap<K, V> map,
117 final Transformer<? super K, ? extends K> keyTransformer,
118 final Transformer<? super V, ? extends V> valueTransformer) {
119 super(map);
120 this.keyTransformer = keyTransformer;
121 this.valueTransformer = valueTransformer;
122 }
123
124 @Override
125 public boolean put(final K key, final V value) {
126 return decorated().put(transformKey(key), transformValue(value));
127 }
128
129 @Override
130 public boolean putAll(final K key, final Iterable<? extends V> values) {
131 Objects.requireNonNull(values, "values");
132
133 final Iterable<V> transformedValues = FluentIterable.of(values).transform(valueTransformer);
134 final Iterator<? extends V> it = transformedValues.iterator();
135 return it.hasNext() && CollectionUtils.addAll(decorated().get(transformKey(key)), it);
136 }
137
138 @Override
139 public boolean putAll(final Map<? extends K, ? extends V> map) {
140 Objects.requireNonNull(map, "map");
141 boolean changed = false;
142 for (final Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
143 changed |= put(entry.getKey(), entry.getValue());
144 }
145 return changed;
146 }
147
148 @Override
149 public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
150 Objects.requireNonNull(map, "map");
151 boolean changed = false;
152 for (final Map.Entry<? extends K, ? extends V> entry : map.entries()) {
153 changed |= put(entry.getKey(), entry.getValue());
154 }
155 return changed;
156 }
157
158
159
160
161
162
163
164
165
166 protected K transformKey(final K object) {
167 if (keyTransformer == null) {
168 return object;
169 }
170 return keyTransformer.transform(object);
171 }
172
173
174
175
176
177
178
179
180
181 protected V transformValue(final V object) {
182 if (valueTransformer == null) {
183 return object;
184 }
185 return valueTransformer.transform(object);
186 }
187
188 }