net.sf.saxon.expr

Class Calculator

Implemented Interfaces:
Serializable

public abstract class Calculator
extends java.lang.Object
implements Serializable

This class evaluates arithmetic expressions; it acts as a helper class to the ArithmeticExpression class. There are many subclasses for the different kinds of arithmetic expression, and static methods that allow the right subclass to be selected, either at compile time or at run time.

Field Summary

static Calculator[]
ANY_ANY
Calculators used for the six operators when the static type information does not allow a more specific calculator to be chosen
static Calculator[]
DATETIME_DATETIME
Calculators used when both operands are xs:dateTime, xs:date, or xs:time
static Calculator[]
DATETIME_DURATION
Calculators used when the first operand is xs:dateTime, xs:date, or xs:time, and the second is a duration
static Calculator[]
DECIMAL_DECIMAL
static Calculator[]
DECIMAL_DOUBLE
Calculators used when the first operand is a decimal
static Calculator[]
DECIMAL_FLOAT
static Calculator[]
DECIMAL_INTEGER
static int
DIV
static Calculator[]
DOUBLE_DECIMAL
static Calculator[]
DOUBLE_DOUBLE
Calculators used when the first operand is a double
static Calculator[]
DOUBLE_FLOAT
static Calculator[]
DOUBLE_INTEGER
static Calculator[]
DURATION_DATETIME
Calculators used when the second operand is xs:dateTime, xs:date, or xs:time, and the first is a duration
static Calculator[]
DURATION_DURATION
Calculators used when the both operands are durations
static Calculator[]
DURATION_NUMERIC
Calculators used when the first operand is a duration and the second is numeric
static Calculator[]
FLOAT_DECIMAL
static Calculator[]
FLOAT_DOUBLE
Calculators used when the first operand is a float
static Calculator[]
FLOAT_FLOAT
static Calculator[]
FLOAT_INTEGER
static int
IDIV
static Calculator[]
INTEGER_DECIMAL
static Calculator[]
INTEGER_DOUBLE
Calculators used when the first operand is an integer
static Calculator[]
INTEGER_FLOAT
static Calculator[]
INTEGER_INTEGER
static int
MINUS
static int
MOD
static Calculator[]
NUMERIC_DURATION
Calculators used when the second operand is a duration and the first is numeric
static int
PLUS
static int
TIMES

Method Summary

abstract AtomicValue
compute(AtomicValue a, AtomicValue b, XPathContext c)
Perform an arithmetic operation
static Calculator
getCalculator(int typeA, int typeB, int operator, boolean mustResolve)
Factory method to get a calculator for a given combination of types
static String
getCalculatorSetName(int typeA, int typeB)
Get the name of the calculator set for a given combination of types
abstract AtomicType
getResultType(AtomicType typeA, AtomicType typeB)
Get the type of the result of the calculator, given arguments types typeA and typeB

Field Details

ANY_ANY

public static Calculator[] ANY_ANY
Calculators used for the six operators when the static type information does not allow a more specific calculator to be chosen

DATETIME_DATETIME

public static Calculator[] DATETIME_DATETIME
Calculators used when both operands are xs:dateTime, xs:date, or xs:time

DATETIME_DURATION

public static Calculator[] DATETIME_DURATION
Calculators used when the first operand is xs:dateTime, xs:date, or xs:time, and the second is a duration

DECIMAL_DECIMAL

public static Calculator[] DECIMAL_DECIMAL

DECIMAL_DOUBLE

public static Calculator[] DECIMAL_DOUBLE
Calculators used when the first operand is a decimal

DECIMAL_FLOAT

public static Calculator[] DECIMAL_FLOAT

DECIMAL_INTEGER

public static Calculator[] DECIMAL_INTEGER

DIV

public static final int DIV
Field Value:
3

DOUBLE_DECIMAL

public static Calculator[] DOUBLE_DECIMAL

DOUBLE_DOUBLE

public static Calculator[] DOUBLE_DOUBLE
Calculators used when the first operand is a double

DOUBLE_FLOAT

public static Calculator[] DOUBLE_FLOAT

DOUBLE_INTEGER

public static Calculator[] DOUBLE_INTEGER

DURATION_DATETIME

public static Calculator[] DURATION_DATETIME
Calculators used when the second operand is xs:dateTime, xs:date, or xs:time, and the first is a duration

DURATION_DURATION

public static Calculator[] DURATION_DURATION
Calculators used when the both operands are durations

DURATION_NUMERIC

public static Calculator[] DURATION_NUMERIC
Calculators used when the first operand is a duration and the second is numeric

FLOAT_DECIMAL

public static Calculator[] FLOAT_DECIMAL

FLOAT_DOUBLE

public static Calculator[] FLOAT_DOUBLE
Calculators used when the first operand is a float

FLOAT_FLOAT

public static Calculator[] FLOAT_FLOAT

FLOAT_INTEGER

public static Calculator[] FLOAT_INTEGER

IDIV

public static final int IDIV
Field Value:
5

INTEGER_DECIMAL

public static Calculator[] INTEGER_DECIMAL

INTEGER_DOUBLE

public static Calculator[] INTEGER_DOUBLE
Calculators used when the first operand is an integer

INTEGER_FLOAT

public static Calculator[] INTEGER_FLOAT

INTEGER_INTEGER

public static Calculator[] INTEGER_INTEGER

MINUS

public static final int MINUS
Field Value:
1

MOD

public static final int MOD
Field Value:
4

NUMERIC_DURATION

public static Calculator[] NUMERIC_DURATION
Calculators used when the second operand is a duration and the first is numeric

PLUS

public static final int PLUS
Field Value:
0

TIMES

public static final int TIMES
Field Value:
2

Method Details

compute

public abstract AtomicValue compute(AtomicValue a,
                                    AtomicValue b,
                                    XPathContext c)
            throws XPathException
Perform an arithmetic operation
Parameters:
a - the first operand. Must not be null, and must be an instance of the type implied by the class name.
b - the second operand. Must not be null, and must be an instance of the type implied by the class name.
c - the XPath dynamic evaluation context
Returns:
the result of the computation, as a value of the correct primitive type
Throws:
XPathException - in the event of an arithmetic error

getCalculator

public static Calculator getCalculator(int typeA,
                                       int typeB,
                                       int operator,
                                       boolean mustResolve)
Factory method to get a calculator for a given combination of types
Parameters:
typeA - fingerprint of the primitive type of the first operand
typeB - fingerprint of the primitive type of the second operand
operator - the arithmetic operator in use
mustResolve - indicates that a concrete Calculator is required (rather than an ANY_ANY calculator which needs to be further resolved at run-time)
Returns:
null if no suitable Calculator can be found.

getCalculatorSetName

public static String getCalculatorSetName(int typeA,
                                          int typeB)
Get the name of the calculator set for a given combination of types
Parameters:
typeA - the fingerprint of the primitive type of the first operand
typeB - the fingerprint of the primitive type of the second operand
Returns:
null if no suitable Calculator can be found.

getResultType

public abstract AtomicType getResultType(AtomicType typeA,
                                         AtomicType typeB)
Get the type of the result of the calculator, given arguments types typeA and typeB
Parameters:
typeA - the type of the first operand
typeB - the type of the second operand
Returns:
the type of the result