Debian Codegraph @

net/sf/saxon/value/SequenceExtent.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.LastPositionFinder;
11 import net.sf.saxon.expr.StaticProperty;
12 import net.sf.saxon.expr.parser.ExpressionTool;
13 import net.sf.saxon.om.*;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.iter.GroundedIterator;
16 import net.sf.saxon.tree.iter.ListIterator;
17 import net.sf.saxon.tree.iter.ReverseListIterator;
18 import net.sf.saxon.tree.iter.UnfailingIterator;
19 import net.sf.saxon.tree.util.FastStringBuffer;
20
21 import java.util.*;
22
23
24 /**
25 * A sequence value implemented extensionally. That is, this class represents a sequence
26 * by allocating memory to each item in the sequence.
27 */
28
29 public class SequenceExtent<T extends Item<?>> implements GroundedValue<T> {
30 private List<T> value;
31
32 /**
33 * Construct an sequence from an array of items. Note, the array of items is used as is,
34 * which means the caller must not subsequently change its contents.
35 *
36 * @param items the array of items to be included in the sequence
37 */
38
39 public SequenceExtent(T[] items) {
40 value = Arrays.asList(items);
41 }
42
43 /**
44 * Construct a SequenceExtent as a view of another SequenceExtent
45 *
46 * @param ext The existing SequenceExtent
47 * @param start zero-based offset of the first item in the existing SequenceExtent
48 * that is to be included in the new SequenceExtent
49 * @param length The number of items in the new SequenceExtent
50 */
51
52 public SequenceExtent(SequenceExtent<T> ext, int start, int length) {
53 value = ext.value.subList(start, start+length);
54 }
55
56 /**
57 * Construct a SequenceExtent from a List. The members of the list must all
58 * be Items
59 *
60 * @param list the list of items to be included in the sequence
61 */
62
63 public SequenceExtent(List<T> list) {
64 this.value = list;
65 }
66
67 /**
68 * Construct a sequence containing all the remaining items in a SequenceIterator.
69 *
70 * @param iter The supplied sequence of items. The returned sequence will contain all
71 * items delivered by repeated calls on next() on this iterator, and the
72 * iterator will be consumed by calling the method.
73 * @throws net.sf.saxon.trans.XPathException
74 * if reading the items using the
75 * SequenceIterator raises an error
76 */
77
78 public SequenceExtent(SequenceIterator<T> iter) throws XPathException {
79 int len = (iter.getProperties() & SequenceIterator.LAST_POSITION_FINDER) == 0
80 ? 20
81 : ((LastPositionFinder)iter).getLength();
82 List<T> list = new ArrayList<>(len);
83 iter.forEachOrFail(list::add);
84 value = list;
85 }
86
87 /**
88 * Factory method to make a GroundedValue holding the contents of any SequenceIterator.
89 *
90 * @param iter a Sequence iterator that may or may not be consumed to deliver the items in the sequence.
91 * The iterator must be positioned at the start.
92 * @return a GroundedValue holding the items delivered by the SequenceIterator. If the
93 * sequence is empty the result will be an instance of {@link EmptySequence}. If it is of length
94 * one, the result will be an {@link Item}. In all other cases, it will be an instance of
95 * {@link SequenceExtent}.
96 * @throws net.sf.saxon.trans.XPathException
97 * if an error occurs processing the values from
98 * the iterator.
99 * @deprecated since 9.9: use {@link SequenceIterator#materialize()}
100 */
101
102 public static <T extends Item<?>> GroundedValue<? extends T> makeSequenceExtent(SequenceIterator<T> iter) throws XPathException {
103 return iter.materialize();
104 }
105
106 /**
107 * Factory method to make a GroundedValue holding the contents of any SequenceIterator.
108 *
109 * @param iter a Sequence iterator that may or may not be consumed to deliver the items in the sequence.
110 * The iterator must be positioned at the start.
111 * @return a GroundedValue holding the items delivered by the SequenceIterator. If the
112 * sequence is empty the result will be an instance of {@link EmptySequence}. If it is of length
113 * one, the result will be an {@link Item}. In other cases, the actual implementation class
114 * is not defined.
115 * <p>This method is intended primarily for internal use. For external applications, calling
116 * {@link SequenceIterator#materialize()} is preferable, because it recognizes cases that can
117 * be optimized (notably, cases where the iterator is already backed by an array or list in memory).</p>
118 *
119 * @throws net.sf.saxon.trans.XPathException if an error occurs processing the values from
120 * the iterator.
121 * @deprecated since 9.9: use {@link SequenceIterator#materialize()}
122 */
123
124 public static <T extends Item<?>> GroundedValue<T> fromIterator(SequenceIterator<T> iter) throws XPathException {
125 return new SequenceExtent<>(iter).reduce();
126 }
127
128 /**
129 * Factory method to make a GroundedValue holding the remaining contents of any SequenceIterator,
130 * that is, the contents that have not yet been read
131 *
132 * @param iter a Sequence iterator that may or may not be consumed to deliver the items in the sequence.
133 * The iterator need not be positioned at the start.
134 * @return a GroundedValue holding the items delivered by the SequenceIterator. If the
135 * sequence is empty the result will be an instance of {@link EmptySequence}. If it is of length
136 * one, the result will be an {@link Item}. In all other cases, it will be an instance of
137 * {@link SequenceExtent}.
138 * @throws net.sf.saxon.trans.XPathException if an error occurs processing the values from
139 * the iterator.
140 */
141
142 public static <T extends Item<?>> GroundedValue<T> makeResidue(SequenceIterator<T> iter) throws XPathException {
143 if ((iter.getProperties() & SequenceIterator.GROUNDED) != 0) {
144 return ((GroundedIterator) iter).getResidue();
145 }
146 SequenceExtent<T> extent = new SequenceExtent<>(iter);
147 return extent.reduce();
148 }
149
150
151 /**
152 * Factory method to make a Value holding the contents of any List of items
153 *
154 * @param input a List containing the items in the sequence
155 * @return a ValueRepresentation holding the items in the list. If the
156 * sequence is empty the result will be an instance of {@link EmptySequence}. If it is of length
157 * one, the result will be an {@link Item}. In all other cases, it will be an instance of
158 * {@link SequenceExtent}.
159 */
160
161 public static <T extends Item<?>> GroundedValue<T> makeSequenceExtent(/*@NotNull*/ List<T> input) {
162 int len = input.size();
163 if (len == 0) {
164 return EmptySequence.getInstance();
165 } else if (len == 1) {
166 return (GroundedValue<T>)input.get(0);
167 } else {
168 return new SequenceExtent<>(input);
169 }
170 }
171
172 public String getStringValue() throws XPathException {
173 return SequenceTool.getStringValue(this);
174 }
175
176 public CharSequence getStringValueCS() throws XPathException {
177 return SequenceTool.getStringValue(this);
178 }
179
180 /**
181 * Get the first item in the sequence.
182 *
183 * @return the first item in the sequence if there is one, or null if the sequence
184 * is empty
185 */
186 public T head() {
187 return itemAt(0);
188 }
189
190 /**
191 * Get the number of items in the sequence
192 *
193 * @return the number of items in the sequence
194 */
195
196 public int getLength() {
197 return value.size();
198 }
199
200 /**
201 * Determine the cardinality
202 *
203 * @return the cardinality of the sequence, using the constants defined in
204 * net.sf.saxon.value.Cardinality
205 * @see net.sf.saxon.value.Cardinality
206 */
207
208 public int getCardinality() {
209 switch (value.size()) {
210 case 0:
211 return StaticProperty.EMPTY;
212 case 1:
213 return StaticProperty.EXACTLY_ONE;
214 default:
215 return StaticProperty.ALLOWS_ONE_OR_MORE;
216 }
217 }
218
219 /**
220 * Get the n'th item in the sequence (starting with 0 as the first item)
221 *
222 * @param n the position of the required item
223 * @return the n'th item in the sequence, or null if the position is out of range
224 */
225
226 /*@Nullable*/
227 public T itemAt(int n) {
228 if (n < 0 || n >= getLength()) {
229 return null;
230 } else {
231 return value.get(n);
232 }
233 }
234
235 /**
236 * Return an iterator over this sequence.
237 *
238 * @return the required SequenceIterator, positioned at the start of the
239 * sequence
240 */
241
242 /*@NotNull*/
243 public ListIterator<T> iterate() {
244 return new ListIterator<>(value);
245 }
246
247 /**
248 * Return an enumeration of this sequence in reverse order (used for reverse axes)
249 *
250 * @return an AxisIterator that processes the items in reverse order
251 */
252
253 /*@NotNull*/
254 public UnfailingIterator<T> reverseIterate() {
255 return new ReverseListIterator<>(value);
256 }
257
258 /**
259 * Get the effective boolean value
260 */
261
262 public boolean effectiveBooleanValue() throws XPathException {
263 int len = getLength();
264 if (len == 0) {
265 return false;
266 } else {
267 Item first = value.get(0);
268 if (first instanceof NodeInfo) {
269 return true;
270 } else if (len == 1 && first instanceof AtomicValue) {
271 return first.effectiveBooleanValue();
272 } else {
273 // this will fail - reuse the error messages
274 return ExpressionTool.effectiveBooleanValue(iterate());
275 }
276 }
277 }
278
279
280 /**
281 * Get a subsequence of the value
282 *
283 * @param start the index of the first item to be included in the result, counting from zero.
284 * A negative value is taken as zero. If the value is beyond the end of the sequence, an empty
285 * sequence is returned
286 * @param length the number of items to be included in the result. Specify Integer.MAX_VALUE to
287 * get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence
288 * is returned. If the value goes off the end of the sequence, the result returns items up to the end
289 * of the sequence
290 * @return the required subsequence. If min is
291 */
292
293 /*@NotNull*/
294 public GroundedValue<T> subsequence(int start, int length) {
295 int end = value.size();
296 if (start < 0) {
297 start = 0;
298 } else if (start >= end) {
299 return EmptySequence.getInstance();
300 }
301 int newStart = start;
302 int newEnd;
303 if (length > end) {
304 newEnd = end;
305 } else if (length < 0) {
306 return EmptySequence.getInstance();
307 } else {
308 newEnd = newStart + length;
309 if (newEnd > end) {
310 newEnd = end;
311 }
312 }
313 return new SequenceExtent<>(value.subList(newStart, newEnd));
314 }
315
316 /*@NotNull*/
317 public String toString() {
318 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
319 for (int i = 0; i < value.size(); i++) {
320 fsb.append(i == 0 ? "(" : ", ");
321 fsb.append(value.get(i).toString());
322 }
323 fsb.append(')');
324 return fsb.toString();
325 }
326
327 /**
328 * Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be
329 * EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance
330 * of AtomicValue. If the value is a single item of any other kind, the result will be an instance
331 * of One. Otherwise, the result will typically be unchanged.
332 *
333 * @return the simplified sequence
334 */
335 public GroundedValue<T> reduce() {
336 int len = getLength();
337 if (len == 0) {
338 return EmptySequence.getInstance();
339 } else if (len == 1) {
340 return (GroundedValue<T>)itemAt(0);
341 } else {
342 return this;
343 }
344 }
345
346 /**
347 * Get the contents of this value in the form of a Java {@link java.lang.Iterable},
348 * so that it can be used in a for-each expression
349 * @return an Iterable containing the same sequence of items
350 */
351
352 public Iterable<T> asIterable() {
353 return value;
354 }
355
356
357 /**
358 * Get an iterator (a Java {@link java.util.Iterator}) over the items in this sequence.
359 * @return an iterator over the items in this sequence.
360 */
361
362 @Override
363 public Iterator<T> iterator() {
364 return value.iterator();
365 }
366 }
367