Close Javadoc tags.

This commit is contained in:
Gary Gregory 2021-07-27 12:57:32 -04:00
parent 66455cbaf8
commit 7c65852709
2 changed files with 77 additions and 19 deletions

View File

@ -101,7 +101,9 @@ public class Streams {
* 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.
* </p>
* *
* @param predicate a non-interfering, stateless predicate to apply to each * @param predicate 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.
@ -116,15 +118,19 @@ public class Streams {
/** /**
* 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 an intermediate operation.
* </p>
* *
* <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.
* </p>
* *
* @param action a non-interfering action to perform on the elements * @param action a non-interfering action to perform on the elements
*/ */
@ -141,33 +147,44 @@ public class Streams {
* collection strategies and composition of collect operations such as * collection strategies and composition of collect operations such as
* multiple-level grouping or partitioning. * multiple-level grouping or partitioning.
* *
* <p>If the underlying stream is parallel, and the {@code Collector} * <p>
* If the underlying stream is parallel, and the {@code Collector}
* is concurrent, and either the stream is unordered or the collector is * is concurrent, and either the stream is unordered or the collector is
* unordered, then a concurrent reduction will be performed * unordered, then a concurrent reduction will be performed
* (see {@link Collector} for details on concurrent reduction.) * (see {@link Collector} for details on concurrent reduction.)
* </p>
* *
* <p>This is a terminal operation. * <p>
* This is an intermediate operation.
* </p>
* *
* <p>When executed in parallel, multiple intermediate results may be * <p>
* When executed in parallel, multiple intermediate results may be
* instantiated, populated, and merged so as to maintain isolation of * instantiated, populated, and merged so as to maintain isolation of
* mutable data structures. Therefore, even when executed in parallel * mutable data structures. Therefore, even when executed in parallel
* with non-thread-safe data structures (such as {@code ArrayList}), no * with non-thread-safe data structures (such as {@code ArrayList}), no
* additional synchronization is needed for a parallel reduction. * additional synchronization is needed for a parallel reduction.
* * </p>
* <p>
* Note * Note
* The following will accumulate strings into an ArrayList: * The following will accumulate strings into an ArrayList:
* </p>
* <pre>{@code * <pre>{@code
* List<String> asList = stringStream.collect(Collectors.toList()); * List<String> asList = stringStream.collect(Collectors.toList());
* }</pre> * }</pre>
* *
* <p>The following will classify {@code Person} objects by city: * <p>
* The following will classify {@code Person} objects by city:
* </p>
* <pre>{@code * <pre>{@code
* Map<String, List<Person>> peopleByCity * Map<String, List<Person>> peopleByCity
* = personStream.collect(Collectors.groupingBy(Person::getCity)); * = personStream.collect(Collectors.groupingBy(Person::getCity));
* }</pre> * }</pre>
* *
* <p>The following will classify {@code Person} objects by state and city, * <p>
* The following will classify {@code Person} objects by state and city,
* cascading two {@code Collector}s together: * cascading two {@code Collector}s together:
* </p>
* <pre>{@code * <pre>{@code
* Map<String, Map<String, List<Person>>> peopleByStateAndCity * Map<String, Map<String, List<Person>>> peopleByStateAndCity
* = personStream.collect(Collectors.groupingBy(Person::getState, * = personStream.collect(Collectors.groupingBy(Person::getState,
@ -198,21 +215,29 @@ public class Streams {
* return result; * return result;
* }</pre> * }</pre>
* *
* <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations * <p>
* Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
* can be parallelized without requiring additional synchronization. * can be parallelized without requiring additional synchronization.
* </p>
* *
* <p>This is a terminal operation. * <p>
* This is an intermediate operation.
* </p>
* *
* <p>
* Note There are many existing classes in the JDK whose signatures are * Note There are many existing classes in the JDK whose signatures are
* well-suited for use with method references as arguments to {@code collect()}. * well-suited for use with method references as arguments to {@code collect()}.
* For example, the following will accumulate strings into an {@code ArrayList}: * For example, the following will accumulate strings into an {@code ArrayList}:
* </p>
* <pre>{@code * <pre>{@code
* List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, * List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add,
* ArrayList::addAll); * ArrayList::addAll);
* }</pre> * }</pre>
* *
* <p>The following will take a stream of strings and concatenates them into a * <p>
* The following will take a stream of strings and concatenates them into a
* single string: * single string:
* </p>
* <pre>{@code * <pre>{@code
* String concat = stringStream.collect(StringBuilder::new, StringBuilder::append, * String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
* StringBuilder::append) * StringBuilder::append)
@ -249,12 +274,16 @@ public class Streams {
* *
* but is not constrained to execute sequentially. * but is not constrained to execute sequentially.
* *
* <p>The {@code identity} value must be an identity for the accumulator * <p>
* The {@code identity} value must be an identity for the accumulator
* function. This means that for all {@code t}, * function. This means that for all {@code t},
* {@code accumulator.apply(identity, t)} is equal to {@code t}. * {@code accumulator.apply(identity, t)} is equal to {@code t}.
* The {@code accumulator} function must be an associative function. * The {@code accumulator} function must be an associative function.
* </p>
* *
* <p>This is a terminal operation. * <p>
* This is an intermediate operation.
* </p>
* *
* Note Sum, min, max, average, and string concatenation are all special * Note Sum, min, max, average, and string concatenation are all special
* cases of reduction. Summing a stream of numbers can be expressed as: * cases of reduction. Summing a stream of numbers can be expressed as:
@ -269,10 +298,12 @@ public class Streams {
* Integer sum = integers.reduce(0, Integer::sum); * Integer sum = integers.reduce(0, Integer::sum);
* }</pre> * }</pre>
* *
* <p>While this may seem a more roundabout way to perform an aggregation * <p>
* While this may seem a more roundabout way to perform an aggregation
* compared to simply mutating a running total in a loop, reduction * compared to simply mutating a running total in a loop, reduction
* operations parallelize more gracefully, without needing additional * operations parallelize more gracefully, without needing additional
* synchronization and with greatly reduced risk of data races. * synchronization and with greatly reduced risk of data races.
* </p>
* *
* @param identity the identity value for the accumulating function * @param identity the identity value for the accumulating function
* @param accumulator an associative, non-interfering, stateless * @param accumulator an associative, non-interfering, stateless
@ -288,7 +319,9 @@ public class Streams {
* Returns a stream consisting of the results of applying the given * Returns a stream consisting of the results of applying the given
* function to the elements of this stream. * function to the elements of this stream.
* *
* <p>This is an intermediate operation. * <p>
* This is an intermediate operation.
* </p>
* *
* @param <R> The element type of the new stream * @param <R> The element type of the new stream
* @param mapper A non-interfering, stateless function to apply to each element * @param mapper A non-interfering, stateless function to apply to each element
@ -313,13 +346,17 @@ public class Streams {
* determining the result. If the stream is empty then {@code true} is * determining the result. If the stream is empty then {@code true} 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.
* </p>
* *
* <p>
* Note * Note
* This method evaluates the <em>universal quantification</em> of the * This method evaluates the <em>universal quantification</em> of the
* predicate over the elements of the stream (for all x P(x)). If 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 * stream is empty, the quantification is said to be <em>vacuously
* satisfied</em> and is always {@code true} (regardless of P(x)). * satisfied</em> and is always {@code true} (regardless of P(x)).
* </p>
* *
* @param predicate A non-interfering, stateless predicate to apply to * @param predicate A non-interfering, stateless predicate to apply to
* elements of this stream * elements of this stream
@ -337,7 +374,9 @@ public class Streams {
* necessary for determining the result. If the stream is empty then * necessary for determining the result. If the stream is empty then
* {@code false} is returned and the predicate is not evaluated. * {@code false} is returned and the predicate is not evaluated.
* *
* <p>This is a short-circuiting terminal operation. * <p>
* This is a short-circuiting terminal operation.
* </p>
* *
* Note * Note
* This method evaluates the <em>existential quantification</em> of the * This method evaluates the <em>existential quantification</em> of the

View File

@ -43,7 +43,7 @@ import org.apache.commons.lang3.function.FailablePredicate;
* {@code java.util.stream} package, or more generally, with Java 8 lambdas. More * {@code java.util.stream} package, or more generally, with Java 8 lambdas. More
* specifically, it attempts to address the fact that lambdas are supposed * specifically, it attempts to address the fact that lambdas are supposed
* not to throw Exceptions, at least not checked Exceptions, AKA instances * not to throw Exceptions, at least not checked Exceptions, AKA instances
* of {@link Exception}. This enforces the use of constructs like * of {@link Exception}. This enforces the use of constructs like:
* <pre> * <pre>
* Consumer&lt;java.lang.reflect.Method&gt; consumer = m -&gt; { * Consumer&lt;java.lang.reflect.Method&gt; consumer = m -&gt; {
* try { * try {
@ -54,7 +54,9 @@ import org.apache.commons.lang3.function.FailablePredicate;
* }; * };
* stream.forEach(consumer); * stream.forEach(consumer);
* </pre> * </pre>
* <p>
* Using a {@link FailableStream}, this can be rewritten as follows: * Using a {@link FailableStream}, this can be rewritten as follows:
* </p>
* <pre> * <pre>
* Streams.failable(stream).forEach((m) -&gt; m.invoke(o, args)); * Streams.failable(stream).forEach((m) -&gt; m.invoke(o, args));
* </pre> * </pre>
@ -140,6 +142,7 @@ public class Streams {
* *
* <p> * <p>
* This is a short-circuiting terminal operation. * This is a short-circuiting terminal operation.
* </p>
* *
* Note This method evaluates the <em>universal quantification</em> of the predicate over the elements of * Note 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 * the stream (for all x P(x)). If the stream is empty, the quantification is said to be <em>vacuously
@ -161,6 +164,7 @@ public class Streams {
* *
* <p> * <p>
* This is a short-circuiting terminal operation. * This is a short-circuiting terminal operation.
* </p>
* *
* Note This method evaluates the <em>existential quantification</em> of the predicate over the elements of * Note This method evaluates the <em>existential quantification</em> of the predicate over the elements of
* the stream (for some x P(x)). * the stream (for some x P(x)).
@ -194,15 +198,18 @@ public class Streams {
* If the underlying stream is parallel, and the {@code Collector} is concurrent, and either the stream is * 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} * unordered or the collector is unordered, then a concurrent reduction will be performed (see {@link Collector}
* for details on concurrent reduction.) * for details on concurrent reduction.)
* </p>
* *
* <p> * <p>
* This is a terminal operation. * This is a terminal operation.
* </p>
* *
* <p> * <p>
* When executed in parallel, multiple intermediate results may be instantiated, populated, and merged so as to * 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 * 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 * data structures (such as {@code ArrayList}), no additional synchronization is needed for a parallel
* reduction. * reduction.
* </p>
* *
* Note The following will accumulate strings into an ArrayList: * Note The following will accumulate strings into an ArrayList:
* *
@ -214,6 +221,7 @@ public class Streams {
* *
* <p> * <p>
* The following will classify {@code Person} objects by city: * The following will classify {@code Person} objects by city:
* </p>
* *
* <pre> * <pre>
* {@code * {@code
@ -224,6 +232,7 @@ public class Streams {
* <p> * <p>
* The following will classify {@code Person} objects by state and city, cascading two {@code Collector}s * The following will classify {@code Person} objects by state and city, cascading two {@code Collector}s
* together: * together:
* </p>
* *
* <pre> * <pre>
* {@code * {@code
@ -262,9 +271,11 @@ public class Streams {
* <p> * <p>
* Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations can be parallelized without * Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations can be parallelized without
* requiring additional synchronization. * requiring additional synchronization.
* </p>
* *
* <p> * <p>
* This is a terminal operation. * This is a terminal operation.
* </p>
* *
* Note There are many existing classes in the JDK whose signatures are well-suited for use with method * Note There are many existing classes in the JDK whose signatures are well-suited for use with method
* references as arguments to {@code collect()}. For example, the following will accumulate strings into an * references as arguments to {@code collect()}. For example, the following will accumulate strings into an
@ -278,6 +289,7 @@ public class Streams {
* *
* <p> * <p>
* The following will take a stream of strings and concatenates them into a single string: * The following will take a stream of strings and concatenates them into a single string:
* </p>
* *
* <pre> * <pre>
* {@code * {@code
@ -307,6 +319,7 @@ public class Streams {
* *
* <p> * <p>
* This is an intermediate operation. * This is an intermediate operation.
* </p>
* *
* @param predicate a non-interfering, stateless predicate to apply to each element to determine if it should be * @param predicate a non-interfering, stateless predicate to apply to each element to determine if it should be
* included. * included.
@ -323,6 +336,7 @@ public class Streams {
* *
* <p> * <p>
* This is a terminal operation. * This is a terminal operation.
* </p>
* *
* <p> * <p>
* The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation * The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation
@ -330,6 +344,7 @@ public class Streams {
* benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever * benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever
* thread the library chooses. If the action accesses shared state, it is responsible for providing the required * thread the library chooses. If the action accesses shared state, it is responsible for providing the required
* synchronization. * synchronization.
* </p>
* *
* @param action a non-interfering action to perform on the elements * @param action a non-interfering action to perform on the elements
*/ */
@ -353,6 +368,7 @@ public class Streams {
* *
* <p> * <p>
* This is an intermediate operation. * This is an intermediate operation.
* </p>
* *
* @param <R> The element type of the new stream * @param <R> The element type of the new stream
* @param mapper A non-interfering, stateless function to apply to each element * @param mapper A non-interfering, stateless function to apply to each element
@ -382,9 +398,11 @@ public class Streams {
* The {@code identity} value must be an identity for the accumulator function. This means that for all * 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 * {@code t}, {@code accumulator.apply(identity, t)} is equal to {@code t}. The {@code accumulator} function
* must be an associative function. * must be an associative function.
* </p>
* *
* <p> * <p>
* This is a terminal operation. * This is a terminal operation.
* </p>
* *
* Note Sum, min, max, average, and string concatenation are all special cases of reduction. Summing a * Note Sum, min, max, average, and string concatenation are all special cases of reduction. Summing a
* stream of numbers can be expressed as: * stream of numbers can be expressed as:
@ -407,6 +425,7 @@ public class Streams {
* While this may seem a more roundabout way to perform an aggregation compared to simply mutating a running * 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 * total in a loop, reduction operations parallelize more gracefully, without needing additional synchronization
* and with greatly reduced risk of data races. * and with greatly reduced risk of data races.
* </p>
* *
* @param identity the identity value for the accumulating function * @param identity the identity value for the accumulating function
* @param accumulator an associative, non-interfering, stateless function for combining two values * @param accumulator an associative, non-interfering, stateless function for combining two values