no.motif.iter.SuccessorIterable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of motif Show documentation
Show all versions of motif Show documentation
A library helping you to compose behavior from smaller parts.
The newest version!
package no.motif.iter;
import static no.motif.Singular.optional;
import java.io.Serializable;
import java.util.Iterator;
import no.motif.f.Fn;
import no.motif.single.Optional;
/**
* Iterable where its elements are computed from an initial element.
*
* @param The type of object this iterable yields.
*/
final class SuccessorIterable implements Iterable, Serializable {
private final Optional firstElement;
private final Fn super T,? extends T> successor;
SuccessorIterable(F firstElement, Fn super T, ? extends T> successor) {
this.firstElement = optional(firstElement);
this.successor = successor;
}
@Override
public Iterator iterator() {
return new SimpleIterator() {
Optional extends T> next = firstElement;
@Override
protected Optional extends T> nextIfAvailable() {
Optional extends T> toReturn = next;
next = next.map(successor);
return toReturn;
}};
}
}