Debian Codegraph @

net/sf/saxon/expr/BigRangeIterator.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.trans.UncheckedXPathException;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.tree.iter.AtomicIterator;
13 import net.sf.saxon.tree.iter.LookaheadIterator;
14 import net.sf.saxon.value.AtomicValue;
15 import net.sf.saxon.value.IntegerValue;
16
17 import java.math.BigInteger;
18
19 /**
20 * An Iterator that produces numeric values in a monotonic sequence,
21 * where the integers may exceed the range of a Long
22 */
23
24 public class BigRangeIterator implements AtomicIterator,
25 LastPositionFinder,
26 LookaheadIterator<AtomicValue> {
27
28 BigInteger start;
29 BigInteger currentValue;
30 BigInteger limit;
31
32 /**
33 * Create an iterator over a range of monotonically increasing integers
34 *
35 * @param start the first integer in the sequence
36 * @param end the last integer in the sequence. Must be &gt;= start.
37 */
38
39 public BigRangeIterator(BigInteger start, BigInteger end) throws XPathException {
40 if (end.subtract(start).compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
41 throw new XPathException("Saxon limit on sequence length exceeded (2^31)", "XPDY0130");
42 }
43 this.start = start;
44 currentValue = start.subtract(BigInteger.valueOf(1));
45 limit = end;
46 }
47
48 public boolean hasNext() {
49 return currentValue.compareTo(limit) < 0;
50 }
51
52 /*@Nullable*/
53 public IntegerValue next() {
54 currentValue = currentValue.add(BigInteger.valueOf(1));
55 if (currentValue.compareTo(limit) > 0) {
56 return null;
57 }
58 return IntegerValue.makeIntegerValue(currentValue);
59 }
60
61 public int getLength() {
62 BigInteger len = limit.subtract(start).add(BigInteger.valueOf(1));
63 if (len.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
64 throw new UncheckedXPathException(new XPathException("Sequence exceeds Saxon limit (32-bit integer)"));
65 }
66 return len.intValue();
67 }
68
69 /**
70 * Get properties of this iterator, as a bit-significant integer.
71 *
72 * @return the properties of this iterator. This will be some combination of
73 * properties such as {@link net.sf.saxon.om.SequenceIterator#GROUNDED}, {@link net.sf.saxon.om.SequenceIterator#LAST_POSITION_FINDER},
74 * and {@link net.sf.saxon.om.SequenceIterator#LOOKAHEAD}. It is always
75 * acceptable to return the value zero, indicating that there are no known special properties.
76 * It is acceptable for the properties of the iterator to change depending on its state.
77 */
78
79 public int getProperties() {
80 return LOOKAHEAD | LAST_POSITION_FINDER;
81 }
82
83 }
84