org.josql.expressions
Class ArithmeticExpression

java.lang.Object
  extended by org.josql.expressions.Expression
      extended by org.josql.expressions.ValueExpression
          extended by org.josql.expressions.ArithmeticExpression

public class ArithmeticExpression
extends ValueExpression

Represents the arithmetic expressions: *, +, /, - and %. It should be noted that ALL numbers in JoSQL are represented as double values, this allows for easy arithmetic operations without the fear of losing precision or casting issues.


Field Summary
static int ADDITION
           
static int DIVIDE
           
static int MODULUS
           
static int MULTIPLY
           
static int SUBTRACT
           
 
Constructor Summary
ArithmeticExpression()
           
 
Method Summary
 Object evaluate(Object o, Query q)
          Evaulate this expression.
 Class getExpectedReturnType(Query q)
          Return the expected return type.
 ValueExpression getLeft()
          Get the LHS value expression.
 ValueExpression getRight()
          Get the RHS value expression.
 int getType()
           
 boolean hasFixedResult(Query q)
          Return whether this expression has a fixed result.
 void init(Query q)
          Perform the necessary initialisation for this expression.
 boolean isTrue(Object o, Query q)
          Determine whether this arithmetic expression evaluates to true.
 void setLeft(ValueExpression exp)
           
 void setRight(ValueExpression exp)
           
 void setType(int t)
           
 String toString()
          Return a string representation of the expression, making this abstract forces sub-classes to provide an implementation.
 
Methods inherited from class org.josql.expressions.ValueExpression
getValue
 
Methods inherited from class org.josql.expressions.Expression
isBracketed, setBracketed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MULTIPLY

public static final int MULTIPLY
See Also:
Constant Field Values

ADDITION

public static final int ADDITION
See Also:
Constant Field Values

SUBTRACT

public static final int SUBTRACT
See Also:
Constant Field Values

DIVIDE

public static final int DIVIDE
See Also:
Constant Field Values

MODULUS

public static final int MODULUS
See Also:
Constant Field Values
Constructor Detail

ArithmeticExpression

public ArithmeticExpression()
Method Detail

getExpectedReturnType

public Class getExpectedReturnType(Query q)
                            throws QueryParseException
Return the expected return type. This just returns the result of calling this method on the LHS.

Specified by:
getExpectedReturnType in class Expression
Parameters:
q - The Query object.
Returns:
The expected return type class.
Throws:
QueryParseException - If something goes wrong in determining the return type.

isTrue

public boolean isTrue(Object o,
                      Query q)
               throws QueryExecutionException
Determine whether this arithmetic expression evaluates to true. Whilst this seems a little bizarre this method is needed to allow artithmetic expressions to be used in the columns part of the SELECT.

Thus the following could be performed:

   SELECT 10 + 20
   FROM   java.lang.Object
 
The rules are as follows:

Specified by:
isTrue in class Expression
Parameters:
o - The object to perform the expression on.
q - The Query object.
Returns:
As according to the rules above.
Throws:
QueryExecutionException - If something goes wrong during the evaluation of the expression.

hasFixedResult

public boolean hasFixedResult(Query q)
Return whether this expression has a fixed result.

Specified by:
hasFixedResult in class Expression
Parameters:
q - The Query object.
Returns:
LHS.hasFixedResult(Query) && RHS.hasFixedResult(Query)

init

public void init(Query q)
          throws QueryParseException
Description copied from class: Expression
Perform the necessary initialisation for this expression. The exact operations performed are defined by the sub-class.

Specified by:
init in class Expression
Parameters:
q - The Query object.
Throws:
QueryParseException - If something goes wrong with the initialisation.

getRight

public ValueExpression getRight()
Get the RHS value expression.

Returns:
The RHS.

getLeft

public ValueExpression getLeft()
Get the LHS value expression.

Returns:
The LHS.

setLeft

public void setLeft(ValueExpression exp)

setRight

public void setRight(ValueExpression exp)

getType

public int getType()

setType

public void setType(int t)

evaluate

public Object evaluate(Object o,
                       Query q)
                throws QueryExecutionException
Evaulate this expression. Apart from the special cases the LHS and RHS must evaluate to an instance of java.lang.Number otherwise a QueryExecutionException is thrown.

Special cases:

Specified by:
evaluate in class ValueExpression
Parameters:
o - The object to perform the expression on.
q - The Query object.
Returns:
The result of the expression, see the "special cases" for the exceptions to what would be the intuitive result.
Throws:
QueryExecutionException - If an error occurs during processing.

toString

public String toString()
Description copied from class: Expression
Return a string representation of the expression, making this abstract forces sub-classes to provide an implementation.

Specified by:
toString in class Expression
Returns:
A string representation of the expression.


  Copyright © 2008 Gary Bentley. All Rights Reserved.