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

gw.xml.simple.DescendentsIterator Maven / Gradle / Ivy

There is a newer version: 1.18.2
Show newest version
/*
 * Copyright 2014 Guidewire Software, Inc.
 */

package gw.xml.simple;

import gw.util.Stack;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

class DescendentsIterator implements Iterator {

  private Stack> _iterators;

  public DescendentsIterator(SimpleXmlNode root) {
    _iterators = new Stack>();
    _iterators.push(root.getChildren().iterator());
  }

  @Override
  public boolean hasNext() {
    while(!_iterators.isEmpty() && !_iterators.peek().hasNext()) {
      _iterators.pop();
    }

    return !_iterators.isEmpty();
  }

  @Override
  public SimpleXmlNode next() {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }

    Iterator top = _iterators.peek();
    SimpleXmlNode result = top.next();
    if (!result.getChildren().isEmpty()) {
      _iterators.push(result.getChildren().iterator());
    }

    return result;
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy