All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dynodao.processor.util.StreamUtil Maven / Gradle / Ivy

package com.github.dynodao.processor.util;

import lombok.experimental.UtilityClass;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Spliterator;
import java.util.stream.Collector;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.stream.Collectors.toCollection;

/**
 * Utility methods for basic stream operations.
 */
@UtilityClass
public class StreamUtil {

    /**
     * Concatenates all of the objects into a single stream. If the passed object is a stream or collection, then
     * it is flattened into the returned stream.
     * @param objects the objects, or collections to be in the stream
     * @return the flattened single stream
     */
    @SuppressWarnings("unchecked")
    public static Stream concat(Object... objects) {
        return Arrays.stream(objects)
                .flatMap(obj -> {
                    if (obj instanceof Stream) {
                        return (Stream) obj;
                    } else if (obj instanceof Object[]) {
                        return Arrays.stream((Object[]) obj);
                    } else if (obj instanceof Collection) {
                        return ((Collection) obj).stream();
                    } else {
                        return Stream.of(obj);
                    }
                });
    }

    /**
     * Returns a {@link Collector} that accumulates the input elements into a {@link LinkedHashSet}.
     * @param  the type of the input elements
     * @return a {@link Collector} that accumulates the input elements into a {@link LinkedHashSet}
     */
    public static  Collector> toLinkedHashSet() {
        return toCollection(LinkedHashSet::new);
    }

    /**
     * A utility interface to add a stream() method to the {@link Iterable} interface.
     * @param  the type of element in the stream
     */
    public interface Streamable extends Iterable {

        /**
         * A stream over this {@link Iterable}.
         * @return a stream
         * @see Collection#stream()
         * @see StreamSupport#stream(Spliterator, boolean)
         */
        default Stream stream() {
            return StreamSupport.stream(spliterator(), false);
        }
    }

}