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

com.github.saulis.enumerables.FunctionIterator Maven / Gradle / Ivy

The newest version!
package com.github.saulis.enumerables;

import java.util.Iterator;
import java.util.function.Function;
import java.util.function.Predicate;

public class FunctionIterator implements Iterator {

    private final Predicate predicate;
    private T seed;
    private final Function function;
    private int cursor;

    public FunctionIterator(T seed, Function function, Predicate predicate) {
        cursor = 0;
        this.seed = seed;
        this.function = function;
        this.predicate = predicate;
    }

    public FunctionIterator(T seed, Function function, int iterations) {
        this(seed, function, x -> x < iterations);
    }

    @Override
    public boolean hasNext() {
        return predicate.test(cursor);
    }

    @Override
    public T next() {
        cursor++;
        T value = seed;

        seed = function.apply(value);

        return value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy