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

no.motif.iter.SkipLeadingIterable Maven / Gradle / Ivy

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();
                }
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy