package org.dgpf.gp.regression.expressions.binary;
import org.dgpf.gp.regression.base.Calculator;
import org.dgpf.gp.regression.base.Expression;
import org.dgpf.gp.regression.base.IExpressionHandler;
import org.dgpf.gp.regression.expressions.Costs;
/**
* The add instruction.
*
* @author Thomas Weise
*/
public final class Add extends BinaryExpression
{
/**
* The serial version uid.
*/
private static final long serialVersionUID = 1;
/**
* Create an addition.
* @param p_sub_1 The first sub-expression.
* @param p_sub_2 The second sub-expression.
*/
public Add (final Expression p_sub_1,
final Expression p_sub_2)
{
super(p_sub_1, p_sub_2);
}
/**
* Compute the expression value.
* @param p_calculator The calculator this expression should be evaluated
* on.
* @return The computed expression value.
*/
@Override
public final double compute (final Calculator p_calculator)
{
return (this.m_sub_1.compute(p_calculator) +
this.m_sub_2.compute(p_calculator));
}
/**
* Obtain the expression handler used to handle this expression.
* @return The expression handler used to handle this expression.
*/
@Override
public final IExpressionHandler get_handler()
{
return AddHandler.INSTANCE;
}
/**
* Store this objects content into a string builder.
* @param p_sb The string builder to store stuff into.
*/
@Override
public final void to_string (final StringBuilder p_sb)
{
p_sb.append('(');
this.m_sub_1.to_string(p_sb);
p_sb.append('+');
this.m_sub_2.to_string(p_sb);
p_sb.append(')');
}
/**
* Check if another object is equal to this one.
* @param p_o The object to check.
* @return <code>true</code> if and only if <code>p_o</code> is exactly
* the same object than this one.
*/
@Override
public final boolean equals (final Object p_o)
{
Add l_be;
if(p_o == this) return true;
if(p_o instanceof Add)
{
l_be = ((Add)p_o);
return ( ( l_be.m_sub_1.equals(this.m_sub_1) &&
l_be.m_sub_2.equals(this.m_sub_2) ) ||
( l_be.m_sub_2.equals(this.m_sub_1) &&
l_be.m_sub_1.equals(this.m_sub_2) ) );
}
return false;
}
/**
* Returns the costs that will arise when executing this expression.
* This is defined as the costs of exactly this expression plus the costs
* of all sub-expressions.
* @return The costs of executing this expression.
*/
@Override
public double get_costs ()
{
return (super.get_costs() + Costs.ADD_COSTS);
}
}