| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 23 |
|
| 24 |
public class Average extends FoldingFunction { |
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public boolean isFinished() { |
| 125 |
return data instanceof DoubleValue && data.isNaN(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 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 |
|