From 324302c53628f21b9a8bd4484d3a9414d25bf0cc Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 5 Jun 2023 19:44:47 +0200 Subject: [PATCH] Add MultiMap.flatten() --- .../java/org/pgpainless/util/MultiMap.java | 13 ++++++++++++ .../org/pgpainless/util/MultiMapTest.java | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/MultiMap.java b/pgpainless-core/src/main/java/org/pgpainless/util/MultiMap.java index ab3c7e67..3b342778 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/MultiMap.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/MultiMap.java @@ -100,6 +100,19 @@ public class MultiMap { return map.entrySet(); } + /** + * Return all values of the {@link MultiMap} in a single {@link LinkedHashSet}. + * + * @return set of all values + */ + public Set flatten() { + LinkedHashSet flattened = new LinkedHashSet<>(); + for (Set items : map.values()) { + flattened.addAll(items); + } + return flattened; + } + @Override public boolean equals(Object o) { if (o == null) { diff --git a/pgpainless-core/src/test/java/org/pgpainless/util/MultiMapTest.java b/pgpainless-core/src/test/java/org/pgpainless/util/MultiMapTest.java index 74c3347f..5bc0c1f7 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/util/MultiMapTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/util/MultiMapTest.java @@ -14,6 +14,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; import org.junit.jupiter.api.Test; @@ -156,4 +158,23 @@ public class MultiMapTest { assertFalse(map.containsKey("foo")); assertTrue(map.containsKey("bingo")); } + + @Test + public void flattenEmptyMap() { + MultiMap empty = new MultiMap<>(); + assertEquals(Collections.emptySet(), empty.flatten()); + } + + @Test + public void flattenMap() { + MultiMap map = new MultiMap<>(); + map.put("A", "1"); + map.put("A", "2"); + map.put("B", "1"); + + Set expected = new LinkedHashSet<>(); + expected.add("1"); + expected.add("2"); + assertEquals(expected, map.flatten()); + } }