Debian Codegraph @

net/sf/saxon/functions/Average.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.*;
11 import net.sf.saxon.lib.ConversionRules;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.trans.Err;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.type.BuiltInAtomicType;
17 import net.sf.saxon.type.StringConverter;
18 import net.sf.saxon.value.*;
19
20
21 /**
22 * Implementation of the fn:avg function
23 */
24 public class Average extends FoldingFunction {
25
26 /**
27 * Determine the cardinality of the function.
28 */
29
30 public int getCardinality(Expression[] arguments) {
31 if (!Cardinality.allowsZero(arguments[0].getCardinality())) {
32 return StaticProperty.EXACTLY_ONE;
33 } else {
34 return super.getCardinality(arguments);
35 }
36 }
37
38 /**
39 * Create the Fold object which actually performs the evaluation.
40 *
41 * @param context the dynamic evaluation context
42 * @param additionalArguments the values of all arguments other than the first.
43 * @return the Fold object used to compute the function
44 */
45 @Override
46 public Fold getFold(XPathContext context, Sequence... additionalArguments) {
47 return new AverageFold(context);
48 }
49
50 private class AverageFold implements Fold {
51 private XPathContext context;
52 private AtomicValue data;
53 private boolean atStart = true;
54 private ConversionRules rules;
55 private StringConverter toDouble;
56 private int count = 0;
57
58 public AverageFold(XPathContext context) {
59 this.context = context;
60 this.rules = context.getConfiguration().getConversionRules();
61 this.toDouble = BuiltInAtomicType.DOUBLE.getStringConverter(rules);
62 }
63
64 /**
65 * Process one item in the input sequence, returning a new copy of the working data
66 *
67 * @param item the item to be processed from the input sequence
68 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs
69 */
70 public void processItem(Item<?> item) throws XPathException {
71 AtomicValue next = (AtomicValue)item;
72 if (next instanceof UntypedAtomicValue) {
73 next = toDouble.convert((UntypedAtomicValue)next).asAtomic();
74 }
75 count++;
76 if (atStart) {
77 if (next instanceof NumericValue || next instanceof DayTimeDurationValue || next instanceof YearMonthDurationValue) {
78 data = next;
79 atStart = false;
80 } else if (next instanceof DurationValue) {
81 throw new XPathException(
82 "Input to avg() contains a duration (" + Err.depict(next) +
83 ") that is neither an xs:dayTimeDuration nor an xs:yearMonthDuration", "FORG0006");
84 } else {
85 throw new XPathException(
86 "Input to avg() contains a value (" + Err.depict(next) +
87 ") that is neither numeric, nor a duration", "FORG0006");
88 }
89 } else {
90 if (data instanceof NumericValue) {
91 if (!(next instanceof NumericValue)) {
92 throw new XPathException(
93 "Input to avg() contains a mix of numeric and non-numeric values", "FORG0006");
94 }
95 data = ArithmeticExpression.compute(data, Calculator.PLUS, next, context);
96 } else if (data instanceof DurationValue) {
97 if (!(next instanceof DurationValue)) {
98 throw new XPathException(
99 "Input to avg() contains a mix of duration and non-duration values", "FORG0006");
100 }
101 try {
102 data = ((DurationValue) data).add((DurationValue) next);
103 } catch (XPathException e) {
104 if ("XPTY0004".equals(e.getErrorCodeLocalPart())){
105 e.setErrorCode("FORG0006");
106 }
107 throw e;
108 }
109 } else {
110 throw new XPathException(
111 "Input to avg() contains a value (" + Err.depict(data) +
112 ") that is neither numeric, nor a duration", "FORG0006");
113 }
114 }
115 }
116
117 /**
118 * Ask whether the computation has completed. A function that can deliver its final
119 * result without reading the whole input should return true; this will be followed
120 * by a call on result() to deliver the final result.
121 * @return true if the result of the function is now available even though not all
122 * items in the sequence have been processed
123 */
124 public boolean isFinished() {
125 return data instanceof DoubleValue && data.isNaN();
126 }
127
128 /**
129 * Compute the final result of the function, when all the input has been processed
130 *
131 * @return the result of the function
132 * @throws net.sf.saxon.trans.XPathException
133 * if a dynamic error occurs
134 */
135 public Sequence<?> result() throws XPathException {
136 if (atStart) {
137 return EmptySequence.getInstance();
138 } else if (data instanceof NumericValue) {
139 return ArithmeticExpression.compute(data, Calculator.DIV, new Int64Value(count), context);
140 } else {
141 return ((DurationValue) data).divide(count);
142 }
143 }
144 }
145
146
147
148 }
149