Debian Codegraph @

net/sf/saxon/value/IntegerRange.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.value;
9
10 import net.sf.saxon.expr.RangeIterator;
11 import net.sf.saxon.expr.parser.ExpressionTool;
12 import net.sf.saxon.om.AtomicArray;
13 import net.sf.saxon.om.AtomicSequence;
14 import net.sf.saxon.om.GroundedValue;
15 import net.sf.saxon.om.SequenceTool;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.iter.AtomicIterator;
18
19 import java.util.Iterator;
20
21 /**
22 * This class represents a sequence of consecutive ascending integers, for example 1 to 50.
23 * The integers must be within the range of a Java long.
24 */
25
26 public class IntegerRange implements AtomicSequence {
27
28 public long start;
29 public long end;
30
31 /**
32 * Construct an integer range expression
33 *
34 * @param start the first integer in the sequence (inclusive)
35 * @param end the last integer in the sequence (inclusive). Must be >= start
36 */
37
38 public IntegerRange(long start, long end) {
39 if (end < start) {
40 throw new IllegalArgumentException("end < start in IntegerRange");
41 }
42 if (end - start > Integer.MAX_VALUE) {
43 throw new IllegalArgumentException("Maximum length of sequence in Saxon is " + Integer.MAX_VALUE);
44 }
45 this.start = start;
46 this.end = end;
47 }
48
49 /**
50 * Get the first integer in the sequence (inclusive)
51 *
52 * @return the first integer in the sequence (inclusive)
53 */
54
55 public long getStart() {
56 return start;
57 }
58
59 /**
60 * Get the last integer in the sequence (inclusive)
61 *
62 * @return the last integer in the sequence (inclusive)
63 */
64
65 public long getEnd() {
66 return end;
67 }
68
69
70 /**
71 * Return an Iterator to iterate over the values of a sequence. The value of every
72 * expression can be regarded as a sequence, so this method is supported for all
73 * expressions. This default implementation handles iteration for expressions that
74 * return singleton values: for non-singleton expressions, the subclass must
75 * provide its own implementation.
76 *
77 * @return a SequenceIterator that can be used to iterate over the result
78 * of the expression
79 */
80
81 /*@NotNull*/
82 public AtomicIterator iterate() {
83 return new RangeIterator(start, end);
84 }
85
86 /**
87 * Get the n'th item in the sequence (starting from 0). This is defined for all
88 * Values, but its real benefits come for a sequence Value stored extensionally
89 * (or for a MemoClosure, once all the values have been read)
90 */
91
92 /*@Nullable*/
93 public IntegerValue itemAt(int n) {
94 if (n < 0 || n > (end - start)) {
95 return null;
96 }
97 return Int64Value.makeIntegerValue(start + n);
98 }
99
100
101 /**
102 * Get a subsequence of the value
103 *
104 * @param start the index of the first item to be included in the result, counting from zero.
105 * A negative value is taken as zero. If the value is beyond the end of the sequence, an empty
106 * sequence is returned
107 * @param length the number of items to be included in the result. Specify Integer.MAX_VALUE to
108 * get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence
109 * is returned. If the value goes off the end of the sequence, the result returns items up to the end
110 * of the sequence
111 * @return the required subsequence.
112 */
113
114 /*@NotNull*/
115 public GroundedValue subsequence(int start, int length) {
116 if (length <= 0) {
117 return EmptySequence.getInstance();
118 }
119 long newStart = this.start + (start > 0 ? start : 0);
120 long newEnd = newStart + length - 1;
121 if (newEnd > end) {
122 newEnd = end;
123 }
124 if (newEnd >= newStart) {
125 return new IntegerRange(newStart, newEnd);
126 } else {
127 return EmptySequence.getInstance();
128 }
129 }
130
131 /**
132 * Get the length of the sequence
133 */
134
135 public int getLength() {
136 return (int) (end - start + 1);
137 }
138
139 public IntegerValue head() {
140 return new Int64Value(start);
141 }
142
143 /**
144 * Get the canonical lexical representation as defined in XML Schema. This is not always the same
145 * as the result of casting to a string according to the XPath rules.
146 *
147 * @return the canonical lexical representation if defined in XML Schema; otherwise, the result
148 * of casting to string according to the XPath 2.0 rules
149 */
150 public CharSequence getCanonicalLexicalRepresentation() {
151 return getStringValueCS();
152 }
153
154 /**
155 * Get a Comparable value that implements the XML Schema ordering comparison semantics for this value.
156 * The default implementation is written to compare sequences of atomic values.
157 * This method is overridden for AtomicValue and its subclasses.
158 * <p>In the case of data types that are partially ordered, the returned Comparable extends the standard
159 * semantics of the compareTo() method by returning the value {@link net.sf.saxon.om.SequenceTool#INDETERMINATE_ORDERING} when there
160 * is no defined order relationship between two given values.</p>
161 *
162 * @return a Comparable that follows XML Schema comparison rules
163 */
164 public Comparable getSchemaComparable() {
165 try {
166 return new AtomicArray(iterate()).getSchemaComparable();
167 } catch (XPathException err) {
168 throw new AssertionError(err);
169 }
170 }
171
172 public CharSequence getStringValueCS() {
173 try {
174 return SequenceTool.getStringValue(this);
175 } catch (XPathException err) {
176 throw new AssertionError(err);
177 }
178 }
179
180 public String getStringValue() {
181 return getStringValueCS().toString();
182 }
183
184 public boolean effectiveBooleanValue() throws XPathException {
185 return ExpressionTool.effectiveBooleanValue(iterate());
186 }
187
188 /**
189 * Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be
190 * EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance
191 * of AtomicValue. If the value is a single item of any other kind, the result will be an instance
192 * of SingletonItem. Otherwise, the result will typically be unchanged.
193 *
194 * @return the simplified sequence
195 */
196 public GroundedValue reduce() {
197 if (start == end) {
198 return itemAt(0);
199 } else {
200 return this;
201 }
202 }
203
204 public String toString() {
205 return "(" + start + " to " + end + ")";
206 }
207
208 /**
209 * Return a Java iterator over the atomic sequence.
210 *
211 * @return an Iterator.
212 */
213
214 public Iterator<AtomicValue> iterator() {
215 return new Iterator<AtomicValue>() {
216
217 long current = start;
218
219 /**
220 * Returns <tt>true</tt> if the iteration has more elements. (In other
221 * words, returns <tt>true</tt> if <tt>next</tt> would return an element
222 * rather than throwing an exception.)
223 *
224 * @return <tt>true</tt> if the iterator has more elements.
225 */
226 public boolean hasNext() {
227 return current <= end;
228 }
229
230 /**
231 * Removes from the underlying collection the last element returned by the
232 * iterator (optional operation). This method can be called only once per
233 * call to <tt>next</tt>. The behavior of an iterator is unspecified if
234 * the underlying collection is modified while the iteration is in
235 * progress in any way other than by calling this method.
236 *
237 * @throws UnsupportedOperationException if the <tt>remove</tt>
238 * operation is not supported by this Iterator.
239 * @throws IllegalStateException if the <tt>next</tt> method has not
240 * yet been called, or the <tt>remove</tt> method has already
241 * been called after the last call to the <tt>next</tt>
242 * method.
243 */
244 public void remove() {
245 throw new UnsupportedOperationException();
246 }
247
248 /**
249 * Returns the next element in the iteration.
250 *
251 * @return the next element in the iteration.
252 * @throws java.util.NoSuchElementException
253 * iteration has no more elements.
254 */
255 public IntegerValue next() {
256 return new Int64Value(current++);
257
258 }
259 };
260 }
261 }
262