| 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.LastPositionFinder; |
| 12 | import net.sf.saxon.expr.XPathContext; |
| 13 | import net.sf.saxon.om.GroundedValue; |
| 14 | import net.sf.saxon.om.Sequence; |
| 15 | import net.sf.saxon.om.SequenceIterator; |
| 16 | import net.sf.saxon.trans.XPathException; |
| 17 | import net.sf.saxon.value.Int64Value; |
| 18 | import net.sf.saxon.value.IntegerValue; |
| 19 | |
| 20 | /** |
| 21 | * Implementation of the fn:count function |
| 22 | */ |
| 23 | public class Count extends SystemFunction { |
| 24 | |
| 25 | /** |
| 26 | * For an expression that returns an integer or a sequence of integers, get |
| 27 | * a lower and upper bound on the values of the integers that may be returned, from |
| 28 | * static analysis. The default implementation returns null, meaning "unknown" or |
| 29 | * "not applicable". Other implementations return an array of two IntegerValue objects, |
| 30 | * representing the lower and upper bounds respectively. The values |
| 31 | * UNBOUNDED_LOWER and UNBOUNDED_UPPER are used by convention to indicate that |
| 32 | * the value may be arbitrarily large. The values MAX_STRING_LENGTH and MAX_SEQUENCE_LENGTH |
| 33 | * are used to indicate values limited by the size of a string or the size of a sequence. |
| 34 | * |
| 35 | * @return the lower and upper bounds of integer values in the result, or null to indicate |
| 36 | * unknown or not applicable. |
| 37 | */ |
| 38 | @Override |
| 39 | public IntegerValue[] getIntegerBounds() { |
| 40 | return new IntegerValue[]{Int64Value.ZERO, Expression.MAX_SEQUENCE_LENGTH}; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the number of items in a sequence identified by a SequenceIterator |
| 45 | * |
| 46 | * @param iter The SequenceIterator. This method moves the current position |
| 47 | * of the supplied iterator; if this isn't safe, make a copy of the iterator |
| 48 | * first by calling getAnother(). The supplied iterator must be positioned |
| 49 | * before the first item (there must have been no call on next()). |
| 50 | * @return the number of items in the underlying sequence |
| 51 | * @throws net.sf.saxon.trans.XPathException |
| 52 | * if a failure occurs reading the input sequence |
| 53 | */ |
| 54 | |
| 55 | public static int count(/*@NotNull*/ SequenceIterator iter) throws XPathException { |
| 56 | if ((iter.getProperties() & SequenceIterator.LAST_POSITION_FINDER) != 0) { |
| 57 | return ((LastPositionFinder) iter).getLength(); |
| 58 | } else { |
| 59 | int n = 0; |
| 60 | while (iter.next() != null) { |
| 61 | n++; |
| 62 | } |
| 63 | return n; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the number of items in a sequence identified by a SequenceIterator |
| 69 | * |
| 70 | * @param iter The SequenceIterator. The supplied iterator must be positioned |
| 71 | * before the first item (there must have been no call on next()). It will |
| 72 | * always be consumed |
| 73 | * @return the number of items in the underlying sequence |
| 74 | * @throws net.sf.saxon.trans.XPathException if a failure occurs reading the input sequence |
| 75 | */ |
| 76 | |
| 77 | public static int steppingCount(SequenceIterator iter) throws XPathException { |
| 78 | int n = 0; |
| 79 | while (iter.next() != null) { |
| 80 | n++; |
| 81 | } |
| 82 | return n; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Evaluate the expression |
| 87 | * |
| 88 | * @param context the dynamic evaluation context |
| 89 | * @param arguments the values of the arguments, supplied as Sequences |
| 90 | * @return the result of the evaluation, in the form of a Sequence |
| 91 | * @throws net.sf.saxon.trans.XPathException |
| 92 | * if a dynamic error occurs during the evaluation of the expression |
| 93 | */ |
| 94 | public IntegerValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 95 | Sequence<?> arg = arguments[0]; |
| 96 | int size = arg instanceof GroundedValue ? ((GroundedValue)arg).getLength() : count(arg.iterate()); |
| 97 | return Int64Value.makeIntegerValue(size); |
| 98 | } |
| 99 | |
| 100 | public String getCompilerName() { |
| 101 | return "CountCompiler"; |
| 102 | } |
| 103 | |
| 104 | public String getStreamerName() { |
| 105 | return "Count"; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | } |
| 112 |