Fixing Checkstyle problems.
This commit is contained in:
parent
2ea44b2ada
commit
3ce3b27dbd
|
@ -16,6 +16,7 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.lang3;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.BinaryOperator;
|
import java.util.function.BinaryOperator;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
@ -48,7 +49,7 @@ import org.apache.commons.lang3.Functions.FailablePredicate;
|
||||||
* </pre>
|
* </pre>
|
||||||
* Using a {@link FailableStream}, this can be rewritten as follows:
|
* Using a {@link FailableStream}, this can be rewritten as follows:
|
||||||
* <pre>
|
* <pre>
|
||||||
* ObjectStreams.failable(stream).forEach((m) -> m.invoke(o, args));
|
* Streams.failable(stream).forEach((m) -> m.invoke(o, args));
|
||||||
* </pre>
|
* </pre>
|
||||||
* Obviously, the second version is much more concise and the spirit of
|
* Obviously, the second version is much more concise and the spirit of
|
||||||
* Lambda expressions is met better than in the first version.
|
* Lambda expressions is met better than in the first version.
|
||||||
|
@ -56,309 +57,309 @@ import org.apache.commons.lang3.Functions.FailablePredicate;
|
||||||
* @see Functions
|
* @see Functions
|
||||||
*/
|
*/
|
||||||
public class Streams {
|
public class Streams {
|
||||||
/** A reduced, and simplified version of a {@link Stream} with
|
/** A reduced, and simplified version of a {@link Stream} with
|
||||||
* failable method signatures.
|
* failable method signatures.
|
||||||
* @param <O> The streams element type.
|
* @param <O> The streams element type.
|
||||||
*/
|
*/
|
||||||
public static class FailableStream<O extends Object> {
|
public static class FailableStream<O extends Object> {
|
||||||
private Stream<O> stream;
|
private Stream<O> stream;
|
||||||
private boolean terminated;
|
private boolean terminated;
|
||||||
|
|
||||||
public FailableStream(Stream<O> pStream) {
|
public FailableStream(Stream<O> pStream) {
|
||||||
stream = pStream;
|
stream = pStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void assertNotTerminated() {
|
protected void assertNotTerminated() {
|
||||||
if (terminated) {
|
if (terminated) {
|
||||||
throw new IllegalStateException("This stream is already terminated.");
|
throw new IllegalStateException("This stream is already terminated.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void makeTerminated() {
|
protected void makeTerminated() {
|
||||||
assertNotTerminated();
|
assertNotTerminated();
|
||||||
terminated = true;
|
terminated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a FailableStream consisting of the elements of this stream that match
|
* Returns a FailableStream consisting of the elements of this stream that match
|
||||||
* the given FailablePredicate.
|
* the given FailablePredicate.
|
||||||
*
|
*
|
||||||
* <p>This is an intermediate operation.
|
* <p>This is an intermediate operation.
|
||||||
*
|
*
|
||||||
* @param pPredicate a non-interfering, stateless predicate to apply to each
|
* @param pPredicate a non-interfering, stateless predicate to apply to each
|
||||||
* element to determine if it should be included.
|
* element to determine if it should be included.
|
||||||
* @return the new stream
|
* @return the new stream
|
||||||
*/
|
*/
|
||||||
public FailableStream<O> filter(FailablePredicate<O,?> pPredicate){
|
public FailableStream<O> filter(FailablePredicate<O, ?> pPredicate){
|
||||||
assertNotTerminated();
|
assertNotTerminated();
|
||||||
stream = stream.filter(Functions.asPredicate(pPredicate));
|
stream = stream.filter(Functions.asPredicate(pPredicate));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs an action for each element of this stream.
|
* Performs an action for each element of this stream.
|
||||||
*
|
*
|
||||||
* <p>This is a terminal operation.
|
* <p>This is a terminal operation.
|
||||||
*
|
*
|
||||||
* <p>The behavior of this operation is explicitly nondeterministic.
|
* <p>The behavior of this operation is explicitly nondeterministic.
|
||||||
* For parallel stream pipelines, this operation does <em>not</em>
|
* For parallel stream pipelines, this operation does <em>not</em>
|
||||||
* guarantee to respect the encounter order of the stream, as doing so
|
* guarantee to respect the encounter order of the stream, as doing so
|
||||||
* would sacrifice the benefit of parallelism. For any given element, the
|
* would sacrifice the benefit of parallelism. For any given element, the
|
||||||
* action may be performed at whatever time and in whatever thread the
|
* action may be performed at whatever time and in whatever thread the
|
||||||
* library chooses. If the action accesses shared state, it is
|
* library chooses. If the action accesses shared state, it is
|
||||||
* responsible for providing the required synchronization.
|
* responsible for providing the required synchronization.
|
||||||
*
|
*
|
||||||
* @param pAction a non-interfering action to perform on the elements
|
* @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));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs a mutable reduction operation on the elements of this stream using a
|
|
||||||
* {@code Collector}. A {@code Collector}
|
|
||||||
* encapsulates the functions used as arguments to
|
|
||||||
* {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of
|
|
||||||
* collection strategies and composition of collect operations such as
|
|
||||||
* multiple-level grouping or partitioning.
|
|
||||||
*
|
|
||||||
* <p>If the underlying stream is parallel, and the {@code Collector}
|
|
||||||
* is concurrent, and either the stream is unordered or the collector is
|
|
||||||
* unordered, then a concurrent reduction will be performed
|
|
||||||
* (see {@link Collector} for details on concurrent reduction.)
|
|
||||||
*
|
|
||||||
* <p>This is a terminal operation.
|
|
||||||
*
|
|
||||||
* <p>When executed in parallel, multiple intermediate results may be
|
|
||||||
* instantiated, populated, and merged so as to maintain isolation of
|
|
||||||
* mutable data structures. Therefore, even when executed in parallel
|
|
||||||
* with non-thread-safe data structures (such as {@code ArrayList}), no
|
|
||||||
* additional synchronization is needed for a parallel reduction.
|
|
||||||
*
|
|
||||||
* \@apiNote
|
|
||||||
* The following will accumulate strings into an ArrayList:
|
|
||||||
* <pre>{@code
|
|
||||||
* List<String> asList = stringStream.collect(Collectors.toList());
|
|
||||||
* }</pre>
|
|
||||||
*
|
|
||||||
* <p>The following will classify {@code Person} objects by city:
|
|
||||||
* <pre>{@code
|
|
||||||
* Map<String, List<Person>> peopleByCity
|
|
||||||
* = personStream.collect(Collectors.groupingBy(Person::getCity));
|
|
||||||
* }</pre>
|
|
||||||
*
|
|
||||||
* <p>The following will classify {@code Person} objects by state and city,
|
|
||||||
* cascading two {@code Collector}s together:
|
|
||||||
* <pre>{@code
|
|
||||||
* Map<String, Map<String, List<Person>>> peopleByStateAndCity
|
|
||||||
* = personStream.collect(Collectors.groupingBy(Person::getState,
|
|
||||||
* Collectors.groupingBy(Person::getCity)));
|
|
||||||
* }</pre>
|
|
||||||
*
|
|
||||||
* @param <R> the type of the result
|
|
||||||
* @param <A> the intermediate accumulation type of the {@code Collector}
|
|
||||||
* @param pCollector the {@code Collector} describing the reduction
|
|
||||||
* @return the result of the reduction
|
|
||||||
* @see #collect(Supplier, BiConsumer, BiConsumer)
|
|
||||||
* @see Collectors
|
|
||||||
*/
|
|
||||||
public <A,R> R collect(Collector<? super O,A,R> pCollector) {
|
|
||||||
makeTerminated();
|
makeTerminated();
|
||||||
return stream().collect(pCollector);
|
stream().forEach(Functions.asConsumer(pAction));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a mutable reduction operation on the elements of this FailableStream.
|
* Performs a mutable reduction operation on the elements of this stream using a
|
||||||
* A mutable reduction is one in which the reduced value is a mutable result
|
* {@code Collector}. A {@code Collector}
|
||||||
* container, such as an {@code ArrayList}, and elements are incorporated by updating
|
* encapsulates the functions used as arguments to
|
||||||
* the state of the result rather than by replacing the result. This produces a result equivalent to:
|
* {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of
|
||||||
* <pre>{@code
|
* collection strategies and composition of collect operations such as
|
||||||
* R result = supplier.get();
|
* multiple-level grouping or partitioning.
|
||||||
* for (T element : this stream)
|
*
|
||||||
* accumulator.accept(result, element);
|
* <p>If the underlying stream is parallel, and the {@code Collector}
|
||||||
* return result;
|
* is concurrent, and either the stream is unordered or the collector is
|
||||||
* }</pre>
|
* unordered, then a concurrent reduction will be performed
|
||||||
*
|
* (see {@link Collector} for details on concurrent reduction.)
|
||||||
* <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
|
*
|
||||||
* can be parallelized without requiring additional synchronization.
|
* <p>This is a terminal operation.
|
||||||
*
|
*
|
||||||
* <p>This is a terminal operation.
|
* <p>When executed in parallel, multiple intermediate results may be
|
||||||
*
|
* instantiated, populated, and merged so as to maintain isolation of
|
||||||
* \@apiNote There are many existing classes in the JDK whose signatures are
|
* mutable data structures. Therefore, even when executed in parallel
|
||||||
* well-suited for use with method references as arguments to {@code collect()}.
|
* with non-thread-safe data structures (such as {@code ArrayList}), no
|
||||||
* For example, the following will accumulate strings into an {@code ArrayList}:
|
* additional synchronization is needed for a parallel reduction.
|
||||||
* <pre>{@code
|
*
|
||||||
* List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add,
|
* \@apiNote
|
||||||
* ArrayList::addAll);
|
* The following will accumulate strings into an ArrayList:
|
||||||
* }</pre>
|
* <pre>{@code
|
||||||
*
|
* List<String> asList = stringStream.collect(Collectors.toList());
|
||||||
* <p>The following will take a stream of strings and concatenates them into a
|
* }</pre>
|
||||||
* single string:
|
*
|
||||||
* <pre>{@code
|
* <p>The following will classify {@code Person} objects by city:
|
||||||
* String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
|
* <pre>{@code
|
||||||
* StringBuilder::append)
|
* Map<String, List<Person>> peopleByCity
|
||||||
* .toString();
|
* = personStream.collect(Collectors.groupingBy(Person::getCity));
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* @param <R> type of the result
|
* <p>The following will classify {@code Person} objects by state and city,
|
||||||
* @param <A> Type of the accumulator.
|
* cascading two {@code Collector}s together:
|
||||||
* @param pSupplier a function that creates a new result container. For a
|
* <pre>{@code
|
||||||
* parallel execution, this function may be called
|
* Map<String, Map<String, List<Person>>> peopleByStateAndCity
|
||||||
* multiple times and must return a fresh value each time.
|
* = personStream.collect(Collectors.groupingBy(Person::getState,
|
||||||
* @param pAccumulator An associative, non-interfering, stateless function for
|
* Collectors.groupingBy(Person::getCity)));
|
||||||
* incorporating an additional element into a result
|
* }</pre>
|
||||||
* @param pCombiner An associative, non-interfering, stateless
|
*
|
||||||
* function for combining two values, which must be compatible with the
|
* @param <R> the type of the result
|
||||||
* accumulator function
|
* @param <A> the intermediate accumulation type of the {@code Collector}
|
||||||
* @return The result of the reduction
|
* @param pCollector the {@code Collector} describing the reduction
|
||||||
*/
|
* @return the result of the reduction
|
||||||
public <A,R> R collect(Supplier<R> pSupplier, BiConsumer<R,? super O> pAccumulator, BiConsumer<R,R> pCombiner) {
|
* @see #collect(Supplier, BiConsumer, BiConsumer)
|
||||||
|
* @see Collectors
|
||||||
|
*/
|
||||||
|
public <A, R> R collect(Collector<? super O, A, R> pCollector) {
|
||||||
makeTerminated();
|
makeTerminated();
|
||||||
return stream().collect(pSupplier, pAccumulator, pCombiner);
|
return stream().collect(pCollector);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a reduction on the elements of this stream, using the provided
|
* Performs a mutable reduction operation on the elements of this FailableStream.
|
||||||
* identity value and an associative accumulation function, and returns
|
* A mutable reduction is one in which the reduced value is a mutable result
|
||||||
* the reduced value. This is equivalent to:
|
* container, such as an {@code ArrayList}, and elements are incorporated by updating
|
||||||
* <pre>{@code
|
* the state of the result rather than by replacing the result. This produces a result equivalent to:
|
||||||
* T result = identity;
|
* <pre>{@code
|
||||||
* for (T element : this stream)
|
* R result = supplier.get();
|
||||||
* result = accumulator.apply(result, element)
|
* for (T element : this stream)
|
||||||
* return result;
|
* accumulator.accept(result, element);
|
||||||
* }</pre>
|
* return result;
|
||||||
*
|
* }</pre>
|
||||||
* but is not constrained to execute sequentially.
|
*
|
||||||
*
|
* <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
|
||||||
* <p>The {@code identity} value must be an identity for the accumulator
|
* can be parallelized without requiring additional synchronization.
|
||||||
* function. This means that for all {@code t},
|
*
|
||||||
* {@code accumulator.apply(identity, t)} is equal to {@code t}.
|
* <p>This is a terminal operation.
|
||||||
* The {@code accumulator} function must be an associative function.
|
*
|
||||||
*
|
* \@apiNote There are many existing classes in the JDK whose signatures are
|
||||||
* <p>This is a terminal operation.
|
* well-suited for use with method references as arguments to {@code collect()}.
|
||||||
*
|
* For example, the following will accumulate strings into an {@code ArrayList}:
|
||||||
* \@apiNote Sum, min, max, average, and string concatenation are all special
|
* <pre>{@code
|
||||||
* cases of reduction. Summing a stream of numbers can be expressed as:
|
* List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add,
|
||||||
*
|
* ArrayList::addAll);
|
||||||
* <pre>{@code
|
* }</pre>
|
||||||
* Integer sum = integers.reduce(0, (a, b) -> a+b);
|
*
|
||||||
* }</pre>
|
* <p>The following will take a stream of strings and concatenates them into a
|
||||||
*
|
* single string:
|
||||||
* or:
|
* <pre>{@code
|
||||||
*
|
* String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
|
||||||
* <pre>{@code
|
* StringBuilder::append)
|
||||||
* Integer sum = integers.reduce(0, Integer::sum);
|
* .toString();
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* <p>While this may seem a more roundabout way to perform an aggregation
|
* @param <R> type of the result
|
||||||
* compared to simply mutating a running total in a loop, reduction
|
* @param <A> Type of the accumulator.
|
||||||
* operations parallelize more gracefully, without needing additional
|
* @param pSupplier a function that creates a new result container. For a
|
||||||
* synchronization and with greatly reduced risk of data races.
|
* parallel execution, this function may be called
|
||||||
*
|
* multiple times and must return a fresh value each time.
|
||||||
* @param pIdentity the identity value for the accumulating function
|
* @param pAccumulator An associative, non-interfering, stateless function for
|
||||||
* @param pAccumulator an associative, non-interfering, stateless
|
* incorporating an additional element into a result
|
||||||
* function for combining two values
|
* @param pCombiner An associative, non-interfering, stateless
|
||||||
* @return the result of the reduction
|
* function for combining two values, which must be compatible with the
|
||||||
*/
|
* accumulator function
|
||||||
public O reduce(O pIdentity, BinaryOperator<O> pAccumulator) {
|
* @return The result of the reduction
|
||||||
|
*/
|
||||||
|
public <A, R> R collect(Supplier<R> pSupplier, BiConsumer<R, ? super O> pAccumulator, BiConsumer<R, R> pCombiner) {
|
||||||
makeTerminated();
|
makeTerminated();
|
||||||
return stream().reduce(pIdentity, pAccumulator);
|
return stream().collect(pSupplier, pAccumulator, pCombiner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a stream consisting of the results of applying the given
|
* Performs a reduction on the elements of this stream, using the provided
|
||||||
* function to the elements of this stream.
|
* identity value and an associative accumulation function, and returns
|
||||||
*
|
* the reduced value. This is equivalent to:
|
||||||
* <p>This is an intermediate operation.
|
* <pre>{@code
|
||||||
*
|
* T result = identity;
|
||||||
* @param <R> The element type of the new stream
|
* for (T element : this stream)
|
||||||
* @param pMapper A non-interfering, stateless function to apply to each element
|
* result = accumulator.apply(result, element)
|
||||||
* @return the new stream
|
* return result;
|
||||||
*/
|
* }</pre>
|
||||||
public <R> FailableStream<R> map(FailableFunction<O,R,?> pMapper) {
|
*
|
||||||
assertNotTerminated();
|
* but is not constrained to execute sequentially.
|
||||||
return new FailableStream<R>(stream.map(Functions.asFunction(pMapper)));
|
*
|
||||||
}
|
* <p>The {@code identity} value must be an identity for the accumulator
|
||||||
|
* function. This means that for all {@code t},
|
||||||
|
* {@code accumulator.apply(identity, t)} is equal to {@code t}.
|
||||||
|
* The {@code accumulator} function must be an associative function.
|
||||||
|
*
|
||||||
|
* <p>This is a terminal operation.
|
||||||
|
*
|
||||||
|
* \@apiNote Sum, min, max, average, and string concatenation are all special
|
||||||
|
* cases of reduction. Summing a stream of numbers can be expressed as:
|
||||||
|
*
|
||||||
|
* <pre>{@code
|
||||||
|
* Integer sum = integers.reduce(0, (a, b) -> a+b);
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* or:
|
||||||
|
*
|
||||||
|
* <pre>{@code
|
||||||
|
* Integer sum = integers.reduce(0, Integer::sum);
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* <p>While this may seem a more roundabout way to perform an aggregation
|
||||||
|
* compared to simply mutating a running total in a loop, reduction
|
||||||
|
* operations parallelize more gracefully, without needing additional
|
||||||
|
* synchronization and with greatly reduced risk of data races.
|
||||||
|
*
|
||||||
|
* @param pIdentity the identity value for the accumulating function
|
||||||
|
* @param pAccumulator an associative, non-interfering, stateless
|
||||||
|
* function for combining two values
|
||||||
|
* @return the result of the reduction
|
||||||
|
*/
|
||||||
|
public O reduce(O pIdentity, BinaryOperator<O> pAccumulator) {
|
||||||
|
makeTerminated();
|
||||||
|
return stream().reduce(pIdentity, pAccumulator);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the FailableStream into an equivalent stream.
|
* Returns a stream consisting of the results of applying the given
|
||||||
* @return A stream, which will return the same elements, which this FailableStream would return.
|
* function to the elements of this stream.
|
||||||
*/
|
*
|
||||||
public Stream<O> stream() {
|
* <p>This is an intermediate operation.
|
||||||
return stream;
|
*
|
||||||
}
|
* @param <R> The element type of the new stream
|
||||||
|
* @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) {
|
||||||
|
assertNotTerminated();
|
||||||
|
return new FailableStream<R>(stream.map(Functions.asFunction(pMapper)));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether all elements of this stream match the provided predicate.
|
* Converts the FailableStream into an equivalent stream.
|
||||||
* May not evaluate the predicate on all elements if not necessary for
|
* @return A stream, which will return the same elements, which this FailableStream would return.
|
||||||
* determining the result. If the stream is empty then {@code true} is
|
*/
|
||||||
* returned and the predicate is not evaluated.
|
public Stream<O> stream() {
|
||||||
*
|
return stream;
|
||||||
* <p>This is a short-circuiting terminal operation.
|
}
|
||||||
*
|
|
||||||
* \@apiNote
|
|
||||||
* This method evaluates the <em>universal quantification</em> of the
|
|
||||||
* predicate over the elements of the stream (for all x P(x)). If the
|
|
||||||
* stream is empty, the quantification is said to be <em>vacuously
|
|
||||||
* satisfied</em> and is always {@code true} (regardless of P(x)).
|
|
||||||
*
|
|
||||||
* @param pPredicate A non-interfering, stateless predicate to apply to
|
|
||||||
* elements of this stream
|
|
||||||
* @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) {
|
|
||||||
assertNotTerminated();
|
|
||||||
return stream().allMatch(Functions.asPredicate(pPredicate));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether any elements of this stream match the provided
|
* Returns whether all elements of this stream match the provided predicate.
|
||||||
* predicate. May not evaluate the predicate on all elements if not
|
* May not evaluate the predicate on all elements if not necessary for
|
||||||
* necessary for determining the result. If the stream is empty then
|
* determining the result. If the stream is empty then {@code true} is
|
||||||
* {@code false} is returned and the predicate is not evaluated.
|
* returned and the predicate is not evaluated.
|
||||||
*
|
*
|
||||||
* <p>This is a short-circuiting terminal operation.
|
* <p>This is a short-circuiting terminal operation.
|
||||||
*
|
*
|
||||||
* \@apiNote
|
* \@apiNote
|
||||||
* This method evaluates the <em>existential quantification</em> of the
|
* This method evaluates the <em>universal quantification</em> of the
|
||||||
* predicate over the elements of the stream (for some x P(x)).
|
* predicate over the elements of the stream (for all x P(x)). If the
|
||||||
*
|
* stream is empty, the quantification is said to be <em>vacuously
|
||||||
* @param pPredicate A non-interfering, stateless predicate to apply to
|
* satisfied</em> and is always {@code true} (regardless of P(x)).
|
||||||
* elements of this stream
|
*
|
||||||
* @return {@code true} if any elements of the stream match the provided
|
* @param pPredicate A non-interfering, stateless predicate to apply to
|
||||||
* predicate, otherwise {@code false}
|
* elements of this stream
|
||||||
*/
|
* @return {@code true} If either all elements of the stream match the
|
||||||
public boolean anyMatch(FailablePredicate<O,?> pPredicate) {
|
* provided predicate or the stream is empty, otherwise {@code false}.
|
||||||
|
*/
|
||||||
|
public boolean allMatch(FailablePredicate<O, ?> pPredicate) {
|
||||||
|
assertNotTerminated();
|
||||||
|
return stream().allMatch(Functions.asPredicate(pPredicate));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether any elements of this stream match the provided
|
||||||
|
* predicate. May not evaluate the predicate on all elements if not
|
||||||
|
* necessary for determining the result. If the stream is empty then
|
||||||
|
* {@code false} is returned and the predicate is not evaluated.
|
||||||
|
*
|
||||||
|
* <p>This is a short-circuiting terminal operation.
|
||||||
|
*
|
||||||
|
* \@apiNote
|
||||||
|
* This method evaluates the <em>existential quantification</em> of the
|
||||||
|
* predicate over the elements of the stream (for some x P(x)).
|
||||||
|
*
|
||||||
|
* @param pPredicate A non-interfering, stateless predicate to apply to
|
||||||
|
* elements of this stream
|
||||||
|
* @return {@code true} if any elements of the stream match the provided
|
||||||
|
* predicate, otherwise {@code false}
|
||||||
|
*/
|
||||||
|
public boolean anyMatch(FailablePredicate<O, ?> pPredicate) {
|
||||||
assertNotTerminated();
|
assertNotTerminated();
|
||||||
return stream().anyMatch(Functions.asPredicate(pPredicate));
|
return stream().anyMatch(Functions.asPredicate(pPredicate));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the given {@link Stream stream} into a {@link FailableStream}.
|
* Converts the given {@link Stream stream} into a {@link FailableStream}.
|
||||||
* This is basically a simplified, reduced version of the {@link Stream}
|
* This is basically a simplified, reduced version of the {@link Stream}
|
||||||
* class, with the same underlying element stream, except that failable
|
* class, with the same underlying element stream, except that failable
|
||||||
* objects, like {@link FailablePredicate}, {@link FailableFunction}, or
|
* objects, like {@link FailablePredicate}, {@link FailableFunction}, or
|
||||||
* {@link FailableConsumer} may be applied, instead of
|
* {@link FailableConsumer} may be applied, instead of
|
||||||
* {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is
|
* {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is
|
||||||
* to rewrite a code snippet like this:
|
* to rewrite a code snippet like this:
|
||||||
* <pre>
|
* <pre>
|
||||||
* final List<O> list;
|
* final List<O> list;
|
||||||
* final Method m;
|
* final Method m;
|
||||||
* final Function<O,String> mapper = (o) -> {
|
* final Function<O,String> mapper = (o) -> {
|
||||||
* try {
|
* try {
|
||||||
* return (String) m.invoke(o);
|
* return (String) m.invoke(o);
|
||||||
* } catch (Throwable t) {
|
* } catch (Throwable t) {
|
||||||
* throw Functions.rethrow(t);
|
* throw Functions.rethrow(t);
|
||||||
* }
|
* }
|
||||||
* };
|
* };
|
||||||
* final List<String> strList = list.stream()
|
* final List<String> strList = list.stream()
|
||||||
* .map(mapper).collect(Collectors.toList());
|
* .map(mapper).collect(Collectors.toList());
|
||||||
* </pre>
|
* </pre>
|
||||||
* as follows:
|
* as follows:
|
||||||
* <pre>
|
* <pre>
|
||||||
* final List<O> list;
|
* final List<O> list;
|
||||||
* final Method m;
|
* final Method m;
|
||||||
* final List<String> strList = Functions.stream(list.stream())
|
* final List<String> strList = Functions.stream(list.stream())
|
||||||
|
@ -369,12 +370,54 @@ public class Streams {
|
||||||
* intermediate objects, of type FailableStream), it is much more
|
* intermediate objects, of type FailableStream), it is much more
|
||||||
* concise, and readable, and meets the spirit of Lambdas better
|
* concise, and readable, and meets the spirit of Lambdas better
|
||||||
* than the first version.
|
* than the first version.
|
||||||
* @param <O> The streams element type.
|
* @param <O> The streams element type.
|
||||||
* @param pStream The stream, which is being converted.
|
* @param pStream The stream, which is being converted.
|
||||||
* @return The {@link FailableStream}, which has been created by
|
* @return The {@link FailableStream}, which has been created by
|
||||||
* converting the stream.
|
* converting the stream.
|
||||||
*/
|
*/
|
||||||
public static <O> FailableStream<O> stream(Stream<O> pStream) {
|
public static <O> FailableStream<O> stream(Stream<O> pStream) {
|
||||||
return new FailableStream<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;
|
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.lang.reflect.UndeclaredThrowableException;
|
||||||
import java.util.ArrayList;
|
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) -> {
|
return (s) -> {
|
||||||
final Integer i = Integer.valueOf(s);
|
final Integer i = Integer.valueOf(s);
|
||||||
if (i.intValue() == 4) {
|
if (i.intValue() == 4) {
|
||||||
|
@ -105,7 +107,9 @@ class StreamsTest {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = Functions.stream(input)
|
final List<Integer> output = Functions.stream(input)
|
||||||
.map((s) -> Integer.valueOf(s))
|
.map((s) -> Integer.valueOf(s))
|
||||||
.filter((i) -> { return i.intValue() %2 == 0;})
|
.filter((i) -> {
|
||||||
|
return i.intValue() %2 == 0;
|
||||||
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertEvenNumbers(output);
|
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) -> {
|
return (i) -> {
|
||||||
if (i.intValue() == 5) {
|
if (i.intValue() == 5) {
|
||||||
if (pThrowable != null) {
|
if (pThrowable != null) {
|
||||||
|
|
Loading…
Reference in New Issue