Skip to content

Commit 4079f86

Browse files
committed
[COLLECTIONS-880] Add MultiMapUtils.invert(Map, MultiValuedMap) overload
1 parent c1f3cb2 commit 4079f86

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/main/java/org/apache/commons/collections4/MultiMapUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,26 @@ M invert(MultiValuedMap<? extends V, ? extends K> input, M output) {
179179
return output;
180180
}
181181

182+
/**
183+
* A utility method to invert the mappings from an input Map
184+
* and add them to an output MultiValuedMap. Use this method to have complete
185+
* control of the output MultiValuedMap or when merging several inverse mappings.
186+
*
187+
* @param input take key-to-value mappings from here
188+
* @param output add value-to-key mappings here
189+
* @param <K> the output MultiValuedMap key type and the input Map value type
190+
* @param <V> the output MultiValuedMap value type and the input Map key type
191+
* @param <M> the output MultiValuedMap with key and value types reversed compared with input
192+
* @return the updated output MultiValuedMap
193+
*/
194+
public static <K, V, M extends MultiValuedMap<K, V>>
195+
M invert(Map<? extends V, ? extends K> input, M output) {
196+
for (Map.Entry<? extends V, ? extends K> e : input.entrySet()) {
197+
output.put(e.getValue(), e.getKey());
198+
}
199+
return output;
200+
}
201+
182202
/**
183203
* Null-safe check if the specified {@code MultiValuedMap} is empty.
184204
* <p>

src/test/java/org/apache/commons/collections4/MultiMapUtilsTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424

2525
import java.util.Arrays;
2626
import java.util.Collection;
27+
import java.util.HashMap;
2728
import java.util.HashSet;
2829
import java.util.List;
30+
import java.util.Map;
2931
import java.util.Set;
3032

3133
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
@@ -119,7 +121,7 @@ void testGetValuesAsSet() {
119121
}
120122

121123
@Test
122-
void testInvert() {
124+
void testInvertMultiValuedMap() {
123125
final HashSetValuedHashMap<String, String> usages = new HashSetValuedHashMap<>();
124126

125127
final LinkedHashSetValuedLinkedHashMap<String, String> deps = new LinkedHashSetValuedLinkedHashMap<>();
@@ -142,6 +144,30 @@ void testInvert() {
142144
assertEquals("[commons-collections, commons-configuration2]", codecUsagesAll.toString());
143145
}
144146

147+
@Test
148+
void testInvertMap() {
149+
final HashSetValuedHashMap<String, String> usages = new HashSetValuedHashMap<>();
150+
151+
final Map<String, String> deps = new HashMap<>();
152+
deps.put("commons-configuration1", "commons-logging");
153+
deps.put("commons-configuration2", "commons-lang3");
154+
deps.put("commons-configuration3", "commons-text");
155+
deps.put("commons-beanutils1", "commons-collections");
156+
deps.put("commons-beanutils2", "commons-logging");
157+
MultiMapUtils.invert(deps, usages);
158+
final Set<String> loggingUsagesCompile = usages.get("commons-logging");
159+
assertEquals("[commons-beanutils2, commons-configuration1]", loggingUsagesCompile.toString());
160+
final Set<String> codecUsagesCompile = usages.get("commons-codec");
161+
assertEquals("[]", codecUsagesCompile.toString());
162+
163+
final Map<String, String> optionalDeps = new HashMap<>();
164+
optionalDeps.put("commons-configuration2", "commons-codec");
165+
optionalDeps.put("commons-collections", "commons-codec");
166+
MultiMapUtils.invert(optionalDeps, usages);
167+
final Set<String> codecUsagesAll = usages.get("commons-codec");
168+
assertEquals("[commons-collections, commons-configuration2]", codecUsagesAll.toString());
169+
}
170+
145171
@Test
146172
void testIsEmptyWithEmptyMap() {
147173
assertTrue(MultiMapUtils.isEmpty(new ArrayListValuedHashMap<>()));

0 commit comments

Comments
 (0)