| 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.OperandRole; |
| 11 | import net.sf.saxon.om.*; |
| 12 | import net.sf.saxon.query.AnnotationList; |
| 13 | import net.sf.saxon.trans.Err; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | import net.sf.saxon.tree.util.FastStringBuffer; |
| 16 | import net.sf.saxon.value.ExternalObject; |
| 17 | import net.sf.saxon.value.SequenceType; |
| 18 | import net.sf.saxon.z.IntSet; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | /** |
| 24 | * A simple implementation of XDM array items, in which the array is backed by a Java List. |
| 25 | */ |
| 26 | public class SimpleArrayItem extends AbstractArrayItem implements ArrayItem { |
| 27 | |
| 28 | /** |
| 29 | * Static constant value representing an empty array |
| 30 | */ |
| 31 | |
| 32 | public static final SimpleArrayItem EMPTY_ARRAY = |
| 33 | new SimpleArrayItem(new ArrayList<>()); |
| 34 | |
| 35 | private List<GroundedValue<?>> members; |
| 36 | private boolean knownToBeGrounded = false; |
| 37 | private SequenceType memberType = null; // computed on demand |
| 38 | |
| 39 | /** |
| 40 | * Construct an array whose members are arbitrary sequences |
| 41 | * @param members the list of values (in general, each value is a sequence) to form the members of the array. |
| 42 | * The values must be repeatable sequences (not LazySequences); this is not checked. |
| 43 | */ |
| 44 | |
| 45 | public SimpleArrayItem(List<GroundedValue<?>> members) { |
| 46 | this.members = members; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Construct an array whose members are single items |
| 51 | * @param input an iterator over the items to make up the array |
| 52 | * @return an array in which each member is a single item, taken from the input sequence |
| 53 | * @throws XPathException if evaluating the SequenceIterator fails |
| 54 | */ |
| 55 | |
| 56 | public static SimpleArrayItem makeSimpleArrayItem(SequenceIterator<?> input) throws XPathException { |
| 57 | List<GroundedValue<?>> members = new ArrayList<>(); |
| 58 | input.forEachOrFail(item -> { |
| 59 | if (item.getClass().getName().equals("com.saxonica.functions.extfn.ArrayMemberValue")) { |
| 60 | members.add((GroundedValue<?>) ((ExternalObject) item).getObject()); |
| 61 | } else { |
| 62 | members.add(item); |
| 63 | } |
| 64 | }); |
| 65 | SimpleArrayItem result = new SimpleArrayItem(members); |
| 66 | result.knownToBeGrounded = true; |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the roles of the arguments, for the purposes of streaming |
| 72 | * |
| 73 | * @return an array of OperandRole objects, one for each argument |
| 74 | */ |
| 75 | public OperandRole[] getOperandRoles() { |
| 76 | return new OperandRole[]{OperandRole.SINGLE_ATOMIC}; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Ensure that all the members are grounded. The idea is that a member may |
| 81 | * initially be a reference to a lazily-evaluated sequence, but once computed, the |
| 82 | * reference will be replaced with the actual value |
| 83 | */ |
| 84 | |
| 85 | public void makeGrounded() throws XPathException { |
| 86 | if (!knownToBeGrounded) { |
| 87 | synchronized(this) { |
| 88 | for (int i=0; i<members.size(); i++) { |
| 89 | members.set(i, ((Sequence<?>) members.get(i)).materialize()); |
| 90 | } |
| 91 | knownToBeGrounded = true; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * Ask whether this function item is an array |
| 99 | * |
| 100 | * @return true (it is an array) |
| 101 | */ |
| 102 | public boolean isArray() { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Ask whether this function item is a map |
| 108 | * |
| 109 | * @return false (it is not a map) |
| 110 | */ |
| 111 | public boolean isMap() { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the function annotations (as defined in XQuery). Returns an empty |
| 117 | * list if there are no function annotations. |
| 118 | * |
| 119 | * @return the function annotations |
| 120 | */ |
| 121 | |
| 122 | @Override |
| 123 | public AnnotationList getAnnotations() { |
| 124 | return AnnotationList.EMPTY; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get a member of the array |
| 129 | * |
| 130 | * @param index the position of the member to retrieve (zero-based) |
| 131 | * @return the value at the given position. |
| 132 | * @throws XPathException if the index is out of range |
| 133 | */ |
| 134 | |
| 135 | |
| 136 | public GroundedValue<?> get(int index) throws XPathException { |
| 137 | if (index < 0 || index >= members.size()) { |
| 138 | throw new XPathException("Array index (" + (index+1) + ") out of range (1 to " + members.size() + ")", "FOAY0001"); |
| 139 | } |
| 140 | return members.get(index); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Replace a member of the array |
| 145 | * |
| 146 | * @param index the position of the member to replace (zero-based) |
| 147 | * @param newValue the replacement value |
| 148 | * @return the value at the given position. |
| 149 | * @throws XPathException if the index is out of range |
| 150 | */ |
| 151 | @Override |
| 152 | public ArrayItem put(int index, GroundedValue newValue) throws XPathException { |
| 153 | ImmutableArrayItem a2 = new ImmutableArrayItem(this); |
| 154 | return a2.put(index, newValue); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get the size of the array |
| 159 | * |
| 160 | * @return the number of members in this array |
| 161 | */ |
| 162 | |
| 163 | public int arrayLength() { |
| 164 | return members.size(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Ask whether the array is empty |
| 169 | * |
| 170 | * @return true if and only if the size of the array is zero |
| 171 | */ |
| 172 | |
| 173 | public boolean isEmpty() { |
| 174 | return members.isEmpty(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Get the list of all members of the array |
| 179 | * |
| 180 | * @return an iterator over the members of the array |
| 181 | */ |
| 182 | |
| 183 | public Iterable<GroundedValue<?>> members() { |
| 184 | return members; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Remove zero or more members from the array |
| 189 | * |
| 190 | * @param positions the positions of the members to be removed (zero-based). |
| 191 | * A value that is out of range is ignored. |
| 192 | * @return a new array in which the requested member has been removed |
| 193 | */ |
| 194 | |
| 195 | public ArrayItem removeSeveral(IntSet positions) { |
| 196 | ImmutableArrayItem a2 = new ImmutableArrayItem(this); |
| 197 | return a2.removeSeveral(positions); |
| 198 | // List<Sequence> newList = new ArrayList<Sequence>(members.size() - 1); |
| 199 | // for (int i = 0; i < members.size(); i++) { |
| 200 | // if (!positions.contains(i)) { |
| 201 | // newList.add(members.get(i)); |
| 202 | // } |
| 203 | // } |
| 204 | // SimpleArrayItem result = new SimpleArrayItem(newList); |
| 205 | // if (knownToBeGrounded) { |
| 206 | // result.knownToBeGrounded = true; |
| 207 | // } |
| 208 | // result.memberType = memberType; |
| 209 | // return result; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Remove a member from the array |
| 214 | * |
| 215 | * @param pos the position of the member to be removed (zero-based). A value |
| 216 | * that is out of range results in an IndexOutOfBoundsException |
| 217 | * @return a new array in which the requested member has been removed |
| 218 | */ |
| 219 | |
| 220 | public ArrayItem remove(int pos) { |
| 221 | ImmutableArrayItem a2 = new ImmutableArrayItem(this); |
| 222 | return a2.remove(pos); |
| 223 | // List<Sequence> newList = new ArrayList<Sequence>(members.size() - 1); |
| 224 | // newList.addAll(members.subList(0, pos)); |
| 225 | // newList.addAll(members.subList(pos + 1, members.size())); |
| 226 | // SimpleArrayItem result = new SimpleArrayItem(newList); |
| 227 | // if (knownToBeGrounded) { |
| 228 | // result.knownToBeGrounded = true; |
| 229 | // } |
| 230 | // result.memberType = memberType; |
| 231 | // return result; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get a subarray given a start and end position |
| 236 | * |
| 237 | * @param start the start position (zero based) |
| 238 | * @param end the end position (the position of the first item not to be returned) |
| 239 | * (zero based) |
| 240 | * @throws IndexOutOfBoundsException if start, or start+end, is out of range |
| 241 | */ |
| 242 | @Override |
| 243 | public ArrayItem subArray(int start, int end) { |
| 244 | return new SimpleArrayItem(members.subList(start, end)); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Insert a new member into an array |
| 249 | * |
| 250 | * @param position the 0-based position that the new item will assume |
| 251 | * @param member the new member to be inserted |
| 252 | * @return a new array item with the new member inserted |
| 253 | * @throws IndexOutOfBoundsException if position is out of range |
| 254 | */ |
| 255 | @Override |
| 256 | public ArrayItem insert(int position, GroundedValue<?> member) { |
| 257 | ImmutableArrayItem a2 = new ImmutableArrayItem(this); |
| 258 | return a2.insert(position, member); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Concatenate this array with another |
| 263 | * |
| 264 | * @param other the second array |
| 265 | * @return the concatenation of the two arrays; that is, an array |
| 266 | * containing first the members of this array, and then the members of the other array |
| 267 | */ |
| 268 | |
| 269 | public ArrayItem concat(ArrayItem other) { |
| 270 | ImmutableArrayItem a2 = new ImmutableArrayItem(this); |
| 271 | return a2.concat(other); |
| 272 | // List<Sequence> newList = new ArrayList<Sequence>(members.size() + other.arrayLength()); |
| 273 | // newList.addAll(members); |
| 274 | // for (Sequence s : other) { |
| 275 | // newList.add(s); |
| 276 | // } |
| 277 | // SimpleArrayItem result = new SimpleArrayItem(newList); |
| 278 | // if (other instanceof SimpleArrayItem) { |
| 279 | // if (knownToBeGrounded && ((SimpleArrayItem) other).knownToBeGrounded) { |
| 280 | // result.knownToBeGrounded = true; |
| 281 | // } |
| 282 | // if (memberType != null && memberType.equals(((SimpleArrayItem) other).memberType)) { |
| 283 | // result.memberType = memberType; |
| 284 | // } |
| 285 | // } |
| 286 | // return result; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /** |
| 291 | * Get a list of the members of the array |
| 292 | * |
| 293 | * @return the list of members. Note that this returns the actual contained member array, and this is |
| 294 | * mutable. Changes to this array are permitted only if the caller knows what they are doing, for example |
| 295 | * during initial construction of an array that will not be presented to the user until construction |
| 296 | * has finished. |
| 297 | */ |
| 298 | |
| 299 | public List<GroundedValue<?>> getMembers() { |
| 300 | return members; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Provide a short string showing the contents of the item, suitable |
| 305 | * for use in error messages |
| 306 | * |
| 307 | * @return a depiction of the item suitable for use in error messages |
| 308 | */ |
| 309 | @Override |
| 310 | public String toShortString() { |
| 311 | int size = getLength(); |
| 312 | if (size == 0) { |
| 313 | return "[]"; |
| 314 | } else if (size > 5) { |
| 315 | return "[(:size " + size + ":)]"; |
| 316 | } else { |
| 317 | FastStringBuffer buff = new FastStringBuffer(256); |
| 318 | buff.append("["); |
| 319 | for (GroundedValue<?> entry : members()) { |
| 320 | buff.append(Err.depictSequence(entry).toString().trim()); |
| 321 | buff.append(", "); |
| 322 | } |
| 323 | if (size == 1) { |
| 324 | buff.append("]"); |
| 325 | } else { |
| 326 | buff.setCharAt(buff.length() - 2, ']'); |
| 327 | } |
| 328 | return buff.toString().trim(); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 |