ArrayType.java

package edu.udel.cis.vsl.tass.model.impl.type;

import java.util.Set;

import edu.udel.cis.vsl.tass.model.IF.type.ArrayTypeIF;
import edu.udel.cis.vsl.tass.model.IF.type.TypeIF;

/**
 * An array type is a type representing an array where the dimensions are not
 * specified, but the number of dimensions is. Examples are "int[][][]" and
 * "rational[]". It may be represented as an ordered pair consisting of a
 * primitive type and a positive integer.
 * <p>
 * The base type of the array is the primitive type.
 * <p>
 * The element type of the array is what results from removing one of the "[]"
 * symbols, or in the ordered pair representation by decrementing the integer or
 * returning the primitive type if the integer is 1.
 * <p>
 * The dimension of the array is really the number of dimensions. I.e., the
 * dimension of "int[][][]" is 3.
 * 
 * @author siegel
 * 
 */
public class ArrayType extends Type implements ArrayTypeIF {

	private TypeIF elementType;

	private TypeIF baseType;

	private int dimension;

	private int hashCode;

	public ArrayType(TypeIF elementType) {
		super(TypeKind.ARRAY);
		if (elementType == null)
			throw new NullPointerException("null element type");
		this.elementType = elementType;
		if (elementType.kind() != TypeKind.ARRAY) {
			baseType = elementType;
			dimension = 1;
		} else {
			ArrayType arrayElementType = (ArrayType) elementType;

			baseType = arrayElementType.baseType;
			dimension = arrayElementType.dimension + 1;
		}
		hashCode = elementType.hashCode() * 2 + 1000;
	}

	public TypeIF baseType() {
		return baseType;
	}

	public int dimension() {
		return dimension;
	}

	public TypeIF elementType() {
		return elementType;
	}

	public boolean equals(Object object) {
		if (object instanceof ArrayType) {
			ArrayType that = (ArrayType) object;

			return elementType.equals(that.elementType);
		}
		return false;
	}

	public int hashCode() {
		return hashCode;
	}

	public boolean isNumeric() {
		return false;
	}

	public boolean isSubtypeOf(TypeIF type) {
		return equals(type);
	}

	@Override
	public String longName(Set<Type> stack) {
		return shortName();
	}

	public String shortName() {
		return elementType + "[" + "]";
	}

}