/*
 * FSRegisterVariable.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

/**
 * FSRegisterVariable is a lightweight class used to identify the register that
 * an argument defined in The FSNewFunction2 class is mapped into when executing
 * the function.
 * 
 * <p>
 * Register numbers up to 255 may be specified. If the number is zero then the
 * argument is defined as a local variable.
 * </p>
 */
public class FSRegisterVariable extends FSTransformObject
{
	private int index = 0;

	private String name = null;

	/**
	 * Construct an FSRegisterVariable object, initialising it with values
	 * decoded from an encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSRegisterVariable(FSCoder coder)
	{
		decode(coder);
	}

	/**
	 * Constructs a new FSRegisterVariable object specifying the name of the
	 * argument and the number of the register it will be mapped to. If the
	 * index is 0 then the argument will be stored in a local variable.
	 * 
	 * @param index
	 *            the number of the register, 1..255, 0 if the argument will be
	 *            stored in a local variable.
	 * @param name
	 *            the name of the argument.
	 */
	public FSRegisterVariable(int index, String name)
	{
		this.index = index;
		this.name = name;
	}

	/**
	 * Gets the number of the register that will contain the function argument.
	 * 
	 * @return the number of the register, 1..255, in which the function
	 *         argument will be stored, 0 if the argument will be stored in a
	 *         local variable.
	 */
	public int getIndex()
	{
		return index;
	}

	/**
	 * Constructs an FSRegisterVariable object by copying values from an
	 * existing object.
	 * 
	 * @param obj
	 *            an FSRegisterVariable object.
	 */
	public FSRegisterVariable(FSRegisterVariable obj)
	{
		index = obj.index;
		name = new String(obj.name);
	}

	/**
	 * Gets the name of the function argument.
	 * 
	 * @return the name of the argument.
	 */
	public String getName()
	{
		return name;
	}

	/**
	 * Sets the number of the register that will contain the function argument.
	 * 
	 * @param index
	 *            the number of the register, 1..255, in which the function
	 *            argument will be stored, 0 if the argument will be stored in a
	 *            local variable.
	 */
	public void setIndex(int index)
	{
		this.index = index;
	}

	/**
	 * Sets the name of the function argument.
	 * 
	 * @param name
	 *            the name of the argument.
	 */
	public void setName(String name)
	{
		this.name = name;
	}

	/**
	 * Returns true if anObject is equal to this one. Objects are considered
	 * equal if they would generate identical binary data when they are encoded
	 * to a Flash file.
	 * 
	 * @return true if this object would be identical to anObject when encoded.
	 */
	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSRegisterVariable typedObject = (FSRegisterVariable) anObject;

			result = index == typedObject.index;
			result = result && name.equals(typedObject.name);
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "index", index);
			Transform.append(buffer, "name", name);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		int length = 1;

		length += coder.strlen(name, true);

		return length;
	}

	public void encode(FSCoder coder)
	{
		coder.writeWord(index, 1);
		coder.writeString(name);
		coder.writeWord(0, 1);
	}

	public void decode(FSCoder coder)
	{
		index = coder.readWord(1, false);
		name = coder.readString();
	}
}