KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > ArithmeticExpression


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3
4
5 /**
6 * Arithmetic Expression: a numeric expression consisting of the sum, difference,
7 * product, quotient, or modulus of two numeric expressions
8 */

9
10 class ArithmeticExpression extends BinaryExpression {
11
12     public ArithmeticExpression(){};
13
14     public ArithmeticExpression(Expression p1, int operator, Expression p2) {
15         super(p1, operator, p2);
16     }
17
18     /**
19     * Evaluate an expression.
20     * @param context The context in which the expression is to be evaluated
21     * @return the value of the expression as a Value object, evaluated in the current context
22     */

23
24     public Value evaluate(Context c) throws XPathException {
25         return new NumericValue(evaluateAsNumber(c));
26     }
27
28     /**
29     * Evaluate an expression as a Number.
30     * @param context The context in which the expression is to be evaluated
31     * @return the value of the expression as a double, evaluated in the current context
32     */

33
34     public double evaluateAsNumber(Context c) throws XPathException {
35         switch(operator) {
36             case Tokenizer.PLUS:
37                 return p1.evaluateAsNumber(c) + p2.evaluateAsNumber(c);
38             case Tokenizer.MINUS:
39                 return p1.evaluateAsNumber(c) - p2.evaluateAsNumber(c);
40             case Tokenizer.MULT:
41                 return p1.evaluateAsNumber(c) * p2.evaluateAsNumber(c);
42             case Tokenizer.DIV:
43                 return p1.evaluateAsNumber(c) / p2.evaluateAsNumber(c);
44             case Tokenizer.MOD:
45                 return p1.evaluateAsNumber(c) % p2.evaluateAsNumber(c);
46             case Tokenizer.NEGATE:
47                 return -p2.evaluateAsNumber(c);
48
49             default:
50                 throw new XPathException("Unknown operator in arithmetic expression");
51         }
52     }
53
54     /**
55     * Determine the data type of the expression, if possible
56     * @return Value.NUMBER
57     */

58
59     public int getDataType() {
60         return Value.NUMBER;
61     }
62
63     /**
64     * Perform a partial evaluation of the expression, by eliminating specified dependencies
65     * on the context.
66     * @param dependencies The dependencies to be removed
67     * @param context The context to be used for the partial evaluation
68     * @return a new expression that does not have any of the specified
69     * dependencies
70     */

71
72     public Expression reduce(int dependencies, Context context) throws XPathException {
73         if ((getDependencies() & dependencies) != 0 ) {
74             Expression e = new ArithmeticExpression(
75                                 p1.reduce(dependencies, context),
76                                 operator,
77                                 p2.reduce(dependencies, context));
78             e.setStaticContext(getStaticContext());
79             return e.simplify();
80         } else {
81             return this;
82         }
83     }
84
85 }
86
87 //
88
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
89
// you may not use this file except in compliance with the License. You may obtain a copy of the
90
// License at http://www.mozilla.org/MPL/
91
//
92
// Software distributed under the License is distributed on an "AS IS" basis,
93
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
94
// See the License for the specific language governing rights and limitations under the License.
95
//
96
// The Original Code is: all this file.
97
//
98
// The Initial Developer of the Original Code is
99
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
100
//
101
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
102
//
103
// Contributor(s): none.
104
//
105
Popular Tags