| 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.ArrayIterator; |
| 15 | import net.sf.saxon.tree.iter.LookaheadIterator; |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * A SubsequenceIterator selects a subsequence of a sequence |
| 20 | */ |
| 21 | |
| 22 | public class SubsequenceIterator<T extends Item<?>> implements SequenceIterator<T>, LastPositionFinder, LookaheadIterator<T> { |
| 23 | |
| 24 | private SequenceIterator<T> base; |
| 25 | private int basePosition = 0; |
| 26 | private int min; |
| 27 | private int max; |
| 28 | /*@Nullable*/ private T nextItem = null; |
| 29 | |
| 30 | /** |
| 31 | * Private Constructor: use the factory method instead! |
| 32 | * |
| 33 | * @param base An iteration of the items to be filtered |
| 34 | * @param min The position of the first item to be included (1-based) |
| 35 | * @param max The position of the last item to be included (1-based) |
| 36 | * @throws XPathException if a dynamic error occurs |
| 37 | */ |
| 38 | |
| 39 | private SubsequenceIterator(SequenceIterator<T> base, int min, int max) throws XPathException { |
| 40 | this.base = base; |
| 41 | this.min = min; |
| 42 | if (min < 1) { |
| 43 | min = 1; |
| 44 | } |
| 45 | this.max = max; |
| 46 | if (max < min) { |
| 47 | nextItem = null; |
| 48 | return; |
| 49 | } |
| 50 | int i = 1; |
| 51 | while (i++ <= min) { |
| 52 | nextItem = base.next(); |
| 53 | basePosition++; |
| 54 | if (nextItem == null) { |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Static factory method. Creates a SubsequenceIterator, unless for example the base Iterator is an |
| 62 | * ArrayIterator, in which case it optimizes by creating a new ArrayIterator directly over the |
| 63 | * underlying array. This optimization is important when doing recursion over a node-set using |
| 64 | * repeated calls of <code>$nodes[position()>1]</code> |
| 65 | * |
| 66 | * @param base An iteration of the items to be filtered |
| 67 | * @param min The position of the first item to be included (base 1) |
| 68 | * @param max The position of the last item to be included (base 1) |
| 69 | * @return an iterator over the requested subsequence |
| 70 | * @throws XPathException if a dynamic error occurs |
| 71 | */ |
| 72 | |
| 73 | public static <T extends Item<?>> SequenceIterator<T> make(SequenceIterator<T> base, int min, int max) throws XPathException { |
| 74 | if (base instanceof ArrayIterator) { |
| 75 | return ((ArrayIterator<T>) base).makeSliceIterator(min, max); |
| 76 | } else if (max == Integer.MAX_VALUE) { |
| 77 | return TailIterator.make(base, min); |
| 78 | } else if ((base.getProperties() & SequenceIterator.GROUNDED) != 0 && min > 4) { |
| 79 | GroundedValue<T> value = base.materialize(); |
| 80 | value = value.subsequence(min - 1, max - min + 1); |
| 81 | return value.iterate(); |
| 82 | } else { |
| 83 | return new SubsequenceIterator<>(base, min, max); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Test whether there are any more items available in the sequence |
| 89 | */ |
| 90 | |
| 91 | public boolean hasNext() { |
| 92 | return nextItem != null; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the next item if there is one |
| 97 | */ |
| 98 | |
| 99 | public T next() throws XPathException { |
| 100 | if (nextItem == null) { |
| 101 | return null; |
| 102 | } |
| 103 | T current = nextItem; |
| 104 | if (basePosition < max) { |
| 105 | nextItem = base.next(); |
| 106 | basePosition++; |
| 107 | } else { |
| 108 | nextItem = null; |
| 109 | base.close(); |
| 110 | } |
| 111 | return current; |
| 112 | } |
| 113 | |
| 114 | public void close() { |
| 115 | base.close(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get properties of this iterator, as a bit-significant integer. |
| 120 | * |
| 121 | * @return the properties of this iterator. This will be some combination of |
| 122 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 123 | * and {@link #LOOKAHEAD}. It is always |
| 124 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 125 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 126 | */ |
| 127 | |
| 128 | public int getProperties() { |
| 129 | int p = LOOKAHEAD; |
| 130 | p |= base.getProperties() & LAST_POSITION_FINDER; |
| 131 | return p; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the last position (that is, the number of items in the sequence). This method is |
| 136 | * non-destructive: it does not change the state of the iterator. |
| 137 | * The result is undefined if the next() method of the iterator has already returned null. |
| 138 | * This method must not be called unless the result of getProperties() on the iterator |
| 139 | * includes the bit setting {@link #LAST_POSITION_FINDER} |
| 140 | */ |
| 141 | |
| 142 | public int getLength() throws XPathException { |
| 143 | int lastBase = ((LastPositionFinder) base).getLength(); |
| 144 | int z = Math.min(lastBase, max); |
| 145 | return Math.max(z - min + 1, 0); |
| 146 | } |
| 147 | |
| 148 | } |
| 149 |