@Override
    public boolean hasNext() {
        if (stack.isEmpty()) {
            return false;
        }
        PeekableListIterator<SpanNode> iterator = stack.peek();
        if (!iterator.hasNext()) {
            stack.pop();
            return hasNext();
        }


        SpanNode node = iterator.peek();

        if (!iterator.traversed) {
            //we set the traversed flag on our way down
            iterator.traversed = true;
            stack.push(new PeekableListIterator<>(node.childIterator()));
            return hasNext();
        }

        return true;
    }
ListIterator implementation which performs a depth-first traversal of SpanNodes.

@author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
document/src/main/java/com/yahoo/document/annotation/RecursiveNodeIterator.java