no.motif.iter.SkipLeadingIterable 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.none;
import static no.motif.Singular.optional;
import java.io.Serializable;
import java.util.Iterator;
import no.motif.single.Optional;
class SkipLeadingIterable implements Iterable, Serializable {
private final Iterable elements;
private final int toSkip;
SkipLeadingIterable(int skip, Iterable elements) {
this.toSkip = skip;
this.elements = elements;
}
@Override
public Iterator iterator() {
return new SimpleIterator() {
Iterator iterator = elements.iterator();
int skipped = 0;
@Override
protected Optional nextIfAvailable() {
if (!iterator.hasNext()) {
return none();
} else if (skipped == toSkip) {
return optional(iterator.next());
} else {
iterator.next();
skipped++;
return nextIfAvailable();
}
}
};
}
}