package prolog.implementation;

import prolog.model.*;

/**
 * <p>This class encapsulates a constant value, one item of a relation.
 * 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 "dan" a value</p>
 */
public class Value
	implements IAtom
{
	/**
	 * the interpreter internal representation of the constant.
	 * this is the index for the symboltable
	 */
	protected int value = -1;

	/**
	 * creates a new Value
	 * @param value is the name used by the programer
	 */
	public Value( int value )
	{
		this.value = value;
	}

	/**
	 * overrides equals in java.lang.Object.
	 * if the given Object o is of the same class, the compare
	 * action is done, if not, it returns false
	 * @return true if the two values are the same in symboltable
	 */
	public boolean equals( Object o )
	{
		if( o instanceof Value )
			return ((Value)o).getName() == this.value;
		else
			return false;
	}

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

	/**
	 * 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( value );
		else
			return Integer.toString( value );
	}

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

	/**
	 * returns a hashcode ;-) WOW ;-)
	 */
	public int hashCode()
	{
		return value;
	}
}
