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

functionalj.stream.SupplierBackedIterator Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
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;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy