ASTStructType.java

package edu.udel.cis.vsl.tass.front.minimp.ast.type;

// TODO: no hashCode method here!!!   This class is mutable.

import java.util.List;
import java.util.Vector;

import edu.udel.cis.vsl.tass.model.IF.SyntaxException;

public class ASTStructType extends ASTType implements ASTTypeIF {
	private String name = "";
	private List<String> fieldNames = null;
	private List<ASTTypeIF> fieldTypes = null;

	public ASTStructType(String name, List<String> fieldNames,
			List<ASTTypeIF> fieldTypes) {
		this.fieldNames = fieldNames;
		this.fieldTypes = fieldTypes;
		this.name = name;
	}

	public ASTStructType(List<String> fieldNames, List<ASTTypeIF> fieldTypes) {
		this("", fieldNames, fieldTypes);
	}

	public ASTStructType(String name) {
		this(name, new Vector<String>(), new Vector<ASTTypeIF>());
	}

	public ASTStructType() {
		this("", new Vector<String>(), new Vector<ASTTypeIF>());
	}

	public String getName() {
		return this.name;
	}

	public ASTTypeIF getFieldType(String name) {
		return this.fieldTypes.get(this.fieldNames.indexOf(name));
	}

	public ASTTypeIF getFieldType(int index) {
		return this.fieldTypes.get(index);
	}

	public int getFieldIndex(String name) {
		return this.fieldNames.indexOf(name);
	}

	public String getFieldName(int index) {
		return this.fieldNames.get(index);
	}

	public ASTTypeIF addFieldType(String name, ASTTypeIF type)
			throws SyntaxException {
		if (this.fieldNames.contains(name)) {
			throw new SyntaxException(getSource(), "Duplicate field name "
					+ name);
		}
		this.fieldNames.add(name);
		this.fieldTypes.add(type);
		return this;
	}

	public boolean hasField(String name) {
		return this.fieldNames.contains(name);
	}

	public int numFieldTypes() {
		return this.fieldTypes.size();
	}

	public String toString() {
		return "struct " + this.name;
	}

	public String toStringLong() {
		String result = "struct " + this.name + "{";
		this.hashCode = "struct".hashCode();
		for (int i = 0; i < this.fieldNames.size(); i++) {
			result += this.fieldTypes.get(i) + " " + this.fieldNames.get(i)
					+ ";\n";
			this.hashCode += this.fieldNames.get(i).hashCode()
					+ this.fieldTypes.get(i).hashCode();
		}
		result += "\n}";
		return result;
	}

	// public boolean equals(Object that) {
	// if (that instanceof StructType) {
	// StructType temp = (StructType) that;
	// if (temp.name.equals(this.name)) {
	// if (this.fieldNames.size() == temp.fieldNames.size()) {
	// for (int i = 0; i < this.fieldNames.size(); i++) {
	// if (!this.fieldNames.get(i).equals(temp.fieldNames.get(i)) ||
	// !this.fieldTypes.get(i).equals(temp.fieldTypes.get(i))) {
	// return false;
	// }
	// }
	// return true;
	// }
	// }
	// }
	// return false;
	// }

	// two struct types are equal iff they have the same name.

	public boolean equals(Object that) {
		return that instanceof ASTStructType
				&& name.equals(((ASTStructType) that).name);
	}

	public int hashCode() {
		return name.hashCode();
	}

	public boolean isAnonymous() {
		return this.name.length() == 0;
	}

	public void setName(String identifier) {
		name = identifier;
	}
}