KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.expr.*;
4
5 import java.util.*;
6 import java.lang.Math JavaDoc;
7 import java.text.*;
8
9
10
11 public class Round extends Function {
12
13
14     /**
15     * Function name (for diagnostics)
16     */

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

26
27     public int getDataType() {
28         return Value.NUMBER;
29     }
30
31     /**
32     * Simplify and validate.
33     * This is a pure function so it can be simplified in advance if the arguments are known
34     */

35
36     public Expression simplify() throws XPathException {
37         checkArgumentCount(1, 1);
38         argument[0] = argument[0].simplify();
39         if (argument[0] instanceof Value) {
40             return evaluate(null);
41         }
42         return this;
43     }
44
45     /**
46     * Evaluate the function in a numeric context
47     */

48
49     public double evaluateAsNumber(Context c) throws XPathException {
50         return round(argument[0].evaluateAsNumber(c));
51     }
52
53     /**
54     * Evaluate in a general context
55     */

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

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

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

83     
84     public static double round(double arg0) {
85         if (Double.isNaN(arg0)) return arg0;
86         if (Double.isInfinite(arg0)) return arg0;
87         if (arg0==0.0) return arg0; // handles the negative zero case
88
if (arg0 > -0.5 && arg0 < 0.0) return -0.0;
89         if (arg0 > Long.MIN_VALUE && arg0 < Long.MAX_VALUE) {
90             return Math.round(arg0);
91         }
92         double fraction = arg0 % 1.0;
93         if (fraction < 0.5) return arg0 - fraction;
94         return arg0 - fraction + 1.0;
95         
96     }
97     
98 }
99
100
101 //
102
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
103
// you may not use this file except in compliance with the License. You may obtain a copy of the
104
// License at http://www.mozilla.org/MPL/
105
//
106
// Software distributed under the License is distributed on an "AS IS" basis,
107
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
108
// See the License for the specific language governing rights and limitations under the License.
109
//
110
// The Original Code is: all this file.
111
//
112
// The Initial Developer of the Original Code is
113
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
114
//
115
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
116
//
117
// Contributor(s): none.
118
//
119
Popular Tags