package prolog.implementation;

import prolog.model.*;

/**
 * <p>This class encapsulates a variable value used in queries.
 * Because all names in the coder's program are replaced by integers,
 * the internal representation of a atom is a integer value. This
 * integer value can be decoded using the symboltable generated
 * by the parser. A value can be compared with a filled cell in a
 * database table row</p>
 * <p>prolog: myFact(dan,X).</p>
 * <p>in this example, a atom would be "X" a variable</p>
 */
public class Variable
	implements IAtom
{
	/**
	 * the interpreter internal representation of the variable.
	 * this is the index for the symboltable.
	 */
	protected int name = -1;

	/**
	 * creates a new Variable
	 * @param name is the name ;-)
	 */
	public Variable( int name )
	{
		this.name = name;
	}

	/**
	 * the key in symboltable of the name
	 */
	public int getName()
	{
		return this.name;
	}

	/**
	 * !!! a variable is equal to anything !!!
	 * @return allways true !!!
	 */
	public boolean equals( Object o )
	{
		return true;
	}

	/**
	 * returns a string representation of the atom.
	 */
	public String toString()
	{
		return toString( null );
	}

	/**
	 * returns a string representation of the atom. because all names
	 * of the programmer's programm are replaced by integers, this
	 * function requires the additional stringtable to decode the
	 * integers into strings again.
	 */
	public String toString( ISymbolTable table )
	{
		if( table != null )
			return table.decode( name ) + "(V)";
		else
			return Integer.toString( name ) + "(V)";
	}
}
