hussam.math.operations
Class Operand

java.lang.Object
extended by hussam.math.operations.Operand
All Implemented Interfaces:
Operation, Operator, java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<Operator>
Direct Known Subclasses:
OperatorFactoryDB.CommaOperand

public abstract class Operand
extends java.lang.Object
implements Operator

An Operand is an Operator that takes at most two Operations as arguments. Operands are like, + - ^ % > <= etc...

For Example:
 5 + 8
 8, and 5 are ConstantNumbers,
 + is an operand.
 Operand o = new PlusOperand(Operator.PLUS, new ConstantNumber(5), new ConstantNumber(8));
 System.out.println(o.result());

Author:
Hussam Almulhim حسام الملحم
See Also:
Operator, OperatorFactoryDB, Function, Serialized Form

Field Summary
 
Fields inherited from interface hussam.math.operations.Operator
COMMA, FUNCTION, INDEFINITE, LOGICAL, MULTIPLY, NOTHING, PLUS, POWER
 
Constructor Summary
Operand(int type, java.lang.String name)
          Construcs an Operand by giving it a name, and a power.
Operand(int type, java.lang.String name, Operation previous, Operation next)
          Construcs an Operation Operand by a Power type, and two operations.
 
Method Summary
protected  voidcheckReady()
          Tests if this operand has two arguments, a previous argument and a next argument.
 Operandclone(OperatorSourceImpl s)
          This method clones the operation.
 intcompareTo(Operator p)
          Compares this operand with another Operator.
 java.lang.StringgetName()
          Returns the name of this Operator.
 OperationgetNext()
          returns the next operation.
 OperationgetPrevious()
          the previous operation.
 intgetType()
           
 Operationoptimize()
          Optimizes this operand.
 voidset(Operation previous, Operation next)
           Sets the previos and next operation. 5 + 8 5 is the previous operation. 8 is the next operation.
 voidsetNext(Operation operation)
           Sets the next operation. 5 + 8 5 is the previous operation. 8 is the next operation.
 voidsetPrevious(Operation operation)
           Sets the previos operation. 5 + 8 5 is the previous operation. 8 is the next operation.
 java.lang.StringtoString()
          returns a description of this operand.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface hussam.math.operations.Operation
result
 

Constructor Detail

Operand

public Operand(int type,
               java.lang.String name,
Operation previous,
Operation next)
Construcs an Operation Operand by a Power type, and two operations.

Parameters:
type - The power of this Operand. for example: Operator.POWER
name - the name of this operand
previous - the previous operation
next - the next operations.

Operand

public Operand(int type,
               java.lang.String name)
Construcs an Operand by giving it a name, and a power. You can set the operations later.

Parameters:
type - The power of the operand. for example: Operator.PLUS
name - the name of the operand.
Method Detail

getType

public int getType()
Specified by:
getType in interface Operator

getName

public java.lang.String getName()
Description copied from interface: Operator
Returns the name of this Operator.

Specified by:
getName in interface Operator
Returns:
the name of this operator.

setPrevious

public void setPrevious(Operation operation)
Sets the previos operation.
 5 + 8
 5 is the previous operation.
 8 is the next operation.

Parameters:
operation - the operation to be set.

setNext

public void setNext(Operation operation)
Sets the next operation.
 5 + 8
 5 is the previous operation.
 8 is the next operation.

Parameters:
operation - the operation to be set.

set

public void set(Operation previous,
Operation next)
Sets the previos and next operation.
 5 + 8
 5 is the previous operation.
 8 is the next operation.

Parameters:
previous - the previous operation.
next - the next operation.

getPrevious

public Operation getPrevious()
the previous operation.

Returns:
the previous operation.

getNext

public Operation getNext()
returns the next operation.

Returns:
the next operation.

toString

public java.lang.String toString()
returns a description of this operand.

Overrides:
toString in class java.lang.Object
Returns:
a description of this Operand

compareTo

public int compareTo(Operator p)
Compares this operand with another Operator. The comparison is based on the Type of the operator.

Specified by:
compareTo in interface java.lang.Comparable<Operator>
Parameters:
p - the Operaotor.
Returns:
a signal indicating the comparison between the two. 1, -1, 0 indicating larger, smaller, equal.

checkReady

protected void checkReady()
                   throws OperatorArgumentMissingException
Tests if this operand has two arguments, a previous argument and a next argument.

Throws:
OperatorArgumentMissingException - Thrown if this Operand has one argument missing.

clone

public Operand clone(OperatorSourceImpl s)
              throws java.lang.CloneNotSupportedException
Description copied from interface: Operation
This method clones the operation. Cloning can be achieved by useing the default clone method since Operations are Clonable since Version 3! However, cloning sometimes needs more than what the clone method offers! Notice that this clone method takes an OperatorSource as a parameter, this method should update this source with any arguments needed such as Variables! It can be seen as an implemetation of the Visitor approach. It is also worth to note that this method's implemetation is optional. You can egnore the implemetation of this method and just return
this
.

Specified by:
clone in interface Operation
Parameters:
s - The OperatorSourceImpl.
Returns:
Returns the cloned operation.
Throws:
java.lang.CloneNotSupportedException - If the operation is not clonable.
See Also:
ConstantNumber.clone(hussam.math.operations.dataBase.OperatorSourceImpl), Variable.clone(hussam.math.operations.dataBase.OperatorSourceImpl)

optimize

public Operation optimize()
Optimizes this operand. If the next and previous operations are ConstantNumbers or Constants, then this will return a new ConstantNumber having the same result of this operand operation.

Specified by:
optimize in interface Operation
Returns:
an optimized Operation of this operation.
See Also:
Function.optimize()