Debian Codegraph @

net/sf/saxon/expr/TailIterator.java

raw symbols SCIP
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.EmptyIterator;
16 import net.sf.saxon.tree.iter.LookaheadIterator;
17
18 /**
19 * TailIterator iterates over a base sequence starting at an element other than the first.
20 * The base sequence is represented by an iterator which is consumed in the process
21 */
22
23 public class TailIterator<T extends Item<?>>
24 implements SequenceIterator<T>, LastPositionFinder, LookaheadIterator<T> {
25
26 private SequenceIterator<T> base;
27 private int start;
28
29 /**
30 * Private constructor: external callers should use the public factory method.
31 * Create a TailIterator, an iterator that starts at position N in a sequence and iterates
32 * to the end of the sequence
33 *
34 * @param base the base sequence of which we want to select the tail. Unusually, this iterator
35 * should be supplied pre-positioned so that the next call on next() returns the first item to
36 * be returned by the TailIterator
37 * @param start the index of the first required item in the sequence, starting from one. To
38 * include all items in the sequence except the first, set start = 2. This value is used only
39 * when cloning the iterator or when calculating the value of last().
40 */
41
42 private TailIterator(SequenceIterator<T> base, int start) {
43 this.base = base;
44 this.start = start;
45 }
46
47 /**
48 * Static factory method. Creates a TailIterator, unless the base Iterator is an
49 * ArrayIterator, in which case it optimizes by creating a new ArrayIterator directly over the
50 * underlying array. This optimization is important when doing recursion over a node-set using
51 * repeated calls of <code>$nodes[position()&gt;1]</code>
52 *
53 * @param base An iteration of the items to be filtered
54 * @param start The position of the first item to be included (base 1). If &lt;= 1, the whole of the
55 * base sequence is returned
56 * @return an iterator over the items in the sequence from the start item to the end of the sequence.
57 * The returned iterator will not necessarily be an instance of this class.
58 * @throws net.sf.saxon.trans.XPathException
59 * if a dynamic error occurs
60 */
61
62 public static <T extends Item<?>> SequenceIterator<T> make(SequenceIterator<T> base, int start) throws XPathException {
63 if (start <= 1) {
64 return base;
65 } else if (base instanceof ArrayIterator) {
66 return ((ArrayIterator<T>) base).makeSliceIterator(start, Integer.MAX_VALUE);
67 } else if ((base.getProperties() & SequenceIterator.GROUNDED) != 0) {
68 GroundedValue<T> value = base.materialize();
69 if (start > value.getLength()) {
70 return EmptyIterator.emptyIterator();
71 } else {
72 return new ValueTailIterator<>(value, start - 1);
73 }
74 } else {
75 // discard the first n-1 items from the underlying iterator
76 for (int i = 0; i < start - 1; i++) {
77 Item b = base.next();
78 if (b == null) {
79 return EmptyIterator.emptyIterator();
80 }
81 }
82 return new TailIterator<>(base, start);
83 }
84 }
85
86
87 public T next() throws XPathException {
88 return base.next();
89 }
90
91 public boolean hasNext() {
92 return ((LookaheadIterator) base).hasNext();
93 }
94
95 public int getLength() throws XPathException {
96 int bl = ((LastPositionFinder) base).getLength() - start + 1;
97 return bl > 0 ? bl : 0;
98 }
99
100 public void close() {
101 base.close();
102 }
103
104 /**
105 * Get properties of this iterator, as a bit-significant integer.
106 *
107 * @return the properties of this iterator. This will be some combination of
108 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
109 * and {@link #LOOKAHEAD}. It is always
110 * acceptable to return the value zero, indicating that there are no known special properties.
111 * It is acceptable for the properties of the iterator to change depending on its state.
112 */
113
114 public int getProperties() {
115 return base.getProperties() & (LAST_POSITION_FINDER | LOOKAHEAD);
116 }
117 }
118