package com.minelittlepony.util; import java.util.function.Supplier; import java.util.stream.Stream; import java.util.stream.StreamSupport; import com.google.common.collect.AbstractIterator; public interface Iterators extends Iterable { static Iterators iterate(Supplier producer) { return () -> new AbstractIterator() { @Override protected T computeNext() { T next = producer.get(); if (next == null) { endOfData(); } return next; } }; } /** * Returns a sequential {@code Stream} with this collection as its source. * *

This method should be overridden when the {@link #spliterator()} * method cannot return a spliterator that is {@code IMMUTABLE}, * {@code CONCURRENT}, or late-binding. (See {@link #spliterator()} * for details.) * * @implSpec * The default implementation creates a sequential {@code Stream} from the * collection's {@code Spliterator}. * * @return a sequential {@code Stream} over the elements in this collection * @since 1.8 */ default Stream stream() { return StreamSupport.stream(spliterator(), false); } /** * Returns a possibly parallel {@code Stream} with this collection as its * source. It is allowable for this method to return a sequential stream. * *

This method should be overridden when the {@link #spliterator()} * method cannot return a spliterator that is {@code IMMUTABLE}, * {@code CONCURRENT}, or late-binding. (See {@link #spliterator()} * for details.) * * @implSpec * The default implementation creates a parallel {@code Stream} from the * collection's {@code Spliterator}. * * @return a possibly parallel {@code Stream} over the elements in this * collection * @since 1.8 */ default Stream parallelStream() { return StreamSupport.stream(spliterator(), true); } }