KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > functions > Sum


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.NodeInfo;
4 import com.icl.saxon.om.NodeEnumeration;
5 import com.icl.saxon.expr.*;
6
7 import java.util.*;
8
9
10 public class Sum extends Function {
11
12     /**
13     * Function name (for diagnostics)
14     */

15
16     public String getName() {
17         return "sum";
18     };
19
20     /**
21     * Determine the data type of the expression
22     * @return Value.NUMBER
23     */

24
25     public int getDataType() {
26         return Value.NUMBER;
27     }
28
29     /**
30     * Simplify and validate.
31     */

32
33     public Expression simplify() throws XPathException {
34         checkArgumentCount(1, 1);
35         argument[0] = argument[0].simplify();
36
37         if (argument[0] instanceof Value) { // can't happen at present?
38
return evaluate(null);
39         }
40         return this;
41     }
42
43     /**
44     * Evaluate the function in a numeric context
45     */

46
47     public double evaluateAsNumber(Context c) throws XPathException {
48         NodeEnumeration nsv = argument[0].enumerate(c, false);
49         return total(nsv);
50     }
51
52     /**
53     * Evaluate in a general context
54     */

55
56     public Value evaluate(Context c) throws XPathException {
57         return new NumericValue(evaluateAsNumber(c));
58     }
59
60     /**
61     * Determine the dependencies
62     */

63
64     public int getDependencies() {
65         return argument[0].getDependencies();
66     }
67
68     /**
69     * Reduce the dependencies
70     */

71
72     public Expression reduce(int dep, Context c) throws XPathException {
73         Sum f = new Sum();
74         f.addArgument(argument[0].reduce(dep, c));
75         f.setStaticContext(getStaticContext());
76         return f.simplify();
77     }
78
79     /**
80     * Here is the algorithm
81     */

82
83     private static double total(NodeEnumeration enum) throws XPathException {
84         
85         double sum = 0.0;
86         while (enum.hasMoreElements()) {
87             String val = enum.nextElement().getStringValue();
88             sum += Value.stringToNumber(val);
89         }
90         return sum;
91     }
92
93 }
94
95
96 //
97
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
98
// you may not use this file except in compliance with the License. You may obtain a copy of the
99
// License at http://www.mozilla.org/MPL/
100
//
101
// Software distributed under the License is distributed on an "AS IS" basis,
102
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
103
// See the License for the specific language governing rights and limitations under the License.
104
//
105
// The Original Code is: all this file.
106
//
107
// The Initial Developer of the Original Code is
108
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
109
//
110
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
111
//
112
// Contributor(s): none.
113
//
114
Popular Tags