ASTIterator.java

package edu.udel.cis.vsl.tass.ast.impl;

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

import edu.udel.cis.vsl.tass.ast.IF.ASTNodeIF;

public class ASTIterator<T extends ASTNodeIF> implements Iterator<T> {

	int counter = 0;
	int numChildren;
	/** The node whose children this ASTIterator is iterating over. */
	ASTNodeIF baseNode;

	public ASTIterator(ASTNodeIF baseNode) {
		this.baseNode = baseNode;
		numChildren = baseNode.numChildren();
	}

	@Override
	public boolean hasNext() {
		return counter < numChildren;
	}

	@Override
	public T next() throws NoSuchElementException {
		if (counter >= numChildren)
			throw new NoSuchElementException(baseNode.toString() + " has no more children.");
		T nextChild = (T)(baseNode.child(counter));
		counter++;
		return nextChild;
	}

	/** This functionality is optional, and not used for an ASTIterator. */
	@Override
	public void remove() throws UnsupportedOperationException {
		throw new UnsupportedOperationException("Removing a child node is not supported.");
	}

}