From 118652d768eb3bb92717dc1520e2cd272f7e068e Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 25 Jul 2024 08:26:41 -0400 Subject: [PATCH] Add LangCollectors.collect(Collector, T...) --- src/changes/changes.xml | 1 + .../commons/lang3/stream/LangCollectors.java | 20 +++ .../lang3/stream/LangCollectorsTest.java | 121 ++++++++++++++++++ 3 files changed, 142 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 27bda2091..5fd88d6a2 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -56,6 +56,7 @@ The type attribute can be add,update,fix,remove. Add StopWatch.getStopInstant() and deprecate getStopTime(). Add StopWatch.getDuration() and deprecate getTime(). Add Javadoc links from StopWatch to DurationUtils #1249. + Add LangCollectors.collect(Collector, T...). diff --git a/src/main/java/org/apache/commons/lang3/stream/LangCollectors.java b/src/main/java/org/apache/commons/lang3/stream/LangCollectors.java index b52f8da84..28af06992 100644 --- a/src/main/java/org/apache/commons/lang3/stream/LangCollectors.java +++ b/src/main/java/org/apache/commons/lang3/stream/LangCollectors.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.stream; +import java.util.Arrays; import java.util.Collections; import java.util.Objects; import java.util.Set; @@ -27,6 +28,7 @@ import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collector; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.lang3.StringUtils; @@ -91,6 +93,24 @@ public final class LangCollectors { private static final Set CH_NOID = Collections.emptySet(); + /** + * Delegates to {@link Stream#collect(Collector)} for a Stream on the given array. + * + * @param The type of the array elements. + * @param the type of the result. + * @param the intermediate accumulation type of the {@code Collector}. + * @param collector the {@code Collector} describing the reduction. + * @param array The array, assumed to be unmodified during use. + * @return the result of the reduction + * @see Stream#collect(Collector) + * @see Arrays#stream(Object[]) + * @see Collectors + * @since 3.16.0 + */ + public static R collect(final Collector collector, final T... array) { + return Arrays.stream(array).collect(collector); + } + /** * Returns a {@code Collector} that concatenates the input elements, separated by the specified delimiter, in encounter order. *

