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

cucumber.runtime.io.FlatteningIterator Maven / Gradle / Ivy

There is a newer version: 7.18.1
Show newest version
package cucumber.runtime.io;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class FlatteningIterator implements Iterator {
    private final Deque> iterators = new ArrayDeque>();

    private T next;
    private boolean nextBlank = true;

    public void push(Iterator iterator) {
        iterators.addFirst(iterator);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }

    private void moveToNext() {
        if (nextBlank && !this.iterators.isEmpty()) {
            if (!iterators.peek().hasNext()) {
                iterators.removeFirst();
                moveToNext();
            } else {
                final Object next = iterators.peekFirst().next();
                if (next instanceof Iterator) {
                    push((Iterator) next);
                    moveToNext();
                } else {
                    this.next = (T) next;
                    nextBlank = false;
                }
            }
        }
    }

    @Override
    public T next() {
        moveToNext();

        if (nextBlank) {
            throw new NoSuchElementException();
        } else {
            T next = this.next;
            this.next = null;
            nextBlank = true;
            return next;
        }
    }

    @Override
    public boolean hasNext() {
        moveToNext();
        return !nextBlank;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy