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
66 public static <K, V> TransformedMultiValuedMap<K, V> transformedMap(final MultiValuedMap<K, V> map,
67 final Transformer<? super K, ? extends K> keyTransformer,
68 final Transformer<? super V, ? extends V> valueTransformer) {
69 final TransformedMultiValuedMap<K, V> decorated =
70 new TransformedMultiValuedMap<>(map, keyTransformer, valueTransformer);
71 if (!map.isEmpty()) {
72 final MultiValuedMap<K, V> mapCopy = new ArrayListValuedHashMap<>(map);
73 decorated.clear();
74 decorated.putAll(mapCopy);
75 }
76 return decorated;
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public static <K, V> TransformedMultiValuedMap<K, V> transformingMap(final MultiValuedMap<K, V> map,
96 final Transformer<? super K, ? extends K> keyTransformer,
97 final Transformer<? super V, ? extends V> valueTransformer) {
98 return new TransformedMultiValuedMap<>(map, keyTransformer, valueTransformer);
99 }
100
101
102 private final Transformer<? super K, ? extends K> keyTransformer;
103
104
105 private final Transformer<? super V, ? extends V> valueTransformer;
106
107
108
109
110
111
112
113
114
115
116
117
118
119 protected TransformedMultiValuedMap(final MultiValuedMap<K, V> map,
120 final Transformer<? super K, ? extends K> keyTransformer,
121 final Transformer<? super V, ? extends V> valueTransformer) {
122 super(map);
123 this.keyTransformer = keyTransformer;
124 this.valueTransformer = valueTransformer;
125 }
126
127 @Override
128 public boolean put(final K key, final V value) {
129 return decorated().put(transformKey(key), transformValue(value));
130 }
131
132 @Override
133 public boolean putAll(final K key, final Iterable<? extends V> values) {
134 Objects.requireNonNull(values, "values");
135
136 final Iterable<V> transformedValues = FluentIterable.of(values).transform(valueTransformer);
137 final Iterator<? extends V> it = transformedValues.iterator();
138 return it.hasNext() && CollectionUtils.addAll(decorated().get(transformKey(key)), it);
139 }
140
141 @Override
142 public boolean putAll(final Map<? extends K, ? extends V> map) {
143 Objects.requireNonNull(map, "map");
144 boolean changed = false;
145 for (final Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
146 changed |= put(entry.getKey(), entry.getValue());
147 }
148 return changed;
149 }
150
151 @Override
152 public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
153 Objects.requireNonNull(map, "map");
154 boolean changed = false;
155 for (final Map.Entry<? extends K, ? extends V> entry : map.entries()) {
156 changed |= put(entry.getKey(), entry.getValue());
157 }
158 return changed;
159 }
160
161
162
163
164
165
166
167
168
169
170 protected K transformKey(final K object) {
171 if (keyTransformer == null) {
172 return object;
173 }
174 return keyTransformer.apply(object);
175 }
176
177
178
179
180
181
182
183
184
185
186 protected V transformValue(final V object) {
187 if (valueTransformer == null) {
188 return object;
189 }
190 return valueTransformer.apply(object);
191 }
192
193 }