KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > Rounding


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.Token;
3 import net.sf.saxon.expr.XPathContext;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.value.AtomicValue;
7 import net.sf.saxon.value.IntegerValue;
8 import net.sf.saxon.value.NumericValue;
9
10 /**
11 * This class supports the ceiling(), floor(), round(), and round-to-half-even() functions,
12  * and also the abs() function
13 */

14
15 public final class Rounding extends SystemFunction {
16
17     public static final int FLOOR = 0;
18     public static final int CEILING = 1;
19     public static final int ROUND = 2;
20     public static final int HALF_EVEN = 3;
21     public static final int ABS = 4;
22
23     /**
24     * Evaluate the function
25     */

26
27     public Item evaluateItem(XPathContext context) throws XPathException {
28
29         AtomicValue val0 = (AtomicValue)argument[0].evaluateItem(context);
30         if (val0==null) return null;
31         NumericValue val = (NumericValue)val0.getPrimitiveValue();
32
33         switch (operation) {
34             case FLOOR:
35                 return val.floor();
36             case CEILING:
37                 return val.ceiling();
38             case ROUND:
39                 return val.round();
40             case HALF_EVEN:
41                 int scale = 0;
42                 if (argument.length==2) {
43                     AtomicValue scaleVal0 = (AtomicValue)argument[1].evaluateItem(context);
44                     NumericValue scaleVal = (NumericValue)scaleVal0.getPrimitiveValue();
45                     scale = (int)scaleVal.longValue();
46                 }
47                 return val.roundToHalfEven(scale);
48             case ABS:
49                 double sign = val.signum();
50                 if (sign < 0) {
51                     return val.negate();
52                 } else if (sign == 0) {
53                     // ensure that the result is positive zero
54
return val.arithmetic(Token.PLUS, IntegerValue.ZERO, context);
55                 } else {
56                     return val;
57                 }
58             default:
59                 throw new UnsupportedOperationException JavaDoc("Unknown rounding function");
60         }
61     }
62
63 }
64
65
66 //
67
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
68
// you may not use this file except in compliance with the License. You may obtain a copy of the
69
// License at http://www.mozilla.org/MPL/
70
//
71
// Software distributed under the License is distributed on an "AS IS" basis,
72
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
73
// See the License for the specific language governing rights and limitations under the License.
74
//
75
// The Original Code is: all this file.
76
//
77
// The Initial Developer of the Original Code is Michael H. Kay.
78
//
79
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
80
//
81
// Contributor(s): none.
82
//
83
Popular Tags