| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 21 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 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 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public int getProperties() { |
| 80 |
return LOOKAHEAD | LAST_POSITION_FINDER; |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|