diff --git a/src/test/java/org/apache/commons/lang3/stream/LangCollectorsTest.java b/src/test/java/org/apache/commons/lang3/stream/LangCollectorsTest.java index b8bdc3355..486963d8e 100644 --- a/src/test/java/org/apache/commons/lang3/stream/LangCollectorsTest.java +++ b/src/test/java/org/apache/commons/lang3/stream/LangCollectorsTest.java @@ -19,6 +19,7 @@ package org.apache.commons.lang3.stream; import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.Arrays; import java.util.Objects; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; @@ -57,8 +58,111 @@ public class LangCollectorsTest { private static final Collector JOINING_4 = LangCollectors.joining("-", "<", ">", TO_STRING); private static final Collector JOINING_4_NUL = LangCollectors.joining("-", "<", ">", o -> Objects.toString(o, "NUL")); + private String join0(final Object... objects) { + return LangCollectors.collect(JOINING_0, objects); + } + + private String join1(final Object... objects) { + return LangCollectors.collect(JOINING_1, objects); + } + + private String join3(final Object... objects) { + return LangCollectors.collect(JOINING_3, objects); + } + + private String join4(final Object... objects) { + return LangCollectors.collect(JOINING_4, objects); + } + + private String join4Nul(final Object... objects) { + return LangCollectors.collect(JOINING_4_NUL, objects); + } + + @Test + public void testCollectStrings1Arg() { + assertEquals("", join1()); + assertEquals("1", join1("1")); + assertEquals("1-2", join1("1", "2")); + assertEquals("1-2-3", join1("1", "2", "3")); + assertEquals("1-null-3", join1("1", null, "3")); + } + + @Test + public void testJoinCollectNonStrings0Arg() { + assertEquals("", join0()); + assertEquals("1", join0(_1L)); + assertEquals("12", join0(_1L, _2L)); + assertEquals("123", join0(_1L, _2L, _3L)); + assertEquals("1null3", join0(_1L, null, _3L)); + assertEquals("12", join0(new AtomicLong(1), new AtomicLong(2))); + assertEquals("12", join0(new Fixture(1), new Fixture(2))); + } + + @Test + public void testJoinCollectNonStrings1Arg() { + assertEquals("", join1()); + assertEquals("1", join1(_1L)); + assertEquals("1-2", join1(_1L, _2L)); + assertEquals("1-2-3", join1(_1L, _2L, _3L)); + assertEquals("1-null-3", join1(_1L, null, _3L)); + assertEquals("1-2", join1(new AtomicLong(1), new AtomicLong(2))); + assertEquals("1-2", join1(new Fixture(1), new Fixture(2))); + } + + @Test + public void testJoinCollectNonStrings3Args() { + assertEquals("<>", join3()); + assertEquals("<1>", join3(_1L)); + assertEquals("<1-2>", join3(_1L, _2L)); + assertEquals("<1-2-3>", join3(_1L, _2L, _3L)); + assertEquals("<1-null-3>", join3(_1L, null, _3L)); + assertEquals("<1-2>", join3(new AtomicLong(1), new AtomicLong(2))); + assertEquals("<1-2>", join3(new Fixture(1), new Fixture(2))); + } + + @Test + public void testJoinCollectNonStrings4Args() { + assertEquals("<>", join4()); + assertEquals("<1>", join4(_1L)); + assertEquals("<1-2>", join4(_1L, _2L)); + assertEquals("<1-2-3>", join4(_1L, _2L, _3L)); + assertEquals("<1-null-3>", join4(_1L, null, _3L)); + assertEquals("<1-NUL-3>", join4Nul(_1L, null, _3L)); + assertEquals("<1-2>", join4(new AtomicLong(1), new AtomicLong(2))); + assertEquals("<1-2>", join4(new Fixture(1), new Fixture(2))); + } + + @Test + public void testJoinCollectStrings0Arg() { + assertEquals("", join0()); + assertEquals("1", join0("1")); + assertEquals("12", join0("1", "2")); + assertEquals("123", join0("1", "2", "3")); + assertEquals("1null3", join0("1", null, "3")); + } + + @Test + public void testJoinCollectStrings3Args() { + assertEquals("<>", join3()); + assertEquals("<1>", join3("1")); + assertEquals("<1-2>", join3("1", "2")); + assertEquals("<1-2-3>", join3("1", "2", "3")); + assertEquals("<1-null-3>", join3("1", null, "3")); + } + + @Test + public void testJoinCollectStrings4Args() { + assertEquals("<>", join4()); + assertEquals("<1>", join4("1")); + assertEquals("<1-2>", join4("1", "2")); + assertEquals("<1-2-3>", join4("1", "2", "3")); + assertEquals("<1-null-3>", join4("1", null, "3")); + assertEquals("<1-NUL-3>", join4Nul("1", null, "3")); + } + @Test public void testJoiningNonStrings0Arg() { + // Stream.of() assertEquals("", Stream.of().collect(JOINING_0)); assertEquals("1", Stream.of(_1L).collect(JOINING_0)); assertEquals("12", Stream.of(_1L, _2L).collect(JOINING_0)); @@ -66,10 +170,19 @@ public class LangCollectorsTest { assertEquals("1null3", Stream.of(_1L, null, _3L).collect(JOINING_0)); assertEquals("12", Stream.of(new AtomicLong(1), new AtomicLong(2)).collect(JOINING_0)); assertEquals("12", Stream.of(new Fixture(1), new Fixture(2)).collect(JOINING_0)); + // Arrays.stream() + assertEquals("", Arrays.stream(new Object[] {}).collect(JOINING_0)); + assertEquals("1", Arrays.stream(new Long[] { _1L }).collect(JOINING_0)); + assertEquals("12", Arrays.stream(new Long[] { _1L, _2L }).collect(JOINING_0)); + assertEquals("123", Arrays.stream(new Long[] { _1L, _2L, _3L }).collect(JOINING_0)); + assertEquals("1null3", Arrays.stream(new Long[] { _1L, null, _3L }).collect(JOINING_0)); + assertEquals("12", Arrays.stream(new AtomicLong[] { new AtomicLong(1), new AtomicLong(2) }).collect(JOINING_0)); + assertEquals("12", Arrays.stream(new Fixture[] { new Fixture(1), new Fixture(2) }).collect(JOINING_0)); } @Test public void testJoiningNonStrings1Arg() { + // Stream.of() assertEquals("", Stream.of().collect(JOINING_1)); assertEquals("1", Stream.of(_1L).collect(JOINING_1)); assertEquals("1-2", Stream.of(_1L, _2L).collect(JOINING_1)); @@ -77,6 +190,14 @@ public class LangCollectorsTest { assertEquals("1-null-3", Stream.of(_1L, null, _3L).collect(JOINING_1)); assertEquals("1-2", Stream.of(new AtomicLong(1), new AtomicLong(2)).collect(JOINING_1)); assertEquals("1-2", Stream.of(new Fixture(1), new Fixture(2)).collect(JOINING_1)); + // Arrays.stream() + assertEquals("", Arrays.stream(new Object[] {}).collect(JOINING_1)); + assertEquals("1", Arrays.stream(new Long[] { _1L }).collect(JOINING_1)); + assertEquals("1-2", Arrays.stream(new Long[] { _1L, _2L }).collect(JOINING_1)); + assertEquals("1-2-3", Arrays.stream(new Long[] { _1L, _2L, _3L }).collect(JOINING_1)); + assertEquals("1-null-3", Arrays.stream(new Long[] { _1L, null, _3L }).collect(JOINING_1)); + assertEquals("1-2", Arrays.stream(new AtomicLong[] { new AtomicLong(1), new AtomicLong(2) }).collect(JOINING_1)); + assertEquals("1-2", Arrays.stream(new Fixture[] { new Fixture(1), new Fixture(2) }).collect(JOINING_1)); } @Test