From 9cadb50991f1ae1833493f6c07ed8d31f6e27a63 Mon Sep 17 00:00:00 2001 From: furplag Date: Thu, 29 Nov 2018 17:07:46 +0900 Subject: [PATCH 1/3] add Streamr#collect for Map . --- .../java/jp/furplag/sandbox/stream/Streamr.java | 17 +++++++++++++++++ .../jp/furplag/sandbox/stream/StreamrTest.java | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/main/java/jp/furplag/sandbox/stream/Streamr.java b/src/main/java/jp/furplag/sandbox/stream/Streamr.java index 6b0adea..8c50d04 100644 --- a/src/main/java/jp/furplag/sandbox/stream/Streamr.java +++ b/src/main/java/jp/furplag/sandbox/stream/Streamr.java @@ -18,8 +18,11 @@ import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; @@ -27,6 +30,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.apache.commons.lang3.tuple.Pair; + import jp.furplag.function.ThrowablePredicate; /** @@ -48,6 +53,18 @@ static > R collect(final Stream stream, final Supp return stream(stream).collect(Collectors.toCollection(supplier)); } + /** + * we might to use this many . + * + * @param the type of the key of stream elements + * @param the type of the value of stream elements + * @param entries {@link Stream} of {@link Pair}, maybe null + * @return {@link Map} + */ + static Map collect(final Stream> entries, BinaryOperator mergeFunction, final Supplier> supplier) { + return Streamr.stream(entries).collect(Collectors.toMap(Pair::getLeft, Pair::getRight, Objects.requireNonNullElse(mergeFunction, (current, next) -> next), Objects.requireNonNullElse(supplier, HashMap::new))); + } + /** * do less coding in case of {@link Collection#stream()} . * diff --git a/src/test/java/jp/furplag/sandbox/stream/StreamrTest.java b/src/test/java/jp/furplag/sandbox/stream/StreamrTest.java index b8c7cf7..38d0251 100644 --- a/src/test/java/jp/furplag/sandbox/stream/StreamrTest.java +++ b/src/test/java/jp/furplag/sandbox/stream/StreamrTest.java @@ -23,17 +23,21 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.Deque; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Objects; import java.util.Queue; import java.util.Set; import java.util.concurrent.DelayQueue; import java.util.function.Function; +import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; +import org.apache.commons.lang3.tuple.Pair; import org.junit.Test; import jp.furplag.sandbox.stream.Streamr.Filter.FilteringMode; @@ -148,6 +152,17 @@ public void testToSet() { assertEquals(new HashSet<>(Arrays.asList(new Integer[] { 1, 2, 3 })), Streamr.collect(Streamr.stream(new Integer[] { 1, null, 2, null, 3 }), HashSet::new)); } + @Test + public void testToMap() { + assertEquals(Collections.emptyMap(), Streamr.collect(Stream.of((Pair) null), null, null)); + assertThat(Streamr.collect(Stream.of((Pair) null, Pair.of(1, 2)), null, null).toString(), is("{1=2}")); + assertEquals(Stream.of(1, 2).map((x) -> Pair.of(x, x)).collect(Collectors.toMap(Pair::getLeft, Pair::getRight)), Streamr.collect(Stream.of((Pair) null, Pair.of(2, 2), Pair.of(1, 1)), null, null)); + assertThat(Streamr.collect(Stream.of((Pair) null, Pair.of(1, 2), Pair.of(1, 1)), null, null).toString(), is("{1=1}")); + assertThat(Streamr.collect(Stream.of((Pair) null, Pair.of(1, 2), Pair.of(1, 1)), (a, b) -> a, null).toString(), is("{1=2}")); + assertThat(Streamr.collect(Stream.of((Pair) null, Pair.of(2, 2), Pair.of(1, 1), Pair.of(1, 2)), (a, b) -> b, LinkedHashMap::new).toString(), is("{2=2, 1=2}")); + assertThat(Streamr.collect(Stream.of((Pair) Pair.of(2, 2), Pair.of(1, 1), Pair.of(1, 2)).sorted(Comparator.comparing(Pair::getLeft)), (a, b) -> b, LinkedHashMap::new).toString(), is("{1=2, 2=2}")); + } + @Test public void testFilteringMode() { assertThat(Streamr.Filter.FilteringMode.And.and(), is(true)); From 8c3125c1823a91b1f313225e70c6c8d88ced1062 Mon Sep 17 00:00:00 2001 From: furplag Date: Mon, 3 Dec 2018 13:05:11 +0900 Subject: [PATCH 2/3] typo . --- src/main/java/jp/furplag/sandbox/reflect/SavageReflection.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/jp/furplag/sandbox/reflect/SavageReflection.java b/src/main/java/jp/furplag/sandbox/reflect/SavageReflection.java index 8ae967f..72f0d8c 100644 --- a/src/main/java/jp/furplag/sandbox/reflect/SavageReflection.java +++ b/src/main/java/jp/furplag/sandbox/reflect/SavageReflection.java @@ -20,7 +20,6 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; -import java.util.function.Predicate; import org.apache.commons.lang3.ArrayUtils; From 7e23acd1c307d1058c1d26b4ede47f84bd796a73 Mon Sep 17 00:00:00 2001 From: furplag Date: Mon, 3 Dec 2018 13:09:20 +0900 Subject: [PATCH 3/3] release 2.1.0 . --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f7f5a40..f42aee1 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ 4.0.0 jp.furplag.sandbox relic - 2.1.0-SNAPSHOT + 2.1.0 jar 2018 ${project.artifactId}