Debian Codegraph @

net/sf/saxon/om/ZeroOrMore.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.om;
9
10 import net.sf.saxon.expr.parser.ExpressionTool;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.tree.iter.ListIterator;
13 import net.sf.saxon.tree.iter.UnfailingIterator;
14 import net.sf.saxon.value.AtomicValue;
15 import net.sf.saxon.value.EmptySequence;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20
21 /**
22 * A value that is a sequence containing one or more items. The main use is in declarations of reflexive extension
23 * functions, where declaring an argument of type <ZeroOrMore<IntegerValue>> triggers automatic type
24 * checking in the same way as for a native XSLT/XQuery function declaring the type as xs:integer*.
25 */
26
27 public class ZeroOrMore<T extends Item<?>> implements GroundedValue<T> {
28
29 private List<T> content;
30
31 /**
32 * Create a sequence containing zero or one items
33 *
34 * @param content The content of the sequence
35 */
36
37 public ZeroOrMore(T[] content) {
38 this.content = Arrays.asList(content);
39 }
40
41 public ZeroOrMore(List<T> content) {
42 this.content = content;
43 }
44
45 public ZeroOrMore(SequenceIterator<? extends T> iter) throws XPathException {
46 content = new ArrayList<T>();
47 iter.forEachOrFail(content::add);
48 }
49
50 public T head() {
51 return content.isEmpty() ? null : content.get(0);
52 }
53
54 public UnfailingIterator<T> iterate() {
55 return new ListIterator<>(content);
56 }
57
58 /**
59 * Get the n'th item in the value, counting from 0
60 *
61 * @param n the index of the required item, with 0 representing the first item in the sequence
62 * @return the n'th item if it exists, or null otherwise
63 */
64 public T itemAt(int n) {
65 if (n >= 0 && n < content.size()) {
66 return content.get(n);
67 } else {
68 return null;
69 }
70 }
71
72 /**
73 * Get a subsequence of the value
74 *
75 * @param start the index of the first item to be included in the result, counting from zero.
76 * A negative value is taken as zero. If the value is beyond the end of the sequence, an empty
77 * sequence is returned
78 * @param length the number of items to be included in the result. Specify Integer.MAX_VALUE to
79 * get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence
80 * is returned. If the value goes off the end of the sequence, the result returns items up to the end
81 * of the sequence
82 * @return the required subsequence.
83 */
84 public GroundedValue<T> subsequence(int start, int length) {
85 if (start < 0) {
86 start = 0;
87 }
88 if (start + length > content.size()) {
89 length = content.size() - start;
90 }
91 return new ZeroOrMore<T>(content.subList(start, start+ length));
92 }
93
94 /**
95 * Get the size of the value (the number of items)
96 *
97 * @return the number of items in the sequence
98 */
99 public int getLength() {
100 return content.size();
101 }
102
103 /**
104 * Get the effective boolean value of this sequence
105 *
106 * @return the effective boolean value
107 * @throws net.sf.saxon.trans.XPathException
108 * if the sequence has no effective boolean value (for example a sequence of two integers)
109 */
110 public boolean effectiveBooleanValue() throws XPathException {
111 return ExpressionTool.effectiveBooleanValue(iterate());
112 }
113
114 /**
115 * Get the string value of this sequence. The string value of an item is the result of applying the string()
116 * function. The string value of a sequence is the space-separated result of applying the string-join() function
117 * using a single space as the separator
118 *
119 * @return the string value of the sequence.
120 * @throws net.sf.saxon.trans.XPathException
121 * if the sequence contains items that have no string value (for example, function items)
122 */
123 public String getStringValue() throws XPathException {
124 return SequenceTool.getStringValue(this);
125 }
126
127 /**
128 * Get the string value of this sequence. The string value of an item is the result of applying the string()
129 * function. The string value of a sequence is the space-separated result of applying the string-join() function
130 * using a single space as the separator
131 *
132 * @return the string value of the sequence.
133 * @throws net.sf.saxon.trans.XPathException
134 * if the sequence contains items that have no string value (for example, function items)
135 */
136 public CharSequence getStringValueCS() throws XPathException {
137 return SequenceTool.getStringValue(this);
138 }
139
140 /**
141 * Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be
142 * EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance
143 * of {@link AtomicValue}. If the value is a single item of any other kind, the result will be an instance
144 * of {@link One}. Otherwise, the result will typically be unchanged.
145 *
146 * @return the simplified sequence
147 */
148 public GroundedValue<T> reduce() {
149 if (content.isEmpty()) {
150 return EmptySequence.getInstance();
151 } else if (content.size() == 1) {
152 T first = content.get(0);
153 if (first instanceof AtomicValue) {
154 return (GroundedValue<T>)first;
155 } else {
156 return new One<T>(head());
157 }
158 }
159 return this;
160 }
161 }