| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 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 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|