Debian Codegraph @

net/sf/saxon/ma/arrays/AbstractArrayItem.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.ma.arrays;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.sort.AtomicComparer;
12 import net.sf.saxon.functions.DeepEqual;
13 import net.sf.saxon.om.*;
14 import net.sf.saxon.query.AnnotationList;
15 import net.sf.saxon.trace.ExpressionPresenter;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.util.FastStringBuffer;
18 import net.sf.saxon.type.*;
19 import net.sf.saxon.value.AtomicValue;
20 import net.sf.saxon.value.Cardinality;
21 import net.sf.saxon.value.IntegerValue;
22 import net.sf.saxon.value.SequenceType;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /**
28 * An abstract implementation of XDM array items, containing methods that can be implemented generically.
29 */
30 public abstract class AbstractArrayItem implements ArrayItem {
31
32 private SequenceType memberType = null; // computed on demand
33
34 /**
35 * Get the roles of the arguments, for the purposes of streaming
36 *
37 * @return an array of OperandRole objects, one for each argument
38 */
39 public OperandRole[] getOperandRoles() {
40 return new OperandRole[]{OperandRole.SINGLE_ATOMIC};
41 }
42
43 /**
44 * Ask whether this function item is an array
45 *
46 * @return true (it is an array)
47 */
48 public boolean isArray() {
49 return true;
50 }
51
52 /**
53 * Ask whether this function item is a map
54 *
55 * @return false (it is not a map)
56 */
57 public boolean isMap() {
58 return false;
59 }
60
61 /**
62 * Atomize the item.
63 *
64 * @return the result of atomization
65 * @throws XPathException if atomization is not allowed for this kind of item
66 */
67 public AtomicSequence atomize() throws XPathException {
68 List<AtomicValue> list = new ArrayList<>(arrayLength());
69 for (GroundedValue<?> seq : members()) {
70 seq.iterate().forEachOrFail(item -> {
71 AtomicSequence atoms = item.atomize();
72 for (AtomicValue atom : atoms) {
73 list.add(atom);
74 }
75 });
76 }
77 return new AtomicArray(list);
78 }
79
80
81 /**
82 * Get the function annotations (as defined in XQuery). Returns an empty
83 * list if there are no function annotations.
84 *
85 * @return the function annotations
86 */
87
88 @Override
89 public AnnotationList getAnnotations() {
90 return AnnotationList.EMPTY;
91 }
92
93 /**
94 * Get the item type of the function item
95 *
96 * @return the function item's type
97 */
98
99 public FunctionItemType getFunctionItemType() {
100 return ArrayItemType.ANY_ARRAY_TYPE;
101 }
102
103 /**
104 * Get the name of the function, or null if it is anonymous
105 *
106 * @return the function name, or null for an anonymous function (which this one is)
107 */
108
109
110 public StructuredQName getFunctionName() {
111 return null;
112 }
113
114 /**
115 * Get a description of this function for use in error messages. For named functions, the description
116 * is the function name (as a lexical QName). For others, it might be, for example, "inline function",
117 * or "partially-applied ends-with function".
118 *
119 * @return a description of the function for use in error messages
120 */
121 public String getDescription() {
122 return "array";
123 }
124
125 /**
126 * Get the arity of the function
127 *
128 * @return the number of arguments in the function signature (in this case, 1 (one))
129 */
130
131 public int getArity() {
132 return 1;
133 }
134
135 /**
136 * Prepare an XPathContext object for evaluating the function
137 *
138 * @param callingContext the XPathContext of the function calling expression
139 * @param originator
140 * @return a suitable context for evaluating the function (which may or may
141 * not be the same as the caller's context)
142 */
143 public XPathContext makeNewContext(XPathContext callingContext, ContextOriginator originator) {
144 return callingContext;
145 }
146
147 /**
148 * Invoke the array in its role as a function
149 *
150 * @param context the XPath dynamic evaluation context
151 * @param args the actual arguments to be supplied (a single integer)
152 * @return the result of invoking the function
153 * @throws XPathException if a dynamic error occurs within the function
154 */
155
156 public GroundedValue<?> call(XPathContext context, Sequence[] args) throws XPathException {
157 IntegerValue subscript = (IntegerValue) args[0].head();
158 return get(ArrayFunctionSet.checkSubscript(subscript) - 1);
159 }
160
161 /**
162 * Test whether this array is deep-equal to another array,
163 * under the rules of the deep-equal function
164 *
165 * @param other the other function item
166 * @param context the dynamic evaluation context
167 * @param comparer the object to perform the comparison
168 * @param flags options for how the comparison is performed
169 * @return true if the two array items are deep-equal; false if they are not deep equal
170 * or if the other item is not an array
171 * @throws XPathException if the comparison cannot be performed
172 */
173
174
175 public boolean deepEquals(Function other, XPathContext context, AtomicComparer comparer, int flags) throws XPathException {
176 if (other instanceof ArrayItem) {
177 ArrayItem that = (ArrayItem) other;
178 if (this.arrayLength() != that.arrayLength()) {
179 return false;
180 }
181 for (int i = 0; i < this.arrayLength(); i++) {
182 if (!DeepEqual.deepEqual(this.get(i).iterate(), that.get(i).iterate(), comparer, context, flags)) {
183 return false;
184 }
185 }
186 return true;
187 } else {
188 return false;
189 }
190 }
191
192 /**
193 * Get the effective boolean value of this sequence
194 *
195 * @return the effective boolean value
196 * @throws XPathException if the sequence has no effective boolean value (for example a sequence of two integers)
197 */
198
199
200 public boolean effectiveBooleanValue() throws XPathException {
201 throw new XPathException("Effective boolean value is not defined for arrays", "FORG0006");
202 }
203
204 /**
205 * Get the value of the item as a string. For arrays, there is no string value,
206 * so an exception is thrown.
207 *
208 * @return the string value of the item
209 * @throws UnsupportedOperationException if the item is an array (an unchecked exception
210 * is used here to avoid introducing exception handling to a large number of paths where it is not
211 * needed)
212 * @see #getStringValueCS
213 * @since 8.4
214 */
215
216 public String getStringValue() {
217 throw new UnsupportedOperationException("An array does not have a string value");
218 }
219
220 /**
221 * Get the value of the item as a CharSequence. For arrays, there is no string value,
222 * so an exception is thrown.
223 *
224 * @return the string value of the item
225 * @throws UnsupportedOperationException if the item is an array (an unchecked exception
226 * is used here to avoid introducing exception handling to a large number of paths where it is not
227 * needed)
228 * @see #getStringValueCS
229 * @since 8.4
230 */
231
232 public CharSequence getStringValueCS() {
233 throw new UnsupportedOperationException("An array does not have a string value");
234 }
235
236 /**
237 * Output information about this function item to the diagnostic explain() output
238 */
239 public void export(ExpressionPresenter out) throws XPathException {
240 out.startElement("array");
241 out.emitAttribute("size", arrayLength() + "");
242 for (GroundedValue mem : members()) {
243 Literal.exportValue(mem, out);
244 }
245 out.endElement();
246 }
247
248 public boolean isTrustedResultType() {
249 return false;
250 }
251
252
253 /**
254 * Output a string representation of the array, suitable for diagnostics
255 */
256
257 public String toString() {
258 FastStringBuffer buffer = new FastStringBuffer(256);
259 buffer.append("[");
260 for (GroundedValue seq : members()) {
261 if (buffer.length() > 1) {
262 buffer.append(", ");
263 }
264 buffer.append(seq.toString());
265 }
266 buffer.append("]");
267 return buffer.toString();
268 }
269
270 /**
271 * Get the lowest common item type of the members of the array
272 *
273 * @return the most specific type to which all the members belong.
274 */
275
276 public SequenceType getMemberType(TypeHierarchy th) {
277 //try {
278 if (memberType == null) {
279 if (isEmpty()) {
280 memberType = SequenceType.makeSequenceType(ErrorType.getInstance(), StaticProperty.EXACTLY_ONE);
281 } else {
282 ItemType contentType = null;
283 int contentCard = StaticProperty.EXACTLY_ONE;
284 for (GroundedValue<?> s : members()) {
285 if (contentType == null) {
286 contentType = SequenceTool.getItemType(s, th);
287 contentCard = SequenceTool.getCardinality(s);
288 } else {
289 contentType = Type.getCommonSuperType(contentType, SequenceTool.getItemType(s, th));
290 contentCard = Cardinality.union(contentCard, SequenceTool.getCardinality(s));
291 }
292 }
293 memberType = SequenceType.makeSequenceType(contentType, contentCard);
294 }
295 }
296 return memberType;
297 // } catch (XPathException e) {
298 // return SequenceType.ANY_SEQUENCE;
299 // }
300 }
301
302 }
303