mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-23 13:14:32 +01:00
66 lines
2 KiB
Java
66 lines
2 KiB
Java
|
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<T> extends Iterable<T> {
|
||
|
|
||
|
static <T> Iterators<T> iterate(Supplier<T> producer) {
|
||
|
return () -> new AbstractIterator<T>() {
|
||
|
@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.
|
||
|
*
|
||
|
* <p>This method should be overridden when the {@link #spliterator()}
|
||
|
* method cannot return a spliterator that is {@code IMMUTABLE},
|
||
|
* {@code CONCURRENT}, or <em>late-binding</em>. (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<T> 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.
|
||
|
*
|
||
|
* <p>This method should be overridden when the {@link #spliterator()}
|
||
|
* method cannot return a spliterator that is {@code IMMUTABLE},
|
||
|
* {@code CONCURRENT}, or <em>late-binding</em>. (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<T> parallelStream() {
|
||
|
return StreamSupport.stream(spliterator(), true);
|
||
|
}
|
||
|
}
|