From 4afdad4f1a1909bddffa8b776f8c5e12b20445b5 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Fri, 13 Oct 2023 10:59:10 -0400 Subject: [PATCH] Add Streams.failableStream(T...) --- src/changes/changes.xml | 1 + .../apache/commons/lang3/stream/Streams.java | 13 ++++ .../lang3/stream/FailableStreamTest.java | 66 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/test/java/org/apache/commons/lang3/stream/FailableStreamTest.java diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0e7820796..4e6e6c479 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -70,6 +70,7 @@ The type attribute can be add,update,fix,remove. Add LocaleUtils.isLanguageUndetermined(Locale). Add ObjectUtils.toString(Supplier<Object>, Supplier<String>). Add LazyInitializer.isInitialized(). + Add Streams.failableStream(T...). Bump commons-parent from 58 to 64. Bump org.easymock:easymock from 5.1.0 to 5.2.0 #1104. diff --git a/src/main/java/org/apache/commons/lang3/stream/Streams.java b/src/main/java/org/apache/commons/lang3/stream/Streams.java index fa639ec2a..aa783eb8f 100644 --- a/src/main/java/org/apache/commons/lang3/stream/Streams.java +++ b/src/main/java/org/apache/commons/lang3/stream/Streams.java @@ -579,6 +579,19 @@ public class Streams { return new FailableStream<>(stream); } + /** + * Shorthand for {@code Streams.failableStream(Streams.of(arrayValues))}. + * + * @param the type of stream elements. + * @param values the elements of the new stream, may be {@code null}. + * @return the new FailableStream on {@code values} or an empty stream. + * @since 3.14.0 + */ + @SafeVarargs // Creating a stream from an array is safe + public static FailableStream failableStream(final T... values) { + return failableStream(of(values)); + } + /** * Streams only instances of the give Class in a collection. *

diff --git a/src/test/java/org/apache/commons/lang3/stream/FailableStreamTest.java b/src/test/java/org/apache/commons/lang3/stream/FailableStreamTest.java new file mode 100644 index 000000000..6944bda73 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/stream/FailableStreamTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.stream; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Locale; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.stream.Streams.FailableStream; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link FailableStream}. + */ +public class FailableStreamTest { + + private String failable(final String value) throws IOException { + if (value == new Object()) { + throw new IOException(); + } + return value.toLowerCase(Locale.ROOT); + } + + @Test + public void testFailableStreamOfArray() { + assertArrayEquals(new String[] {}, toArray()); + assertArrayEquals(new String[] { "a" }, toArray("A")); + assertArrayEquals(new String[] { "a", "b" }, toArray("A", "B")); + assertArrayEquals(new String[] { "a", "b", "c" }, toArray("A", "B", "C")); + } + + @Test + public void testFailableStreamOfCollection() { + assertArrayEquals(new String[] {}, toArray()); + assertArrayEquals(new String[] { "a" }, toArray(Arrays.asList("A"))); + assertArrayEquals(new String[] { "a", "b" }, toArray(Arrays.asList("A", "B"))); + assertArrayEquals(new String[] { "a", "b", "c" }, toArray(Arrays.asList("A", "B", "C"))); + } + + private String[] toArray(final Collection strings) { + return Streams.failableStream(strings).map(this::failable).collect(Collectors.toList()).toArray(new String[0]); + } + + private String[] toArray(final String... strings) { + return Streams.failableStream(strings).map(this::failable).collect(Collectors.toList()).toArray(new String[0]); + } +}