package edu.uprm.admg.nettraveler.schema;

// JDK imports
import edu.uprm.admg.nettraveler.type.MDObject;
import edu.uprm.admg.nettraveler.type.MDSmallObject;
import edu.uprm.admg.util.DataVerify;

/**
 * <code>ValueAttribute</code> represents a constant attribute being manipulated 
 * by the execution engine.
 * <br>
 * Universidad de Puerto Rico@Mayaguez.
 * <br>
 * <code>NetTraveler</code>
 */
 /*
 * @author Manuel Rodriguez-Martinez
 * @author Elliot A. Vargas-Figueroa - M.S. Thesis Project.
 * P.I. Manuel Rodriguez-Martinez
 */
public class ValueAttribute extends Attribute {
    /**
	 * Serial Version
	 */
	private static final long serialVersionUID = 2719224603963301362L;
    /**
     * Constant value.
     */
    private MDSmallObject consValue = null;
    /**
     * Constructs a <code>ValueAttribute</code> from: an attribute name,
     * and a class implementing the base type of this object.
     * <code>CAUTION</code>: This constructor cannot be used for constants,
     * because a value is needed. For constants use the second constructor.
     * 
     * @param name the name of the attribute
     * @param className Name of the implementing class.
     */
    protected ValueAttribute(String name, String className) { 
    		super(name, className);
    }
    /**
     * Constructs a <code>ValueAttribute</code> from: an attribute name,
     * and a constant value.
     * <p/>
     * This constructor is used for constants.
     *  Only numbers (integers and floats), booleans  and
     * strings are supported.
     * 
     * @param name The name of the attribute.
     * @param consValue The value of the constant.
     */
    public ValueAttribute(String name, MDSmallObject consValue){
	    	super(name, consValue.getClassName());
	    	DataVerify.VerifyObjectParam("consValue", consValue);
	    	this.consValue = consValue;
    }
    /**
     * Returns the constant value of this attribute.
     * 
     * @return Value of this attribute. 
     */
    public MDObject getValue(){
	    		return this.consValue;
    }
    /**
     * @see edu.uprm.admg.nettraveler.schema.Attribute#localEquals(edu.uprm.admg.nettraveler.schema.Attribute)
     */
	protected boolean localEquals(Attribute attr) {
		if(!(attr instanceof ValueAttribute))
			return false;
		ValueAttribute o = (ValueAttribute) attr;
		return consValue.equals(o.consValue);
	}
	/**
	 * @see edu.uprm.admg.nettraveler.schema.Attribute#localHashCode()
	 */
	protected int localHashCode() {
		int result = 17;
		if(consValue != null)
			result += result * 37 + consValue.hashCode();
		return result;
	}
}
