001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3;
018
019import java.lang.reflect.Array;
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.Collections;
023import java.util.List;
024import java.util.Set;
025import java.util.function.BiConsumer;
026import java.util.function.BinaryOperator;
027import java.util.function.Consumer;
028import java.util.function.Function;
029import java.util.function.Predicate;
030import java.util.function.Supplier;
031import java.util.stream.Collector;
032import java.util.stream.Collectors;
033import java.util.stream.Stream;
034
035import org.apache.commons.lang3.Functions.FailableConsumer;
036import org.apache.commons.lang3.Functions.FailableFunction;
037import org.apache.commons.lang3.Functions.FailablePredicate;
038
039/**
040 * Provides utility functions, and classes for working with the
041 * {@code java.util.stream} package, or more generally, with Java 8 lambdas. More
042 * specifically, it attempts to address the fact that lambdas are supposed
043 * not to throw Exceptions, at least not checked Exceptions, AKA instances
044 * of {@link Exception}. This enforces the use of constructs like
045 * <pre>
046 *     Consumer&lt;java.lang.reflect.Method&gt; consumer = (m) -&gt; {
047 *         try {
048 *             m.invoke(o, args);
049 *         } catch (Throwable t) {
050 *             throw Functions.rethrow(t);
051 *         }
052 *    };
053 *    stream.forEach(consumer);
054 * </pre>
055 * Using a {@link FailableStream}, this can be rewritten as follows:
056 * <pre>
057 *     Streams.failable(stream).forEach((m) -&gt; m.invoke(o, args));
058 * </pre>
059 * Obviously, the second version is much more concise and the spirit of
060 * Lambda expressions is met better than in the first version.
061 *
062 * @see Stream
063 * @see Functions
064 * @since 3.10
065 */
066public class Streams {
067
068   /**
069    * A reduced, and simplified version of a {@link Stream} with
070    * failable method signatures.
071    * @param <O> The streams element type.
072    */
073    public static class FailableStream<O extends Object> {
074
075        private Stream<O> stream;
076        private boolean terminated;
077
078        /**
079         * Constructs a new instance with the given {@code stream}.
080         * @param stream The stream.
081         */
082        public FailableStream(final Stream<O> stream) {
083            this.stream = stream;
084        }
085
086        protected void assertNotTerminated() {
087            if (terminated) {
088                throw new IllegalStateException("This stream is already terminated.");
089            }
090        }
091
092        protected void makeTerminated() {
093            assertNotTerminated();
094            terminated = true;
095        }
096
097        /**
098         * Returns a FailableStream consisting of the elements of this stream that match
099         * the given FailablePredicate.
100         *
101         * <p>This is an intermediate operation.
102         *
103         * @param predicate a non-interfering, stateless predicate to apply to each
104         * element to determine if it should be included.
105         * @return the new stream
106         */
107        public FailableStream<O> filter(final FailablePredicate<O, ?> predicate){
108            assertNotTerminated();
109            stream = stream.filter(Functions.asPredicate(predicate));
110            return this;
111        }
112
113        /**
114         * Performs an action for each element of this stream.
115         *
116         * <p>This is a terminal operation.
117         *
118         * <p>The behavior of this operation is explicitly nondeterministic.
119         * For parallel stream pipelines, this operation does <em>not</em>
120         * guarantee to respect the encounter order of the stream, as doing so
121         * would sacrifice the benefit of parallelism.  For any given element, the
122         * action may be performed at whatever time and in whatever thread the
123         * library chooses.  If the action accesses shared state, it is
124         * responsible for providing the required synchronization.
125         *
126         * @param action a non-interfering action to perform on the elements
127         */
128        public void forEach(final FailableConsumer<O, ?> action) {
129            makeTerminated();
130            stream().forEach(Functions.asConsumer(action));
131        }
132
133        /**
134         * Performs a mutable reduction operation on the elements of this stream using a
135         * {@code Collector}.  A {@code Collector}
136         * encapsulates the functions used as arguments to
137         * {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for reuse of
138         * collection strategies and composition of collect operations such as
139         * multiple-level grouping or partitioning.
140         *
141         * <p>If the underlying stream is parallel, and the {@code Collector}
142         * is concurrent, and either the stream is unordered or the collector is
143         * unordered, then a concurrent reduction will be performed
144         * (see {@link Collector} for details on concurrent reduction.)
145         *
146         * <p>This is a terminal operation.
147         *
148         * <p>When executed in parallel, multiple intermediate results may be
149         * instantiated, populated, and merged so as to maintain isolation of
150         * mutable data structures.  Therefore, even when executed in parallel
151         * with non-thread-safe data structures (such as {@code ArrayList}), no
152         * additional synchronization is needed for a parallel reduction.
153         *
154         * \@apiNote
155         * The following will accumulate strings into an ArrayList:
156         * <pre>{@code
157         *     List<String> asList = stringStream.collect(Collectors.toList());
158         * }</pre>
159         *
160         * <p>The following will classify {@code Person} objects by city:
161         * <pre>{@code
162         *     Map<String, List<Person>> peopleByCity
163         *         = personStream.collect(Collectors.groupingBy(Person::getCity));
164         * }</pre>
165         *
166         * <p>The following will classify {@code Person} objects by state and city,
167         * cascading two {@code Collector}s together:
168         * <pre>{@code
169         *     Map<String, Map<String, List<Person>>> peopleByStateAndCity
170         *         = personStream.collect(Collectors.groupingBy(Person::getState,
171         *                                                      Collectors.groupingBy(Person::getCity)));
172         * }</pre>
173         *
174         * @param <R> the type of the result
175         * @param <A> the intermediate accumulation type of the {@code Collector}
176         * @param collector the {@code Collector} describing the reduction
177         * @return the result of the reduction
178         * @see #collect(Supplier, BiConsumer, BiConsumer)
179         * @see Collectors
180         */
181        public <A, R> R collect(final Collector<? super O, A, R> collector) {
182            makeTerminated();
183            return stream().collect(collector);
184        }
185
186        /**
187         * Performs a mutable reduction operation on the elements of this FailableStream.
188         * A mutable reduction is one in which the reduced value is a mutable result
189         * container, such as an {@code ArrayList}, and elements are incorporated by updating
190         * the state of the result rather than by replacing the result. This produces a result equivalent to:
191         * <pre>{@code
192         *     R result = supplier.get();
193         *     for (T element : this stream)
194         *         accumulator.accept(result, element);
195         *     return result;
196         * }</pre>
197         *
198         * <p>Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations
199         * can be parallelized without requiring additional synchronization.
200         *
201         * <p>This is a terminal operation.
202         *
203         * \@apiNote There are many existing classes in the JDK whose signatures are
204         * well-suited for use with method references as arguments to {@code collect()}.
205         * For example, the following will accumulate strings into an {@code ArrayList}:
206         * <pre>{@code
207         *     List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add,
208         *                                                ArrayList::addAll);
209         * }</pre>
210         *
211         * <p>The following will take a stream of strings and concatenates them into a
212         * single string:
213         * <pre>{@code
214         *     String concat = stringStream.collect(StringBuilder::new, StringBuilder::append,
215         *                                          StringBuilder::append)
216         *                                 .toString();
217         * }</pre>
218         *
219         * @param <R> type of the result
220         * @param <A> Type of the accumulator.
221         * @param pupplier a function that creates a new result container. For a
222         *                 parallel execution, this function may be called
223         *                 multiple times and must return a fresh value each time.
224         * @param accumulator An associative, non-interfering, stateless function for
225         *   incorporating an additional element into a result
226         * @param combiner An associative, non-interfering, stateless
227         *   function for combining two values, which must be compatible with the
228         *   accumulator function
229         * @return The result of the reduction
230         */
231        public <A, R> R collect(final Supplier<R> pupplier, final BiConsumer<R, ? super O> accumulator, final BiConsumer<R, R> combiner) {
232            makeTerminated();
233            return stream().collect(pupplier, accumulator, combiner);
234        }
235
236        /**
237         * Performs a reduction on the elements of this stream, using the provided
238         * identity value and an associative accumulation function, and returns
239         * the reduced value.  This is equivalent to:
240         * <pre>{@code
241         *     T result = identity;
242         *     for (T element : this stream)
243         *         result = accumulator.apply(result, element)
244         *     return result;
245         * }</pre>
246         *
247         * but is not constrained to execute sequentially.
248         *
249         * <p>The {@code identity} value must be an identity for the accumulator
250         * function. This means that for all {@code t},
251         * {@code accumulator.apply(identity, t)} is equal to {@code t}.
252         * The {@code accumulator} function must be an associative function.
253         *
254         * <p>This is a terminal operation.
255         *
256         * \@apiNote Sum, min, max, average, and string concatenation are all special
257         * cases of reduction. Summing a stream of numbers can be expressed as:
258         *
259         * <pre>{@code
260         *     Integer sum = integers.reduce(0, (a, b) -> a+b);
261         * }</pre>
262         *
263         * or:
264         *
265         * <pre>{@code
266         *     Integer sum = integers.reduce(0, Integer::sum);
267         * }</pre>
268         *
269         * <p>While this may seem a more roundabout way to perform an aggregation
270         * compared to simply mutating a running total in a loop, reduction
271         * operations parallelize more gracefully, without needing additional
272         * synchronization and with greatly reduced risk of data races.
273         *
274         * @param identity the identity value for the accumulating function
275         * @param accumulator an associative, non-interfering, stateless
276         *                    function for combining two values
277         * @return the result of the reduction
278         */
279        public O reduce(final O identity, final BinaryOperator<O> accumulator) {
280            makeTerminated();
281            return stream().reduce(identity, accumulator);
282        }
283
284        /**
285         * Returns a stream consisting of the results of applying the given
286         * function to the elements of this stream.
287         *
288         * <p>This is an intermediate operation.
289         *
290         * @param <R> The element type of the new stream
291         * @param mapper A non-interfering, stateless function to apply to each element
292         * @return the new stream
293         */
294        public <R> FailableStream<R> map(final FailableFunction<O, R, ?> mapper) {
295            assertNotTerminated();
296            return new FailableStream<>(stream.map(Functions.asFunction(mapper)));
297        }
298
299        /**
300         * Converts the FailableStream into an equivalent stream.
301         * @return A stream, which will return the same elements, which this FailableStream would return.
302         */
303        public Stream<O> stream() {
304            return stream;
305        }
306
307        /**
308         * Returns whether all elements of this stream match the provided predicate.
309         * May not evaluate the predicate on all elements if not necessary for
310         * determining the result.  If the stream is empty then {@code true} is
311         * returned and the predicate is not evaluated.
312         *
313         * <p>This is a short-circuiting terminal operation.
314         *
315         * \@apiNote
316         * This method evaluates the <em>universal quantification</em> of the
317         * predicate over the elements of the stream (for all x P(x)).  If the
318         * stream is empty, the quantification is said to be <em>vacuously
319         * satisfied</em> and is always {@code true} (regardless of P(x)).
320         *
321         * @param predicate A non-interfering, stateless predicate to apply to
322         * elements of this stream
323         * @return {@code true} If either all elements of the stream match the
324         * provided predicate or the stream is empty, otherwise {@code false}.
325         */
326        public boolean allMatch(final FailablePredicate<O, ?> predicate) {
327            assertNotTerminated();
328            return stream().allMatch(Functions.asPredicate(predicate));
329        }
330
331        /**
332         * Returns whether any elements of this stream match the provided
333         * predicate.  May not evaluate the predicate on all elements if not
334         * necessary for determining the result.  If the stream is empty then
335         * {@code false} is returned and the predicate is not evaluated.
336         *
337         * <p>This is a short-circuiting terminal operation.
338         *
339         * \@apiNote
340         * This method evaluates the <em>existential quantification</em> of the
341         * predicate over the elements of the stream (for some x P(x)).
342         *
343         * @param predicate A non-interfering, stateless predicate to apply to
344         * elements of this stream
345         * @return {@code true} if any elements of the stream match the provided
346         * predicate, otherwise {@code false}
347         */
348        public boolean anyMatch(final FailablePredicate<O, ?> predicate) {
349            assertNotTerminated();
350            return stream().anyMatch(Functions.asPredicate(predicate));
351        }
352    }
353
354    /**
355     * Converts the given {@link Stream stream} into a {@link FailableStream}.
356     * This is basically a simplified, reduced version of the {@link Stream}
357     * class, with the same underlying element stream, except that failable
358     * objects, like {@link FailablePredicate}, {@link FailableFunction}, or
359     * {@link FailableConsumer} may be applied, instead of
360     * {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is
361     * to rewrite a code snippet like this:
362     * <pre>
363     *     final List&lt;O&gt; list;
364     *     final Method m;
365     *     final Function&lt;O,String&gt; mapper = (o) -&gt; {
366     *         try {
367     *             return (String) m.invoke(o);
368     *         } catch (Throwable t) {
369     *             throw Functions.rethrow(t);
370     *         }
371     *     };
372     *     final List&lt;String&gt; strList = list.stream()
373     *         .map(mapper).collect(Collectors.toList());
374     *  </pre>
375     *  as follows:
376     *  <pre>
377     *     final List&lt;O&gt; list;
378     *     final Method m;
379     *     final List&lt;String&gt; strList = Functions.stream(list.stream())
380     *         .map((o) -&gt; (String) m.invoke(o)).collect(Collectors.toList());
381     *  </pre>
382     *  While the second version may not be <em>quite</em> as
383     *  efficient (because it depends on the creation of additional,
384     *  intermediate objects, of type FailableStream), it is much more
385     *  concise, and readable, and meets the spirit of Lambdas better
386     *  than the first version.
387     * @param <O> The streams element type.
388     * @param stream The stream, which is being converted.
389     * @return The {@link FailableStream}, which has been created by
390     *   converting the stream.
391     */
392    public static <O> FailableStream<O> stream(final Stream<O> stream) {
393        return new FailableStream<>(stream);
394    }
395
396    /**
397     * Converts the given {@link Collection} into a {@link FailableStream}.
398     * This is basically a simplified, reduced version of the {@link Stream}
399     * class, with the same underlying element stream, except that failable
400     * objects, like {@link FailablePredicate}, {@link FailableFunction}, or
401     * {@link FailableConsumer} may be applied, instead of
402     * {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is
403     * to rewrite a code snippet like this:
404     * <pre>
405     *     final List&lt;O&gt; list;
406     *     final Method m;
407     *     final Function&lt;O,String&gt; mapper = (o) -&gt; {
408     *         try {
409     *             return (String) m.invoke(o);
410     *         } catch (Throwable t) {
411     *             throw Functions.rethrow(t);
412     *         }
413     *     };
414     *     final List&lt;String&gt; strList = list.stream()
415     *         .map(mapper).collect(Collectors.toList());
416     *  </pre>
417     *  as follows:
418     *  <pre>
419     *     final List&lt;O&gt; list;
420     *     final Method m;
421     *     final List&lt;String&gt; strList = Functions.stream(list.stream())
422     *         .map((o) -&gt; (String) m.invoke(o)).collect(Collectors.toList());
423     *  </pre>
424     *  While the second version may not be <em>quite</em> as
425     *  efficient (because it depends on the creation of additional,
426     *  intermediate objects, of type FailableStream), it is much more
427     *  concise, and readable, and meets the spirit of Lambdas better
428     *  than the first version.
429     * @param <O> The streams element type.
430     * @param stream The stream, which is being converted.
431     * @return The {@link FailableStream}, which has been created by
432     *   converting the stream.
433     */
434    public static <O> FailableStream<O> stream(final Collection<O> stream) {
435        return stream(stream.stream());
436    }
437
438    public static class ArrayCollector<O> implements Collector<O, List<O>, O[]> {
439        private static final Set<Characteristics> characteristics = Collections.emptySet();
440        private final Class<O> elementType;
441
442        public ArrayCollector(final Class<O> elementType) {
443            this.elementType = elementType;
444        }
445
446        @Override
447        public Supplier<List<O>> supplier() {
448            return () -> new ArrayList<>();
449        }
450
451        @Override
452        public BiConsumer<List<O>, O> accumulator() {
453            return (list, o) -> {
454                list.add(o);
455            };
456        }
457
458        @Override
459        public BinaryOperator<List<O>> combiner() {
460            return (left, right) -> {
461                left.addAll(right);
462                return left;
463            };
464        }
465
466        @Override
467        public Function<List<O>, O[]> finisher() {
468            return (list) -> {
469                @SuppressWarnings("unchecked")
470                final O[] array = (O[]) Array.newInstance(elementType, list.size());
471                return list.toArray(array);
472            };
473        }
474
475        @Override
476        public Set<Characteristics> characteristics() {
477            return characteristics;
478        }
479    }
480
481    /**
482     * Returns a {@code Collector} that accumulates the input elements into a
483     * new array.
484     *
485     * @param pElementType Type of an element in the array.
486     * @param <O> the type of the input elements
487     * @return a {@code Collector} which collects all the input elements into an
488     * array, in encounter order
489     */
490    public static <O extends Object> Collector<O, ?, O[]> toArray(final Class<O> pElementType) {
491        return new ArrayCollector<>(pElementType);
492    }
493}