Add Streams.failableStream(T...)
This commit is contained in:
parent
11d2deca1f
commit
4afdad4f1a
|
@ -70,6 +70,7 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add LocaleUtils.isLanguageUndetermined(Locale).</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ObjectUtils.toString(Supplier<Object>, Supplier<String>).</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add LazyInitializer.isInitialized().</action>
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.failableStream(T...).</action>
|
||||
<!-- UPDATE -->
|
||||
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 58 to 64.</action>
|
||||
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.easymock:easymock from 5.1.0 to 5.2.0 #1104.</action>
|
||||
|
|
|
@ -579,6 +579,19 @@ public class Streams {
|
|||
return new FailableStream<>(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand for {@code Streams.failableStream(Streams.of(arrayValues))}.
|
||||
*
|
||||
* @param <T> 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 <T> FailableStream<T> failableStream(final T... values) {
|
||||
return failableStream(of(values));
|
||||
}
|
||||
|
||||
/**
|
||||
* Streams only instances of the give Class in a collection.
|
||||
* <p>
|
||||
|
|
|
@ -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<String> 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]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue