Fixing Checkstyle problems.

This commit is contained in:
Jochen Wiedmann 2020-02-07 21:59:23 +01:00
parent 2ea44b2ada
commit 3ce3b27dbd
2 changed files with 346 additions and 299 deletions

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.lang3;
import java.util.Collection;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
@ -48,7 +49,7 @@ import org.apache.commons.lang3.Functions.FailablePredicate;
* </pre>
* Using a {@link FailableStream}, this can be rewritten as follows:
* <pre>
* ObjectStreams.failable(stream).forEach((m) -&gt; m.invoke(o, args));
* Streams.failable(stream).forEach((m) -&gt; m.invoke(o, args));
* </pre>
* Obviously, the second version is much more concise and the spirit of
* Lambda expressions is met better than in the first version.
@ -377,4 +378,46 @@ public class Streams {
public static <O> FailableStream<O> stream(Stream<O> pStream) {
return new FailableStream<O>(pStream);
}
/**
* Converts the given {@link Collection} into a {@link FailableStream}.
* This is basically a simplified, reduced version of the {@link Stream}
* class, with the same underlying element stream, except that failable
* objects, like {@link FailablePredicate}, {@link FailableFunction}, or
* {@link FailableConsumer} may be applied, instead of
* {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is
* to rewrite a code snippet like this:
* <pre>
* final List&lt;O&gt; list;
* final Method m;
* final Function&lt;O,String&gt; mapper = (o) -&gt; {
* try {
* return (String) m.invoke(o);
* } catch (Throwable t) {
* throw Functions.rethrow(t);
* }
* };
* final List&lt;String&gt; strList = list.stream()
* .map(mapper).collect(Collectors.toList());
* </pre>
* as follows:
* <pre>
* final List&lt;O&gt; list;
* final Method m;
* final List&lt;String&gt; strList = Functions.stream(list.stream())
* .map((o) -&gt; (String) m.invoke(o)).collect(Collectors.toList());
* </pre>
* While the second version may not be <em>quite</em> as
* efficient (because it depends on the creation of additional,
* intermediate objects, of type FailableStream), it is much more
* concise, and readable, and meets the spirit of Lambdas better
* than the first version.
* @param <O> The streams element type.
* @param pStream The stream, which is being converted.
* @return The {@link FailableStream}, which has been created by
* converting the stream.
*/
public static <O> FailableStream<O> stream(Collection<O> pStream) {
return stream(pStream.stream());
}
}

View File

@ -16,7 +16,9 @@
*/
package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
@ -105,7 +107,9 @@ class StreamsTest {
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
final List<Integer> output = Functions.stream(input)
.map((s) -> Integer.valueOf(s))
.filter((i) -> { return i.intValue() %2 == 0;})
.filter((i) -> {
return i.intValue() %2 == 0;
})
.collect(Collectors.toList());
assertEvenNumbers(output);
}