| 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.om.GroundedValue; |
| 11 | import net.sf.saxon.om.Item; |
| 12 | import net.sf.saxon.om.SequenceIterator; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.tree.iter.GroundedIterator; |
| 15 | import net.sf.saxon.tree.iter.LookaheadIterator; |
| 16 | |
| 17 | /** |
| 18 | * <tt>ValueTailIterator</tt> iterates over a base sequence starting at an element other than the first. |
| 19 | * It is used in the case where the base sequence is "grounded", that is, it exists in memory and |
| 20 | * supports efficient direct addressing. |
| 21 | */ |
| 22 | |
| 23 | public class ValueTailIterator<T extends Item<?>> |
| 24 | implements SequenceIterator<T>, GroundedIterator<T>, LookaheadIterator<T> { |
| 25 | |
| 26 | private GroundedValue<T> baseValue; |
| 27 | private int start; // zero-based |
| 28 | private int pos = 0; |
| 29 | |
| 30 | /** |
| 31 | * Construct a ValueTailIterator |
| 32 | * |
| 33 | * @param base The items to be filtered |
| 34 | * @param start The position of the first item to be included (zero-based) |
| 35 | */ |
| 36 | |
| 37 | public ValueTailIterator(GroundedValue<T> base, int start) { |
| 38 | baseValue = base; |
| 39 | this.start = start; |
| 40 | pos = 0; |
| 41 | } |
| 42 | |
| 43 | public T next() throws XPathException { |
| 44 | return baseValue.itemAt(start + pos++); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | public boolean hasNext() { |
| 49 | return baseValue.itemAt(start + pos) != null; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Return a Value containing all the items in the sequence returned by this |
| 54 | * SequenceIterator. This should be an "in-memory" value, not a Closure. |
| 55 | * |
| 56 | * @return the corresponding Value |
| 57 | */ |
| 58 | |
| 59 | public GroundedValue<T> materialize() { |
| 60 | if (start == 0) { |
| 61 | return baseValue; |
| 62 | } else { |
| 63 | return baseValue.subsequence(start, Integer.MAX_VALUE); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public GroundedValue<T> getResidue() { |
| 69 | if (start == 0 && pos == 0) { |
| 70 | return baseValue; |
| 71 | } else { |
| 72 | return baseValue.subsequence(start + pos, Integer.MAX_VALUE); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get properties of this iterator, as a bit-significant integer. |
| 78 | * |
| 79 | * @return the properties of this iterator. This will be some combination of |
| 80 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 81 | * and {@link #LOOKAHEAD}. It is always |
| 82 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 83 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 84 | */ |
| 85 | |
| 86 | public int getProperties() { |
| 87 | return GROUNDED | LOOKAHEAD; |
| 88 | } |
| 89 | } |
| 90 |