functionalj.stream.SupplierBackedIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
package functionalj.stream;
import java.util.Iterator;
import java.util.function.Supplier;
import functionalj.functions.ThrowFuncs;
import functionalj.result.NoMoreResultException;
public class SupplierBackedIterator implements Iterator {
/** Throw a no more element exception. This is used for generator. */
public static D noMoreElement() throws NoMoreResultException {
ThrowFuncs.doThrowFrom(()->new NoMoreResultException());
return (D)null;
}
private final Supplier supplier;
private DATA next;
public SupplierBackedIterator(Supplier supplier) {
this.supplier = supplier;
}
@Override
public boolean hasNext() {
try {
next = supplier.get();
return true;
} catch (NoMoreResultException e) {
return false;
}
}
@Override
public DATA next() {
return next;
}
}