    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.stream;
    018
    019import java.util.ArrayList;
    020import java.util.Collection;
    021import java.util.Collections;
    022import java.util.Enumeration;
    023import java.util.Iterator;
    024import java.util.List;
    025import java.util.Objects;
    026import java.util.Set;
    027import java.util.Spliterator;
    028import java.util.Spliterators;
    029import java.util.Spliterators.AbstractSpliterator;
    030import java.util.function.BiConsumer;
    031import java.util.function.BinaryOperator;
    032import java.util.function.Consumer;
    033import java.util.function.Function;
    034import java.util.function.Predicate;
    035import java.util.function.Supplier;
    036import java.util.stream.Collector;
    037import java.util.stream.Collectors;
    038import java.util.stream.Stream;
    039import java.util.stream.StreamSupport;
    040
    041import org.apache.commons.lang3.ArrayUtils;
    042import org.apache.commons.lang3.function.Failable;
    043import org.apache.commons.lang3.function.FailableConsumer;
    044import org.apache.commons.lang3.function.FailableFunction;
    045import org.apache.commons.lang3.function.FailablePredicate;
    046
    047/**
    048 * Provides utility functions, and classes for working with the {@code java.util.stream} package, or more generally,
    049 * with Java 8 lambdas. More specifically, it attempts to address the fact that lambdas are supposed not to throw
    050 * Exceptions, at least not checked Exceptions, AKA instances of {@link Exception}. This enforces the use of constructs
    051 * like:
    052 *
    053 * <pre>
    054 * {@code
    055 * Consumer<java.lang.reflect.Method> consumer = m -> {
    056 *     try {
    057 *         m.invoke(o, args);
    058 *     } catch (Throwable t) {
    059 *         throw Failable.rethrow(t);
    060 *     }
    061 * };
    062 * stream.forEach(consumer);
    063 * }
    064 * </pre>
    065 * <p>
    066 * Using a {@link FailableStream}, this can be rewritten as follows:
    067 * </p>
    068 *
    069 * <pre>
    070 * {@code
    071 * Streams.failable(stream).forEach((m) -> m.invoke(o, args));
    072 * }
    073 * </pre>
    074 *
    075 * Obviously, the second version is much more concise and the spirit of Lambda expressions is met better than in the
    076 * first version.
    077 *
    078 * @see Stream
    079 * @see Failable
    080 * @since 3.11
    081 */
    082public class Streams {
    083
    084    /**
    085     * A Collector type for arrays.
    086     *
    087     * @param <E> The array type.
    088     */
    089    public static class ArrayCollector<E> implements Collector<E, List<E>, E[]> {
    090        private static final Set<Characteristics> characteristics = Collections.emptySet();
    091        private final Class<E> elementType;
    092
    093        /**
    094         * Constructs a new instance for the given element type.
    095         *
    096         * @param elementType The element type.
    097         */
    098        public ArrayCollector(final Class<E> elementType) {
    099            this.elementType = Objects.requireNonNull(elementType, "elementType");
    100        }
    101
    102        @Override
    103        public BiConsumer<List<E>, E> accumulator() {
    104            return List::add;
    105        }
    106
    107        @Override
    108        public Set<Characteristics> characteristics() {
    109            return characteristics;
    110        }
    111
    112        @Override
    113        public BinaryOperator<List<E>> combiner() {
    114            return (left, right) -> {
    115                left.addAll(right);
    116                return left;
    117            };
    118        }
    119
    120        @Override
    121        public Function<List<E>, E[]> finisher() {
    122            return list -> list.toArray(ArrayUtils.newInstance(elementType, list.size()));
    123        }
    124
    125        @Override
    126        public Supplier<List<E>> supplier() {
    127            return ArrayList::new;
    128        }
    129    }
    130
    131    /**
    132     * Helps implement {@link Streams#of(Enumeration)}.
    133     *
    134     * @param <T> The element type.
    135     */
    136    private static class EnumerationSpliterator<T> extends AbstractSpliterator<T> {
    137
    138        private final Enumeration<T> enumeration;
    139
    140        /**
    141         * Creates a spliterator reporting the given estimated size and additionalCharacteristics.
    142         *
    143         * @param estimatedSize the estimated size of this spliterator if known, otherwise {@code Long.MAX_VALUE}.
    144         * @param additionalCharacteristics properties of this spliterator's source or elements. If {@code SIZED} is reported then this spliterator will
    145         *        additionally report {@code SUBSIZED}.
    146         * @param enumeration The Enumeration to wrap.
    147         */
    148        protected EnumerationSpliterator(final long estimatedSize, final int additionalCharacteristics, final Enumeration<T> enumeration) {
    149            super(estimatedSize, additionalCharacteristics);
    150            this.enumeration = Objects.requireNonNull(enumeration, "enumeration");
    151        }
    152
    153        @Override
    154        public void forEachRemaining(final Consumer<? super T> action) {
    155            while (enumeration.hasMoreElements()) {
    156                next(action);
    157            }
    158        }
    159
    160        private boolean next(final Consumer<? super T> action) {
    161            action.accept(enumeration.nextElement());
    162            return true;
    163
    164        }
    165
    166        @Override
    167        public boolean tryAdvance(final Consumer<? super T> action) {
    168            return enumeration.hasMoreElements() && next(action);
    169        }
    170    }
    171
    172    /**
    173     * A reduced, and simplified version of a {@link Stream} with failable method signatures.
    174     *
    175     * @param <T> The streams element type.
    176     */
    177    public static class FailableStream<T> {
    178
    179        private Stream<T> stream;
    180        private boolean terminated;
    181
    182        /**
    183         * Constructs a new instance with the given {@code stream}.
    184         *
    185         * @param stream The stream.
    186         */
    187        public FailableStream(final Stream<T> stream) {
    188            this.stream = stream;
    189        }
    190
    191        /**
    192         * Returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all
    193         * elements if not necessary for determining the result. If the stream is empty then {@code true} is returned and the
    194         * predicate is not evaluated.
    195         *
    196         * <p>
    197         * This is a short-circuiting terminal operation.
    198         * </p>
    199         *
    200         * Note This method evaluates the <em>universal quantification</em> of the predicate over the elements of the stream
    201         * (for all x P(x)). If the stream is empty, the quantification is said to be <em>vacuously satisfied</em> and is always
    202         * {@code true} (regardless of P(x)).
    203         *
    204         * @param predicate A non-interfering, stateless predicate to apply to elements of this stream
    205         * @return {@code true} If either all elements of the stream match the provided predicate or the stream is empty,
    206         *         otherwise {@code false}.
    207         */
    208        public boolean allMatch(final FailablePredicate<T, ?> predicate) {
    209            assertNotTerminated();
    210            return stream().allMatch(Failable.asPredicate(predicate));
    211        }
    212
    213        /**
    214         * Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all
    215         * elements if not necessary for determining the result. If the stream is empty then {@code false} is returned and the
    216         * predicate is not evaluated.
    217         *
    218         * <p>
    219         * This is a short-circuiting terminal operation.
    220         * </p>
    221         *
    222         * Note This method evaluates the <em>existential quantification</em> of the predicate over the elements of the stream
    223         * (for some x P(x)).
    224         *
    225         * @param predicate A non-interfering, stateless predicate to apply to elements of this stream
    226         * @return {@code true} if any elements of the stream match the provided predicate, otherwise {@code false}
    227         */
    228        public boolean anyMatch(final FailablePredicate<T, ?> predicate) {
    229            assertNotTerminated();
    230            return stream().anyMatch(Failable.asPredicate(predicate));
    231        }
    232
    233        /**
    234         * Throws IllegalStateException if this stream is already terminated.
    235         *
    236         * @throws IllegalStateException if this stream is already terminated.
    237         */
    238        protected void assertNotTerminated() {
    239            if (terminated) {
    240                throw new IllegalStateException("This stream is already terminated.");
    241            }
    242        }
    243
    244        /**
    245         * Performs a mutable reduction operation on the elements of this stream using a {@link Collector}. A {@link Collector}
    246         * encapsulates the functions used as arguments to {@link #collect(Supplier, BiConsumer, BiConsumer)}, allowing for
    247         * reuse of collection strategies and composition of collect operations such as multiple-level grouping or partitioning.
    248         *
    249         * <p>
    250         * If the underlying stream is parallel, and the {@link Collector} is concurrent, and either the stream is unordered or
    251         * the collector is unordered, then a concurrent reduction will be performed (see {@link Collector} for details on
    252         * concurrent reduction.)
    253         * </p>
    254         *
    255         * <p>
    256         * This is a terminal operation.
    257         * </p>
    258         *
    259         * <p>
    260         * When executed in parallel, multiple intermediate results may be instantiated, populated, and merged so as to maintain
    261         * isolation of mutable data structures. Therefore, even when executed in parallel with non-thread-safe data structures
    262         * (such as {@link ArrayList}), no additional synchronization is needed for a parallel reduction.
    263         * </p>
    264         *
    265         * Note The following will accumulate strings into an ArrayList:
    266         *
    267         * <pre>
    268         * {@code
    269         *     List<String> asList = stringStream.collect(Collectors.toList());
    270         * }
    271         * </pre>
    272         *
    273         * <p>
    274         * The following will classify {@code Person} objects by city:
    275         * </p>
    276         *
    277         * <pre>
    278         * {@code
    279         *     Map<String, List<Person>> peopleByCity = personStream.collect(Collectors.groupingBy(Person::getCity));
    280         * }
    281         * </pre>
    282         *
    283         * <p>
    284         * The following will classify {@code Person} objects by state and city, cascading two {@link Collector}s together:
    285         * </p>
    286         *
    287         * <pre>
    288         * {@code
    289         *     Map<String, Map<String, List<Person>>> peopleByStateAndCity = personStream
    290         *         .collect(Collectors.groupingBy(Person::getState, Collectors.groupingBy(Person::getCity)));
    291         * }
    292         * </pre>
    293         *
    294         * @param <R> the type of the result
    295         * @param <A> the intermediate accumulation type of the {@link Collector}
    296         * @param collector the {@link Collector} describing the reduction
    297         * @return the result of the reduction
    298         * @see #collect(Supplier, BiConsumer, BiConsumer)
    299         * @see Collectors
    300         */
    301        public <A, R> R collect(final Collector<? super T, A, R> collector) {
    302            makeTerminated();
    303            return stream().collect(collector);
    304        }
    305
    306        /**
    307         * Performs a mutable reduction operation on the elements of this FailableStream. A mutable reduction is one in which
    308         * the reduced value is a mutable result container, such as an {@link ArrayList}, and elements are incorporated by
    309         * updating the state of the result rather than by replacing the result. This produces a result equivalent to:
    310         *
    311         * <pre>
    312         * {@code
    313         *     R result = supplier.get();
    314         *     for (T element : this stream)
    315         *         accumulator.accept(result, element);
    316         *     return result;
    317         * }
    318         * </pre>
    319         *
    320         * <p>
    321         * Like {@link #reduce(Object, BinaryOperator)}, {@code collect} operations can be parallelized without requiring
    322         * additional synchronization.
    323         * </p>
    324         *
    325         * <p>
    326         * This is a terminal operation.
    327         * </p>
    328         *
    329         * Note There are many existing classes in the JDK whose signatures are well-suited for use with method references as
    330         * arguments to {@code collect()}. For example, the following will accumulate strings into an {@link ArrayList}:
    331         *
    332         * <pre>
    333         * {@code
    334         *     List<String> asList = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    335         * }
    336         * </pre>
    337         *
    338         * <p>
    339         * The following will take a stream of strings and concatenates them into a single string:
    340         * </p>
    341         *
    342         * <pre>
    343         * {@code
    344         *     String concat = stringStream.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();
    345         * }
    346         * </pre>
    347         *
    348         * @param <R> type of the result
    349         * @param <A> Type of the accumulator.
    350         * @param supplier a function that creates a new result container. For a parallel execution, this function may be called
    351         *        multiple times and must return a fresh value each time.
    352         * @param accumulator An associative, non-interfering, stateless function for incorporating an additional element into a
    353         *        result
    354         * @param combiner An associative, non-interfering, stateless function for combining two values, which must be
    355         *        compatible with the accumulator function
    356         * @return The result of the reduction
    357         */
    358        public <A, R> R collect(final Supplier<R> supplier, final BiConsumer<R, ? super T> accumulator, final BiConsumer<R, R> combiner) {
    359            makeTerminated();
    360            return stream().collect(supplier, accumulator, combiner);
    361        }
    362
    363        /**
    364         * Returns a FailableStream consisting of the elements of this stream that match the given FailablePredicate.
    365         *
    366         * <p>
    367         * This is an intermediate operation.
    368         * </p>
    369         *
    370         * @param predicate a non-interfering, stateless predicate to apply to each element to determine if it should be
    371         *        included.
    372         * @return the new stream
    373         */
    374        public FailableStream<T> filter(final FailablePredicate<T, ?> predicate) {
    375            assertNotTerminated();
    376            stream = stream.filter(Failable.asPredicate(predicate));
    377            return this;
    378        }
    379
    380        /**
    381         * Performs an action for each element of this stream.
    382         *
    383         * <p>
    384         * This is a terminal operation.
    385         * </p>
    386         *
    387         * <p>
    388         * The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does
    389         * <em>not</em> guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of
    390         * parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library
    391         * chooses. If the action accesses shared state, it is responsible for providing the required synchronization.
    392         * </p>
    393         *
    394         * @param action a non-interfering action to perform on the elements
    395         */
    396        public void forEach(final FailableConsumer<T, ?> action) {
    397            makeTerminated();
    398            stream().forEach(Failable.asConsumer(action));
    399        }
    400
    401        /**
    402         * Marks this stream as terminated.
    403         *
    404         * @throws IllegalStateException if this stream is already terminated.
    405         */
    406        protected void makeTerminated() {
    407            assertNotTerminated();
    408            terminated = true;
    409        }
    410
    411        /**
    412         * Returns a stream consisting of the results of applying the given function to the elements of this stream.
    413         *
    414         * <p>
    415         * This is an intermediate operation.
    416         * </p>
    417         *
    418         * @param <R> The element type of the new stream
    419         * @param mapper A non-interfering, stateless function to apply to each element
    420         * @return the new stream
    421         */
    422        public <R> FailableStream<R> map(final FailableFunction<T, R, ?> mapper) {
    423            assertNotTerminated();
    424            return new FailableStream<>(stream.map(Failable.asFunction(mapper)));
    425        }
    426
    427        /**
    428         * Performs a reduction on the elements of this stream, using the provided identity value and an associative
    429         * accumulation function, and returns the reduced value. This is equivalent to:
    430         *
    431         * <pre>
    432         * {@code
    433         *     T result = identity;
    434         *     for (T element : this stream)
    435         *         result = accumulator.apply(result, element)
    436         *     return result;
    437         * }
    438         * </pre>
    439         *
    440         * but is not constrained to execute sequentially.
    441         *
    442         * <p>
    443         * The {@code identity} value must be an identity for the accumulator function. This means that for all {@code t},
    444         * {@code accumulator.apply(identity, t)} is equal to {@code t}. The {@code accumulator} function must be an associative
    445         * function.
    446         * </p>
    447         *
    448         * <p>
    449         * This is a terminal operation.
    450         * </p>
    451         *
    452         * Note Sum, min, max, average, and string concatenation are all special cases of reduction. Summing a stream of numbers
    453         * can be expressed as:
    454         *
    455         * <pre>
    456         * {@code
    457         *     Integer sum = integers.reduce(0, (a, b) -> a + b);
    458         * }
    459         * </pre>
    460         *
    461         * or:
    462         *
    463         * <pre>
    464         * {@code
    465         *     Integer sum = integers.reduce(0, Integer::sum);
    466         * }
    467         * </pre>
    468         *
    469         * <p>
    470         * While this may seem a more roundabout way to perform an aggregation compared to simply mutating a running total in a
    471         * loop, reduction operations parallelize more gracefully, without needing additional synchronization and with greatly
    472         * reduced risk of data races.
    473         * </p>
    474         *
    475         * @param identity the identity value for the accumulating function
    476         * @param accumulator an associative, non-interfering, stateless function for combining two values
    477         * @return the result of the reduction
    478         */
    479        public T reduce(final T identity, final BinaryOperator<T> accumulator) {
    480            makeTerminated();
    481            return stream().reduce(identity, accumulator);
    482        }
    483
    484        /**
    485         * Converts the FailableStream into an equivalent stream.
    486         *
    487         * @return A stream, which will return the same elements, which this FailableStream would return.
    488         */
    489        public Stream<T> stream() {
    490            return stream;
    491        }
    492    }
    493
    494    /**
    495     * Converts the given {@link Collection} into a {@link FailableStream}. This is basically a simplified, reduced version
    496     * of the {@link Stream} class, with the same underlying element stream, except that failable objects, like
    497     * {@link FailablePredicate}, {@link FailableFunction}, or {@link FailableConsumer} may be applied, instead of
    498     * {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is to rewrite a code snippet like this:
    499     *
    500     * <pre>
    501     * {@code
    502     * final List<O> list;
    503     * final Method m;
    504     * final Function<O, String> mapper = (o) -> {
    505     *     try {
    506     *         return (String) m.invoke(o);
    507     *     } catch (Throwable t) {
    508     *         throw Failable.rethrow(t);
    509     *     }
    510     * };
    511     * final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
    512     * }
    513     * </pre>
    514     *
    515     * as follows:
    516     *
    517     * <pre>
    518     * {@code
    519     * final List<O> list;
    520     * final Method m;
    521     * final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
    522     * }
    523     * </pre>
    524     *
    525     * While the second version may not be <em>quite</em> as efficient (because it depends on the creation of additional,
    526     * intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
    527     * better than the first version.
    528     *
    529     * @param <T> The streams element type.
    530     * @param stream The stream, which is being converted.
    531     * @return The {@link FailableStream}, which has been created by converting the stream.
    532     * @since 3.13.0
    533     */
    534    public static <T> FailableStream<T> failableStream(final Collection<T> stream) {
    535        return failableStream(of(stream));
    536    }
    537
    538    /**
    539     * Converts the given {@link Stream stream} into a {@link FailableStream}. This is basically a simplified, reduced
    540     * version of the {@link Stream} class, with the same underlying element stream, except that failable objects, like
    541     * {@link FailablePredicate}, {@link FailableFunction}, or {@link FailableConsumer} may be applied, instead of
    542     * {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is to rewrite a code snippet like this:
    543     *
    544     * <pre>
    545     * {@code
    546     * final List<O> list;
    547     * final Method m;
    548     * final Function<O, String> mapper = (o) -> {
    549     *     try {
    550     *         return (String) m.invoke(o);
    551     *     } catch (Throwable t) {
    552     *         throw Failable.rethrow(t);
    553     *     }
    554     * };
    555     * final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
    556     * }
    557     * </pre>
    558     *
    559     * as follows:
    560     *
    561     * <pre>
    562     * {@code
    563     * final List<O> list;
    564     * final Method m;
    565     * final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
    566     * }
    567     * </pre>
    568     *
    569     * While the second version may not be <em>quite</em> as efficient (because it depends on the creation of additional,
    570     * intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
    571     * better than the first version.
    572     *
    573     * @param <T> The streams element type.
    574     * @param stream The stream, which is being converted.
    575     * @return The {@link FailableStream}, which has been created by converting the stream.
    576     * @since 3.13.0
    577     */
    578    public static <T> FailableStream<T> failableStream(final Stream<T> stream) {
    579        return new FailableStream<>(stream);
    580    }
    581
    582    /**
    583     * Streams only instances of the give Class in a collection.
    584     * <p>
    585     * This method shorthand for:
    586     * </p>
    587     * <pre>
    588     * {@code (Stream<E>) Streams.toStream(collection).filter(collection, SomeClass.class::isInstance);}
    589     * </pre>
    590     *
    591     * @param <E> the type of elements in the collection we want to stream.
    592     * @param clazz the type of elements in the collection we want to stream.
    593     * @param collection the collection to stream or null.
    594     * @return A non-null stream that only provides instances we want.
    595     * @since 3.13.0
    596     */
    597    public static <E> Stream<E> instancesOf(final Class<? super E> clazz, final Collection<? super E> collection) {
    598        return instancesOf(clazz, of(collection));
    599    }
    600
    601    @SuppressWarnings("unchecked") // After the isInstance check, we still need to type-cast.
    602    private static <E> Stream<E> instancesOf(final Class<? super E> clazz, final Stream<?> stream) {
    603        return (Stream<E>) of(stream).filter(clazz::isInstance);
    604    }
    605
    606    /**
    607     * Streams the non-null elements of a collection.
    608     *
    609     * @param <E> the type of elements in the collection.
    610     * @param collection the collection to stream or null.
    611     * @return A non-null stream that filters out null elements.
    612     * @since 3.13.0
    613     */
    614    public static <E> Stream<E> nonNull(final Collection<E> collection) {
    615        return of(collection).filter(Objects::nonNull);
    616    }
    617
    618    /**
    619     * Streams the non-null elements of an array.
    620     *
    621     * @param <E> the type of elements in the collection.
    622     * @param array the array to stream or null.
    623     * @return A non-null stream that filters out null elements.
    624     * @since 3.13.0
    625     */
    626    @SafeVarargs
    627    public static <E> Stream<E> nonNull(final E... array) {
    628        return nonNull(of(array));
    629    }
    630
    631    /**
    632     * Streams the non-null elements of a stream.
    633     *
    634     * @param <E> the type of elements in the collection.
    635     * @param stream the stream to stream or null.
    636     * @return A non-null stream that filters out null elements.
    637     * @since 3.13.0
    638     */
    639    public static <E> Stream<E> nonNull(final Stream<E> stream) {
    640        return of(stream).filter(Objects::nonNull);
    641    }
    642
    643    /**
    644     * Delegates to {@link Collection#stream()} or returns {@link Stream#empty()} if the collection is null.
    645     *
    646     * @param <E> the type of elements in the collection.
    647     * @param collection the collection to stream or null.
    648     * @return {@link Collection#stream()} or {@link Stream#empty()} if the collection is null.
    649     * @since 3.13.0
    650     */
    651    public static <E> Stream<E> of(final Collection<E> collection) {
    652        return collection == null ? Stream.empty() : collection.stream();
    653    }
    654
    655    /**
    656     * Streams the elements of the given enumeration in order.
    657     *
    658     * @param <E> The enumeration element type.
    659     * @param enumeration The enumeration to stream.
    660     * @return a new stream.
    661     * @since 3.13.0
    662     */
    663    public static <E> Stream<E> of(final Enumeration<E> enumeration) {
    664        return StreamSupport.stream(new EnumerationSpliterator<>(Long.MAX_VALUE, Spliterator.ORDERED, enumeration), false);
    665    }
    666
    667    /**
    668     * Creates a stream on the given Iterable.
    669     *
    670     * @param <E> the type of elements in the Iterable.
    671     * @param iterable the Iterable to stream or null.
    672     * @return a new Stream or {@link Stream#empty()} if the Iterable is null.
    673     * @since 3.13.0
    674     */
    675    public static <E> Stream<E> of(final Iterable<E> iterable) {
    676        return iterable == null ? Stream.empty() : StreamSupport.stream(iterable.spliterator(), false);
    677    }
    678
    679    /**
    680     * Creates a stream on the given Iterator.
    681     *
    682     * @param <E> the type of elements in the Iterator.
    683     * @param iterator the Iterator to stream or null.
    684     * @return a new Stream or {@link Stream#empty()} if the Iterator is null.
    685     * @since 3.13.0
    686     */
    687    public static <E> Stream<E> of(final Iterator<E> iterator) {
    688        return iterator == null ? Stream.empty() : StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
    689    }
    690
    691    /**
    692     * Returns the stream or {@link Stream#empty()} if the stream is null.
    693     *
    694     * @param <E> the type of elements in the collection.
    695     * @param stream the stream to stream or null.
    696     * @return the stream or {@link Stream#empty()} if the stream is null.
    697     * @since 3.13.0
    698     */
    699    private static <E> Stream<E> of(final Stream<E> stream) {
    700        return stream == null ? Stream.empty() : stream;
    701    }
    702
    703    /**
    704     * Null-safe version of {@link Stream#of(Object[])}.
    705     *
    706     * @param <T> the type of stream elements.
    707     * @param values the elements of the new stream, may be {@code null}.
    708     * @return the new stream on {@code values} or {@link Stream#empty()}.
    709     * @since 3.13.0
    710     */
    711    @SafeVarargs // Creating a stream from an array is safe
    712    public static <T> Stream<T> of(final T... values) {
    713        return values == null ? Stream.empty() : Stream.of(values);
    714    }
    715
    716    /**
    717     * Converts the given {@link Collection} into a {@link FailableStream}. This is basically a simplified, reduced version
    718     * of the {@link Stream} class, with the same underlying element stream, except that failable objects, like
    719     * {@link FailablePredicate}, {@link FailableFunction}, or {@link FailableConsumer} may be applied, instead of
    720     * {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is to rewrite a code snippet like this:
    721     *
    722     * <pre>
    723     * {@code
    724     * final List<O> list;
    725     * final Method m;
    726     * final Function<O, String> mapper = (o) -> {
    727     *     try {
    728     *         return (String) m.invoke(o);
    729     *     } catch (Throwable t) {
    730     *         throw Failable.rethrow(t);
    731     *     }
    732     * };
    733     * final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
    734     * }
    735     * </pre>
    736     *
    737     * as follows:
    738     *
    739     * <pre>
    740     * {@code
    741     * final List<O> list;
    742     * final Method m;
    743     * final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
    744     * }
    745     * </pre>
    746     *
    747     * While the second version may not be <em>quite</em> as efficient (because it depends on the creation of additional,
    748     * intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
    749     * better than the first version.
    750     *
    751     * @param <E> The streams element type.
    752     * @param collection The stream, which is being converted.
    753     * @return The {@link FailableStream}, which has been created by converting the stream.
    754     * @deprecated Use {@link #failableStream(Collection)}.
    755     */
    756    @Deprecated
    757    public static <E> FailableStream<E> stream(final Collection<E> collection) {
    758        return failableStream(collection);
    759    }
    760
    761    /**
    762     * Converts the given {@link Stream stream} into a {@link FailableStream}. This is basically a simplified, reduced
    763     * version of the {@link Stream} class, with the same underlying element stream, except that failable objects, like
    764     * {@link FailablePredicate}, {@link FailableFunction}, or {@link FailableConsumer} may be applied, instead of
    765     * {@link Predicate}, {@link Function}, or {@link Consumer}. The idea is to rewrite a code snippet like this:
    766     *
    767     * <pre>
    768     * {@code
    769     * final List<O> list;
    770     * final Method m;
    771     * final Function<O, String> mapper = (o) -> {
    772     *     try {
    773     *         return (String) m.invoke(o);
    774     *     } catch (Throwable t) {
    775     *         throw Failable.rethrow(t);
    776     *     }
    777     * };
    778     * final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
    779     * }
    780     * </pre>
    781     *
    782     * as follows:
    783     *
    784     * <pre>
    785     * {@code
    786     * final List<O> list;
    787     * final Method m;
    788     * final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
    789     * }
    790     * </pre>
    791     *
    792     * While the second version may not be <em>quite</em> as efficient (because it depends on the creation of additional,
    793     * intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
    794     * better than the first version.
    795     *
    796     * @param <T> The streams element type.
    797     * @param stream The stream, which is being converted.
    798     * @return The {@link FailableStream}, which has been created by converting the stream.
    799     * @deprecated Use {@link #failableStream(Stream)}.
    800     */
    801    @Deprecated
    802    public static <T> FailableStream<T> stream(final Stream<T> stream) {
    803        return failableStream(stream);
    804    }
    805
    806    /**
    807     * Returns a {@link Collector} that accumulates the input elements into a new array.
    808     *
    809     * @param pElementType Type of an element in the array.
    810     * @param <T> the type of the input elements
    811     * @return a {@link Collector} which collects all the input elements into an array, in encounter order
    812     */
    813    public static <T> Collector<T, ?, T[]> toArray(final Class<T> pElementType) {
    814        return new ArrayCollector<>(pElementType);
    815    }
    816}