com.github.saulis.enumerables.FunctionIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of enumerables Show documentation
Show all versions of enumerables Show documentation
Iterable Java Collections and Arrays made Functional
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;
}
}