Debian Codegraph @

net/sf/saxon/expr/RangeIterator.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.SequenceIterator;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.tree.iter.*;
14 import net.sf.saxon.value.*;
15
16 /**
17 * An Iterator that produces numeric values in a monotonic sequence,
18 * ascending or descending. Although a range expression (N to M) is always
19 * in ascending order, applying the reverse() function will produce
20 * a RangeIterator that works in descending order.
21 */
22
23 public class RangeIterator implements AtomicIterator,
24 ReversibleIterator<AtomicValue>,
25 LastPositionFinder,
26 LookaheadIterator<AtomicValue>,
27 GroundedIterator<AtomicValue> {
28
29 long start;
30 long currentValue;
31 long limit;
32
33 public static SequenceIterator<AtomicValue> makeRangeIterator(IntegerValue start, IntegerValue end) throws XPathException {
34 if (start == null || end == null) {
35 return EmptyIterator.OfAtomic.THE_INSTANCE;
36 } else {
37 if (start.compareTo(end) > 0) {
38 return EmptyIterator.OfAtomic.THE_INSTANCE;
39 }
40 if (start instanceof BigIntegerValue || end instanceof BigIntegerValue) {
41 return new BigRangeIterator(start.asBigInteger(), end.asBigInteger());
42 } else {
43 long startVal = start.longValue();
44 long endVal = end.longValue();
45 if (endVal - startVal > Integer.MAX_VALUE) {
46 throw new XPathException("Saxon limit on sequence length exceeded (2^31)", "XPDY0130");
47 }
48 return new RangeIterator(startVal, endVal);
49 }
50 }
51 }
52
53 /**
54 * Create an iterator over a range of monotonically increasing integers
55 *
56 * @param start the first integer in the sequence
57 * @param end the last integer in the sequence. Must be &gt;= start.
58 */
59
60 public RangeIterator(long start, long end) {
61 this.start = start;
62 currentValue = start - 1;
63 limit = end;
64 }
65
66 public boolean hasNext() {
67 return currentValue < limit;
68 }
69
70 /*@Nullable*/
71 public IntegerValue next() {
72 if (++currentValue > limit) {
73 return null;
74 }
75 return Int64Value.makeIntegerValue(currentValue);
76 }
77
78 public int getLength() {
79 return (int) ((limit - start) + 1);
80 }
81
82 /**
83 * Get properties of this iterator, as a bit-significant integer.
84 *
85 * @return the properties of this iterator. This will be some combination of
86 * properties such as {@link net.sf.saxon.om.SequenceIterator#GROUNDED}, {@link net.sf.saxon.om.SequenceIterator#LAST_POSITION_FINDER},
87 * and {@link net.sf.saxon.om.SequenceIterator#LOOKAHEAD}. It is always
88 * acceptable to return the value zero, indicating that there are no known special properties.
89 * It is acceptable for the properties of the iterator to change depending on its state.
90 */
91
92 public int getProperties() {
93 return LOOKAHEAD | LAST_POSITION_FINDER | GROUNDED;
94 }
95
96 public SequenceIterator<AtomicValue> getReverseIterator() {
97 try {
98 return new ReverseRangeIterator(limit, start);
99 } catch (XPathException err) {
100 throw new AssertionError(err);
101 }
102 }
103
104 /**
105 * Return a Value containing all the items in the sequence returned by this
106 * SequenceIterator. This should be an "in-memory" value, not a Closure.
107 *
108 * @return the corresponding Value
109 */
110
111 public GroundedValue<AtomicValue> materialize() {
112 return new IntegerRange(start, limit);
113 }
114
115 @Override
116 public GroundedValue<AtomicValue> getResidue() {
117 return new IntegerRange(currentValue, limit);
118 }
119 }
120