SequenceNode.java

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

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

import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.Vector;

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

public class SequenceNode<T extends ASTNodeIF> extends ASTNode implements SequenceNodeIF<T> {

	private Vector<T> nodes;
	
	private String childQName = null;
	
	public SequenceNode(long id, Vector<T> nodes) {
		super(id);
		this.nodes = nodes;
	}
	
	public SequenceNode(long id) {
		super(id);
		nodes = new Vector<T>();
	}

	@Override
	public void setChildQName(String mChildQName) {
		childQName = mChildQName;
	}

	@Override
	public String getChildQName() {
		return childQName;
	}

	@Override
	public ASTIterator<T> iterator() {
		return new ASTIterator<T>(this);
	}

	@Override
	public int numChildren() {
		return nodes.size();
	}

	@Override
	public String nodeType() {
		return "Sequence";
	}

	@Override
	public ASTNodeIF child(int i) {
		if (i >= 0 && i < nodes.size()) {
			return nodes.elementAt(i);
		}
		throw new NoSuchElementException("Node " + id()
				+ " does not have a child with index " + i + ".");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setChild(int i, ASTNodeIF child) {
		if (i >= 0 && i < nodes.size()) {
			nodes.setElementAt((T) child, i);
		} else if (i == nodes.size()) {
			nodes.add((T) child);
		} else {
			throw new NoSuchElementException("Node " + id()
					+ " does not have a child with index " + i + ".");
		}		
	}

	//TODO: Make this throw NoSuchElementException if the index is out of bounds
	@Override
	public void addSequenceChild(T child) {
		nodes.add(child);
	}

	//TODO: Make this throw NoSuchElementException if the index is out of bounds
	@Override
	public T getSequenceChild(int i) {
		return nodes.elementAt(i);
	}

	//TODO: Make this throw NoSuchElementException if the index is out of bounds
	@Override
	public void setSequenceChild(int i, T child) {
		nodes.set(i,child);
	}

	@Override
	public void toXml(String prefix, PrintWriter out,AbstractSyntaxTreeIF ast) {
		super.toXml(prefix,out,ast);

		for (T node : nodes) {
			out.println(prefix+"<"+childQName+">");
			node.toXml(prefix+"  ",out,ast);
			out.println(prefix+"</"+childQName+">");
		}
	}

	@Override
	public void insertChild(int position, T child) {
		nodes.insertElementAt(child, position);
	}
}