| 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.expr; |
| 9 | |
| 10 | import net.sf.saxon.trans.XPathException; |
| 11 | import net.sf.saxon.tree.iter.AtomicIterator; |
| 12 | import net.sf.saxon.tree.iter.LookaheadIterator; |
| 13 | import net.sf.saxon.tree.iter.ReversibleIterator; |
| 14 | import net.sf.saxon.value.AtomicValue; |
| 15 | import net.sf.saxon.value.Int64Value; |
| 16 | import net.sf.saxon.value.IntegerValue; |
| 17 | |
| 18 | /** |
| 19 | * Iterator that produces numeric values in a monotonic sequence, |
| 20 | * ascending or descending. Although a range expression (N to M) is always |
| 21 | * in ascending order, applying the reverse() function will produce |
| 22 | * a RangeIterator that works in descending order. |
| 23 | */ |
| 24 | |
| 25 | public class ReverseRangeIterator implements AtomicIterator, |
| 26 | ReversibleIterator<AtomicValue>, |
| 27 | LastPositionFinder, |
| 28 | LookaheadIterator<AtomicValue> { |
| 29 | |
| 30 | long start; |
| 31 | long currentValue; |
| 32 | long limit; |
| 33 | |
| 34 | /** |
| 35 | * Create an iterator over a range of integers in monotonic descending order |
| 36 | * |
| 37 | * @param start the first integer to be delivered (the highest in the range) |
| 38 | * @param end the last integer to be delivered (the lowest in the range). Must be <= start |
| 39 | * @throws XPathException if the values are inconsistent or if the length is greater than 2^31 |
| 40 | */ |
| 41 | |
| 42 | public ReverseRangeIterator(long start, long end) throws XPathException { |
| 43 | if (start - end > Integer.MAX_VALUE) { |
| 44 | throw new XPathException("Saxon limit on sequence length exceeded (2^31)", "XPDY0130"); |
| 45 | } |
| 46 | this.start = start; |
| 47 | currentValue = start + 1; |
| 48 | limit = end; |
| 49 | } |
| 50 | |
| 51 | public boolean hasNext() { |
| 52 | return currentValue > limit; |
| 53 | } |
| 54 | |
| 55 | /*@Nullable*/ |
| 56 | public IntegerValue next() { |
| 57 | if (--currentValue < limit) { |
| 58 | return null; |
| 59 | } |
| 60 | return Int64Value.makeIntegerValue(currentValue); |
| 61 | } |
| 62 | |
| 63 | public int getLength() { |
| 64 | return (int) ((start - limit) + 1); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get properties of this iterator, as a bit-significant integer. |
| 69 | * |
| 70 | * @return the properties of this iterator. This will be some combination of |
| 71 | * properties such as {@link net.sf.saxon.om.SequenceIterator#GROUNDED}, {@link net.sf.saxon.om.SequenceIterator#LAST_POSITION_FINDER}, |
| 72 | * and {@link net.sf.saxon.om.SequenceIterator#LOOKAHEAD}. It is always |
| 73 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 74 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 75 | */ |
| 76 | |
| 77 | public int getProperties() { |
| 78 | return LOOKAHEAD | LAST_POSITION_FINDER; |
| 79 | } |
| 80 | |
| 81 | public AtomicIterator getReverseIterator() { |
| 82 | return new RangeIterator(limit, start); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | } |
| 87 |