use of java.util.Iterator in project groovy-core by groovy.
the class GPathResult method breadthFirst.
/**
* Provides an Iterator over all the nodes of this GPathResult using a breadth-first traversal.
*
* @return the <code>Iterator</code> of (breadth-first) ordered GPathResults
*/
public Iterator breadthFirst() {
return new Iterator() {
private final List list = new LinkedList();
private Iterator iter = iterator();
private GPathResult next = getNextByBreadth();
public boolean hasNext() {
return this.next != null;
}
public Object next() {
try {
return this.next;
} finally {
this.next = getNextByBreadth();
}
}
public void remove() {
throw new UnsupportedOperationException();
}
private GPathResult getNextByBreadth() {
List children = new ArrayList();
while (this.iter.hasNext() || !children.isEmpty()) {
if (this.iter.hasNext()) {
final GPathResult node = (GPathResult) this.iter.next();
this.list.add(node);
this.list.add(this.iter);
children.add(node.children());
} else {
List nextLevel = new ArrayList();
for (Object child : children) {
GPathResult next = (GPathResult) child;
Iterator iterator = next.iterator();
while (iterator.hasNext()) {
nextLevel.add(iterator.next());
}
}
this.iter = nextLevel.iterator();
children = new ArrayList();
}
}
if (this.list.isEmpty()) {
return null;
} else {
GPathResult result = (GPathResult) this.list.get(0);
this.list.remove(0);
this.iter = (Iterator) this.list.get(0);
this.list.remove(0);
return result;
}
}
};
}
use of java.util.Iterator in project groovy-core by groovy.
the class GPathResult method depthFirst.
/**
* Provides an Iterator over all the nodes of this GPathResult using a depth-first traversal.
*
* @return the <code>Iterator</code> of (depth-first) ordered GPathResults
*/
public Iterator depthFirst() {
return new Iterator() {
private final List list = new LinkedList();
private final Stack stack = new Stack();
private Iterator iter = iterator();
private GPathResult next = getNextByDepth();
public boolean hasNext() {
return this.next != null;
}
public Object next() {
try {
return this.next;
} finally {
this.next = getNextByDepth();
}
}
public void remove() {
throw new UnsupportedOperationException();
}
private GPathResult getNextByDepth() {
while (this.iter.hasNext()) {
final GPathResult node = (GPathResult) this.iter.next();
this.list.add(node);
this.stack.push(this.iter);
this.iter = node.children().iterator();
}
if (this.list.isEmpty()) {
return null;
} else {
GPathResult result = (GPathResult) this.list.get(0);
this.list.remove(0);
this.iter = (Iterator) this.stack.pop();
return result;
}
}
};
}
use of java.util.Iterator in project groovy-core by groovy.
the class NodeChildren method size.
public synchronized int size() {
if (this.size == -1) {
final Iterator iter = iterator();
this.size = 0;
while (iter.hasNext()) {
iter.next();
this.size++;
}
}
return this.size;
}
use of java.util.Iterator in project groovy-core by groovy.
the class NodeChildren method replaceNode.
protected void replaceNode(final Closure newValue) {
final Iterator iter = iterator();
while (iter.hasNext()) {
final NodeChild result = (NodeChild) iter.next();
result.replaceNode(newValue);
}
}
use of java.util.Iterator in project groovy-core by groovy.
the class DOMCategory method toString.
private static String toString(NodeList self) {
StringBuilder sb = new StringBuilder();
sb.append("[");
Iterator it = XmlGroovyMethods.iterator(self);
while (it.hasNext()) {
if (sb.length() > 1)
sb.append(", ");
sb.append(it.next().toString());
}
sb.append("]");
return sb.toString();
}
Aggregations