Debian Codegraph @

net/sf/saxon/om/AtomicArray.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.AtomicIterator;
13 import net.sf.saxon.tree.iter.ListIterator;
14 import net.sf.saxon.tree.iter.UnfailingIterator;
15 import net.sf.saxon.tree.util.FastStringBuffer;
16 import net.sf.saxon.value.AtomicValue;
17 import net.sf.saxon.value.EmptySequence;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23
24 /**
25 * A sequence of atomic values, implemented using an underlying arrayList.
26 * <p>Often used for representing the typed value of a list-valued node.</p>
27 *
28 * @since 9.5
29 */
30 public class AtomicArray implements AtomicSequence {
31
32 private static List<AtomicValue> emptyAtomicList = Collections.emptyList();
33 public static AtomicArray EMPTY_ATOMIC_ARRAY = new AtomicArray(emptyAtomicList);
34
35 private List<AtomicValue> content;
36
37 /**
38 * Create an AtomicArray over a supplied arrayList of atomic values
39 *
40 * @param content the supplied arrayList. The caller warrants that the contents of this array will not change.
41 */
42
43 public AtomicArray(List<AtomicValue> content) {
44 this.content = content;
45 }
46
47 /**
48 * Create an AtomicArray supplying the contents as an iterator
49 *
50 * @param iter the iterator that supplies the atomic values (which must be position
51 * at the start of the sequence, and which will be consumed by the method).
52 * @throws XPathException if evaluation of the SequenceIterator fails
53 * @throws ClassCastException if any of the items returned by the SequenceIterator is not atomic
54 */
55
56 public AtomicArray(SequenceIterator<? extends AtomicValue> iter) throws XPathException {
57 ArrayList<AtomicValue> list = new ArrayList<>(10);
58 iter.forEachOrFail(list::add);
59 content = list;
60 }
61
62 public AtomicValue head() {
63 return content.isEmpty() ? null : content.get(0);
64 }
65
66 public AtomicIterator iterate() {
67 return new ListIterator.Atomic(content);
68 }
69
70 /**
71 * Get the n'th item in the sequence (base-zero addressing)
72 *
73 * @param n the index of the required item, the first item being zero
74 * @return the n'th item if n is in range, or null otherwise
75 */
76
77 public AtomicValue itemAt(int n) {
78 if (n >= 0 && n < content.size()) {
79 return content.get(n);
80 } else {
81 return null;
82 }
83 }
84
85 /**
86 * Get the length of the sequence
87 *
88 * @return the number of items in the sequence
89 */
90
91 public int getLength() {
92 return content.size();
93 }
94
95 /**
96 * Get a subsequence of this sequence
97 *
98 * @param start the index of the first item to be included in the result, counting from zero.
99 * A negative value is taken as zero. If the value is beyond the end of the sequence, an empty
100 * sequence is returned
101 * @param length the number of items to be included in the result. Specify Integer.MAX_VALUE to
102 * get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence
103 * is returned. If the value goes off the end of the sequence, the result returns items up to the end
104 * of the sequence
105 * @return the required subsequence
106 */
107
108 public AtomicArray subsequence(int start, int length) {
109 if (start < 0) {
110 start = 0;
111 }
112 if (start + length > content.size()) {
113 length = content.size() - start;
114 }
115 return new AtomicArray(content.subList(start, start + length));
116 }
117
118 /**
119 * Get the canonical lexical representation as defined in XML Schema. This is not always the same
120 * as the result of casting to a string according to the XPath rules.
121 *
122 * @return the canonical lexical representation if defined in XML Schema; otherwise, the result
123 * of casting to string according to the XPath 2.0 rules
124 */
125
126 public CharSequence getCanonicalLexicalRepresentation() {
127 return getStringValueCS();
128 }
129
130 /**
131 * Get the value of the item as a CharSequence. This is in some cases more efficient than
132 * the version of the method that returns a String.
133 */
134
135 public CharSequence getStringValueCS() {
136 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
137 boolean first = true;
138 for (AtomicValue av : content) {
139 if (!first) {
140 fsb.append(' ');
141 } else {
142 first = false;
143 }
144 fsb.append(av.getStringValueCS());
145 }
146 return fsb.condense();
147 }
148
149 public String getStringValue() {
150 return getStringValueCS().toString();
151 }
152
153 public boolean effectiveBooleanValue() throws XPathException {
154 return ExpressionTool.effectiveBooleanValue(iterate());
155 }
156
157 /**
158 * Get a Comparable value that implements the XML Schema ordering comparison semantics for this value.
159 * The default implementation is written to compare sequences of atomic values.
160 * This method is overridden for AtomicValue and its subclasses.
161 * <p>In the case of data types that are partially ordered, the returned Comparable extends the standard
162 * semantics of the compareTo() method by returning the value {@link SequenceTool#INDETERMINATE_ORDERING} when there
163 * is no defined order relationship between two given values.</p>
164 * <p>For comparing key/keyref values, XSD 1.1 defines that a singleton list is equal to its only member. To
165 * achieve this, this method returns the schema comparable of the singleton member if the list has length one.
166 * This won't give the correct ordering semantics, but we rely on lists never taking part in ordering comparisons.</p>
167 *
168 * @return a Comparable that follows XML Schema comparison rules
169 */
170
171 public Comparable getSchemaComparable() {
172 if (content.size() == 1) {
173 return content.get(0).getSchemaComparable();
174 } else {
175 return new ValueSchemaComparable();
176 }
177 }
178
179 private class ValueSchemaComparable implements Comparable<ValueSchemaComparable> {
180 public AtomicArray getValue() {
181 return AtomicArray.this;
182 }
183
184 public int compareTo(ValueSchemaComparable obj) {
185 UnfailingIterator iter1 = getValue().iterate();
186 UnfailingIterator iter2 = obj.getValue().iterate();
187 while (true) {
188 AtomicValue item1 = (AtomicValue) iter1.next();
189 AtomicValue item2 = (AtomicValue) iter2.next();
190 if (item1 == null && item2 == null) {
191 return 0;
192 }
193 if (item1 == null) {
194 return -1;
195 } else if (item2 == null) {
196 return +1;
197 }
198 int c = item1.getSchemaComparable().compareTo(item2.getSchemaComparable());
199 if (c != 0) {
200 return c;
201 }
202 }
203 }
204
205 public boolean equals(/*@NotNull*/ Object obj) {
206 return ValueSchemaComparable.class.isAssignableFrom(obj.getClass())
207 && compareTo((ValueSchemaComparable) obj) == 0;
208 }
209
210 public int hashCode() {
211 try {
212 int hash = 0x06639662; // arbitrary seed
213 SequenceIterator iter = getValue().iterate();
214 while (true) {
215 Item item = iter.next();
216 if (item == null) {
217 return hash;
218 }
219 if (item instanceof AtomicValue) {
220 hash ^= ((AtomicValue) item).getSchemaComparable().hashCode();
221 }
222 }
223 } catch (XPathException e) {
224 return 0;
225 }
226 }
227 }
228
229 /**
230 * Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be
231 * EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance
232 * of AtomicValue. If the value is a single item of any other kind, the result will be an instance
233 * of SingletonItem. Otherwise, the result will typically be unchanged.
234 *
235 * @return the simplified sequence
236 */
237 public GroundedValue<AtomicValue> reduce() {
238 int len = getLength();
239 if (len == 0) {
240 return EmptySequence.getInstance();
241 } else if (len == 1) {
242 return (AtomicValue)itemAt(0);
243 } else {
244 return this;
245 }
246 }
247
248 /**
249 * Returns a Java iterator over the atomic sequence.
250 *
251 * @return an Iterator.
252 */
253
254 public Iterator<AtomicValue> iterator() {
255 return content.iterator();
256 }
257 }
258