Fixing Checkstyle problems.
This commit is contained in:
parent
2ea44b2ada
commit
3ce3b27dbd
|
@ -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) -> m.invoke(o, args));
|
||||
* Streams.failable(stream).forEach((m) -> 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.
|
||||
|
@ -89,7 +90,7 @@ public class Streams {
|
|||
* element to determine if it should be included.
|
||||
* @return the new stream
|
||||
*/
|
||||
public FailableStream<O> filter(FailablePredicate<O,?> pPredicate){
|
||||
public FailableStream<O> filter(FailablePredicate<O, ?> pPredicate){
|
||||
assertNotTerminated();
|
||||
stream = stream.filter(Functions.asPredicate(pPredicate));
|
||||
return this;
|
||||
|
@ -110,7 +111,7 @@ public class Streams {
|
|||
*
|
||||
* @param pAction a non-interfering action to perform on the elements
|
||||
*/
|
||||
public void forEach(FailableConsumer<O,?> pAction) {
|
||||
public void forEach(FailableConsumer<O, ?> pAction) {
|
||||
makeTerminated();
|
||||
stream().forEach(Functions.asConsumer(pAction));
|
||||
}
|
||||
|
@ -163,7 +164,7 @@ public class Streams {
|
|||
* @see #collect(Supplier, BiConsumer, BiConsumer)
|
||||
* @see Collectors
|
||||
*/
|
||||
public <A,R> R collect(Collector<? super O,A,R> pCollector) {
|
||||
public <A, R> R collect(Collector<? super O, A, R> pCollector) {
|
||||
makeTerminated();
|
||||
return stream().collect(pCollector);
|
||||
}
|
||||
|
@ -213,7 +214,7 @@ public class Streams {
|
|||
* accumulator function
|
||||
* @return The result of the reduction
|
||||
*/
|
||||
public <A,R> R collect(Supplier<R> pSupplier, BiConsumer<R,? super O> pAccumulator, BiConsumer<R,R> pCombiner) {
|
||||
public <A, R> R collect(Supplier<R> pSupplier, BiConsumer<R, ? super O> pAccumulator, BiConsumer<R, R> pCombiner) {
|
||||
makeTerminated();
|
||||
return stream().collect(pSupplier, pAccumulator, pCombiner);
|
||||
}
|
||||
|
@ -276,7 +277,7 @@ public class Streams {
|
|||
* @param pMapper A non-interfering, stateless function to apply to each element
|
||||
* @return the new stream
|
||||
*/
|
||||
public <R> FailableStream<R> map(FailableFunction<O,R,?> pMapper) {
|
||||
public <R> FailableStream<R> map(FailableFunction<O, R, ?> pMapper) {
|
||||
assertNotTerminated();
|
||||
return new FailableStream<R>(stream.map(Functions.asFunction(pMapper)));
|
||||
}
|
||||
|
@ -308,7 +309,7 @@ public class Streams {
|
|||
* @return {@code true} If either all elements of the stream match the
|
||||
* provided predicate or the stream is empty, otherwise {@code false}.
|
||||
*/
|
||||
public boolean allMatch(FailablePredicate<O,?> pPredicate) {
|
||||
public boolean allMatch(FailablePredicate<O, ?> pPredicate) {
|
||||
assertNotTerminated();
|
||||
return stream().allMatch(Functions.asPredicate(pPredicate));
|
||||
}
|
||||
|
@ -330,7 +331,7 @@ public class Streams {
|
|||
* @return {@code true} if any elements of the stream match the provided
|
||||
* predicate, otherwise {@code false}
|
||||
*/
|
||||
public boolean anyMatch(FailablePredicate<O,?> pPredicate) {
|
||||
public boolean anyMatch(FailablePredicate<O, ?> pPredicate) {
|
||||
assertNotTerminated();
|
||||
return stream().anyMatch(Functions.asPredicate(pPredicate));
|
||||
}
|
||||
|
@ -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<O> list;
|
||||
* final Method m;
|
||||
* final Function<O,String> mapper = (o) -> {
|
||||
* try {
|
||||
* return (String) m.invoke(o);
|
||||
* } catch (Throwable t) {
|
||||
* throw Functions.rethrow(t);
|
||||
* }
|
||||
* };
|
||||
* final List<String> strList = list.stream()
|
||||
* .map(mapper).collect(Collectors.toList());
|
||||
* </pre>
|
||||
* as follows:
|
||||
* <pre>
|
||||
* final List<O> list;
|
||||
* final Method m;
|
||||
* final List<String> strList = Functions.stream(list.stream())
|
||||
* .map((o) -> (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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
@ -62,7 +64,7 @@ class StreamsTest {
|
|||
}
|
||||
}
|
||||
|
||||
protected <T extends Throwable> FailableConsumer<String,T> asIntConsumer(T pThrowable) {
|
||||
protected <T extends Throwable> FailableConsumer<String, T> asIntConsumer(T pThrowable) {
|
||||
return (s) -> {
|
||||
final Integer i = Integer.valueOf(s);
|
||||
if (i.intValue() == 4) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -117,7 +121,7 @@ class StreamsTest {
|
|||
}
|
||||
}
|
||||
|
||||
protected <T extends Throwable> FailablePredicate<Integer,T> asIntPredicate(T pThrowable) {
|
||||
protected <T extends Throwable> FailablePredicate<Integer, T> asIntPredicate(T pThrowable) {
|
||||
return (i) -> {
|
||||
if (i.intValue() == 5) {
|
||||
if (pThrowable != null) {
|
||||
|
|
Loading…
Reference in New Issue