| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.ma.arrays.ArrayFunctionSet; |
| 11 | import net.sf.saxon.ma.arrays.ArrayItem; |
| 12 | import net.sf.saxon.ma.arrays.SimpleArrayItem; |
| 13 | import net.sf.saxon.om.GroundedValue; |
| 14 | import net.sf.saxon.om.Sequence; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.Iterator; |
| 19 | import java.util.List; |
| 20 | |
| 21 | /** |
| 22 | * An array in the XDM data model. An array is a list of zero or more members, each of which |
| 23 | * is an arbitrary XDM value. The array itself is an XDM item. |
| 24 | * <p>An XdmArray is immutable.</p> |
| 25 | * |
| 26 | * @since 9.8 |
| 27 | */ |
| 28 | |
| 29 | public class XdmArray extends XdmFunctionItem { |
| 30 | |
| 31 | /** |
| 32 | * Create an empty XdmArray |
| 33 | */ |
| 34 | |
| 35 | public XdmArray() { |
| 36 | setValue(SimpleArrayItem.EMPTY_ARRAY); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Create an XdmArray that wraps a supplied ArrayItem |
| 41 | * @param array the ArrayItem to be encapsulated |
| 42 | */ |
| 43 | |
| 44 | public XdmArray(ArrayItem array) { |
| 45 | setValue(array); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Create an XdmArray supplying the members as an array of XdmValue objects |
| 50 | * |
| 51 | * @param members an array of XdmValue objects. Note that subsequent changes to the array will have no effect |
| 52 | * on the XdmValue. |
| 53 | */ |
| 54 | |
| 55 | public XdmArray(XdmValue[] members) { |
| 56 | List<GroundedValue<?>> values = new ArrayList<>(); |
| 57 | for (XdmValue member : members) { |
| 58 | values.add(member.getUnderlyingValue()); |
| 59 | } |
| 60 | setValue(new SimpleArrayItem(values)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Create an XdmArray supplying the members as a collection of XdmValue objects |
| 65 | * |
| 66 | * @param members a sequence of XdmValue objects. Note that if this is supplied as a list or similar |
| 67 | * collection, subsequent changes to the list/collection will have no effect on the XdmValue. |
| 68 | * Note that the argument can be a single XdmValue representing a sequence, in which case the |
| 69 | * constructed array will have one member for each item in the supplied sequence. |
| 70 | */ |
| 71 | |
| 72 | public XdmArray(Iterable<? extends XdmValue> members) { |
| 73 | List<GroundedValue<?>> values = new ArrayList<>(); |
| 74 | for (XdmValue member : members) { |
| 75 | values.add(member.getUnderlyingValue()); |
| 76 | } |
| 77 | setValue(new SimpleArrayItem(values)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the number of members in the array |
| 82 | * |
| 83 | * @return the number of members in the array. (Note that the {@link #size()} method returns 1 (one), |
| 84 | * because an XDM array is an item.) |
| 85 | */ |
| 86 | |
| 87 | public int arrayLength() { |
| 88 | return getUnderlyingValue().arrayLength(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the n'th member in the array, counting from zero. |
| 93 | * |
| 94 | * @param n the member that is required, counting the first member in the array as member zero |
| 95 | * @return the n'th member in the sequence making up the array, counting from zero |
| 96 | * @throws IndexOutOfBoundsException if n is less than zero or greater than or equal to the number |
| 97 | * of members in the array |
| 98 | */ |
| 99 | |
| 100 | public XdmValue get(int n) throws IndexOutOfBoundsException { |
| 101 | try { |
| 102 | Sequence<?> member = getUnderlyingValue().get(n); |
| 103 | return XdmValue.wrap(member); |
| 104 | } catch (XPathException e) { |
| 105 | throw new IndexOutOfBoundsException(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Create a new array in which one member is replaced with a new value. |
| 111 | * |
| 112 | * @param n the position of the member that is to be replaced, counting the first member |
| 113 | * in the array as member zero |
| 114 | * @param value the new value for this member |
| 115 | * @return a new array, the same length as the original, with one member replaced |
| 116 | * by a new value |
| 117 | * @throws IndexOutOfBoundsException if n is less than zero or greater than or equal to the number |
| 118 | * of members in the array |
| 119 | */ |
| 120 | |
| 121 | public XdmArray put(int n, XdmValue value) throws IndexOutOfBoundsException { |
| 122 | try { |
| 123 | GroundedValue member = value.getUnderlyingValue(); |
| 124 | return (XdmArray)XdmValue.wrap(getUnderlyingValue().put(n, member)); |
| 125 | } catch (XPathException e) { |
| 126 | throw new IndexOutOfBoundsException(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Append a new member to an array |
| 132 | * @param value the new member |
| 133 | * @return a new array, one item longer than the original |
| 134 | * @throws SaxonApiUncheckedException if the value is lazily evaluated, and evaluation fails |
| 135 | * @since 9.9. (See bug 3968: on first release of 9.9, the method was mistakenly named <code>append</code>). |
| 136 | */ |
| 137 | |
| 138 | public XdmArray addMember(XdmValue value) { |
| 139 | try { |
| 140 | GroundedValue member = value.getUnderlyingValue(); |
| 141 | ArrayItem newArray = ArrayFunctionSet.ArrayAppend.append(getUnderlyingValue(), member); |
| 142 | return (XdmArray) XdmValue.wrap(newArray); |
| 143 | } catch (XPathException e) { |
| 144 | throw new SaxonApiUncheckedException(e); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Concatenate another array |
| 150 | * |
| 151 | * @param value the other array |
| 152 | * @return a new array, containing the members of this array followed by the members of the |
| 153 | * other array |
| 154 | * @since 9.9 |
| 155 | */ |
| 156 | |
| 157 | public XdmArray concat(XdmArray value) { |
| 158 | ArrayItem other = value.getUnderlyingValue(); |
| 159 | ArrayItem newArray = getUnderlyingValue().concat(other); |
| 160 | return (XdmArray) XdmValue.wrap(newArray); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Get the members of the array in the form of a list. |
| 166 | * @return a list of the members of this array. |
| 167 | */ |
| 168 | public List<XdmValue> asList() { |
| 169 | Iterator<GroundedValue<?>> members = getUnderlyingValue().members().iterator(); |
| 170 | List<XdmValue> result = new ArrayList<XdmValue>(getUnderlyingValue().getLength()); |
| 171 | while (members.hasNext()) { |
| 172 | result.add(XdmValue.wrap(members.next())); |
| 173 | } |
| 174 | return result; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Get the underlying implementation object representing the value. This method allows |
| 179 | * access to lower-level Saxon functionality, including classes and methods that offer |
| 180 | * no guarantee of stability across releases. |
| 181 | * |
| 182 | * @return the underlying implementation object representing the value |
| 183 | */ |
| 184 | @Override |
| 185 | public ArrayItem getUnderlyingValue() { |
| 186 | return (ArrayItem)super.getUnderlyingValue(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Make an XDM array from a Java array. Each member of the supplied array |
| 191 | * is converted to a single member in the result array using the method |
| 192 | * {@link XdmValue#makeValue(Object)} |
| 193 | * |
| 194 | * @return the result of the conversion if successful |
| 195 | * @throws IllegalArgumentException if conversion is not possible |
| 196 | */ |
| 197 | |
| 198 | public static XdmArray makeArray(Object[] input) throws IllegalArgumentException { |
| 199 | List<XdmValue> result = new ArrayList<XdmValue>(input.length); |
| 200 | for (Object o : input) { |
| 201 | result.add(XdmValue.makeValue(o)); |
| 202 | } |
| 203 | return new XdmArray(result); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Make an XdmArray whose members are xs:boolean values |
| 208 | * @param input the input array of booleans |
| 209 | * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input |
| 210 | */ |
| 211 | |
| 212 | public static XdmArray makeArray(boolean[] input) { |
| 213 | List<XdmValue> result = new ArrayList<XdmValue>(input.length); |
| 214 | for (boolean o : input) { |
| 215 | result.add(new XdmAtomicValue(o)); |
| 216 | } |
| 217 | return new XdmArray(result); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Make an XdmArray whose members are xs:long values |
| 222 | * |
| 223 | * @param input the input array of integers |
| 224 | * @return an XdmArray whose members are xs:integer values corresponding one-to-one with the input |
| 225 | */ |
| 226 | |
| 227 | public static XdmArray makeArray(long[] input) { |
| 228 | List<XdmValue> result = new ArrayList<XdmValue>(input.length); |
| 229 | for (long o : input) { |
| 230 | result.add(new XdmAtomicValue(o)); |
| 231 | } |
| 232 | return new XdmArray(result); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Make an XdmArray whose members are xs:integer values |
| 237 | * |
| 238 | * @param input the input array of integers |
| 239 | * @return an XdmArray whose members are xs:integer values corresponding one-to-one with the input |
| 240 | */ |
| 241 | |
| 242 | public static XdmArray makeArray(int[] input) { |
| 243 | List<XdmValue> result = new ArrayList<XdmValue>(input.length); |
| 244 | for (int o : input) { |
| 245 | result.add(new XdmAtomicValue(o)); |
| 246 | } |
| 247 | return new XdmArray(result); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Make an XdmArray whose members are xs:integer values |
| 252 | * |
| 253 | * @param input the input array of integers |
| 254 | * @return an XdmArray whose members are xs:integer values corresponding one-to-one with the input |
| 255 | */ |
| 256 | |
| 257 | public static XdmArray makeArray(short[] input) { |
| 258 | List<XdmValue> result = new ArrayList<XdmValue>(input.length); |
| 259 | for (short o : input) { |
| 260 | result.add(new XdmAtomicValue(o)); |
| 261 | } |
| 262 | return new XdmArray(result); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Make an XdmArray whose members are xs:integer values |
| 267 | * |
| 268 | * @param input the input array of integers |
| 269 | * @return an XdmArray whose members are xs:integer values corresponding one-to-one with the input |
| 270 | */ |
| 271 | |
| 272 | public static XdmArray makeArray(byte[] input) { |
| 273 | List<XdmValue> result = new ArrayList<XdmValue>(input.length); |
| 274 | for (byte o : input) { |
| 275 | result.add(new XdmAtomicValue(o)); |
| 276 | } |
| 277 | return new XdmArray(result); |
| 278 | } |
| 279 | |
| 280 | } |
| 281 |