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 extends T> source, @Nonnegative @Nonnull Predicate predicate) {
return stream(SpliteratorUtils.takeWhile(source.spliterator(), predicate), false)
.onClose(() -> AutoCloseableUtils.closeQuietly(source));
}
@Nonnull
public static Stream> batch(@Nonnull Stream extends T> 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 extends T> source, @Nonnegative int batchSize) {
return batch(source, () -> batchSize);
}
@Nonnull
public static Stream generate(@Nonnull Generator extends T> supplier) {
//noinspection unchecked
return (Stream) stream(SpliteratorUtils.generate(supplier), false)
.onClose(() -> AutoCloseableUtils.closeQuietly(supplier));
}
@Nonnull
public static Stream generate(@Nonnull Supplier extends T> supplier) {
return generate(() -> valueOf(supplier.get()));
}
}