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

org.echocat.jsu.StreamUtils Maven / Gradle / Ivy

package org.echocat.jsu;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static java.util.stream.StreamSupport.stream;
import static org.echocat.jsu.Generator.Value.valueOf;

public final class StreamUtils {

    @Nonnull
    public static  Stream takeWhile(@Nonnull Stream source, @Nonnegative @Nonnull Predicate predicate) {
        return stream(SpliteratorUtils.takeWhile(source.spliterator(), predicate), false)
            .onClose(() -> AutoCloseableUtils.closeQuietly(source));
    }

    @Nonnull
    public static  Stream> batch(@Nonnull Stream source, @Nonnegative @Nonnull Supplier batchSize) {
        //noinspection unchecked,rawtypes
        return (Stream) stream(SpliteratorUtils.batch(source.spliterator(), batchSize), false)
            .onClose(() -> AutoCloseableUtils.closeQuietly(source));
    }

    @Nonnull
    public static  Stream> batch(@Nonnull Stream source, @Nonnegative int batchSize) {
        return batch(source, () -> batchSize);
    }

    @Nonnull
    public static  Stream generate(@Nonnull Generator supplier) {
        //noinspection unchecked
        return (Stream) stream(SpliteratorUtils.generate(supplier), false)
            .onClose(() -> AutoCloseableUtils.closeQuietly(supplier));
    }

    @Nonnull
    public static  Stream generate(@Nonnull Supplier supplier) {
        return generate(() -> valueOf(supplier.get()));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy