Debian Codegraph @

net/sf/saxon/functions/RoundHalfToEven.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.functions;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.om.One;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.om.ZeroOrOne;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.value.NumericValue;
17
18 /**
19 * This class supports the round-to-half-even() function
20 */
21
22 public final class RoundHalfToEven extends SystemFunction {
23
24 /**
25 * Determine the cardinality of the function.
26 */
27
28 public int getCardinality(Expression[] arguments) {
29 return arguments[0].getCardinality();
30 }
31
32 /**
33 * Evaluate the expression
34 *
35 * @param context the dynamic evaluation context
36 * @param arguments the values of the arguments, supplied as Sequences
37 * @return the result of the evaluation, in the form of a Sequence
38 * @throws net.sf.saxon.trans.XPathException
39 * if a dynamic error occurs during the evaluation of the expression
40 */
41 public ZeroOrOne<NumericValue> call(XPathContext context, Sequence[] arguments) throws XPathException {
42 NumericValue val0 = (NumericValue) arguments[0].head();
43 if (val0 == null) {
44 return ZeroOrOne.empty();
45 }
46
47 int scale = 0;
48 if (arguments.length == 2) {
49 NumericValue scaleVal = (NumericValue) arguments[1].head();
50 if (scaleVal.compareTo(Integer.MAX_VALUE) > 0) {
51 return new ZeroOrOne<>(val0);
52 } else if (scaleVal.compareTo(Integer.MIN_VALUE) < 0) {
53 scale = Integer.MIN_VALUE;
54 } else {
55 scale = (int) scaleVal.longValue();
56 }
57 }
58 return new One<>(val0.roundHalfToEven(scale));
59 }
60
61 public String getCompilerName() {
62 return "RoundingCompiler";
63 }
64
65 }
66