| 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.StaticProperty; |
| 11 | import net.sf.saxon.om.Function; |
| 12 | import net.sf.saxon.om.Genre; |
| 13 | import net.sf.saxon.om.GroundedValue; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | import net.sf.saxon.type.TypeHierarchy; |
| 16 | import net.sf.saxon.value.SequenceType; |
| 17 | import net.sf.saxon.z.IntSet; |
| 18 | |
| 19 | /** |
| 20 | * Interface supported by different implementations of an XDM array item |
| 21 | */ |
| 22 | public interface ArrayItem extends Function { |
| 23 | |
| 24 | SequenceType SINGLE_ARRAY_TYPE = |
| 25 | SequenceType.makeSequenceType(ArrayItemType.ANY_ARRAY_TYPE, StaticProperty.EXACTLY_ONE); |
| 26 | |
| 27 | /** |
| 28 | * Get a member of the array |
| 29 | * |
| 30 | * @param index the position of the member to retrieve (zero-based) |
| 31 | * @return the value at the given position. |
| 32 | * @throws XPathException if the index is out of range |
| 33 | */ |
| 34 | |
| 35 | GroundedValue<?> get(int index) throws XPathException; |
| 36 | |
| 37 | /** |
| 38 | * Replace a member of the array |
| 39 | * |
| 40 | * @param index the position of the member to replace (zero-based) |
| 41 | * @param newValue the replacement value |
| 42 | * @return the value at the given position. |
| 43 | * @throws XPathException if the index is out of range |
| 44 | */ |
| 45 | |
| 46 | ArrayItem put(int index, GroundedValue<?> newValue) throws XPathException; |
| 47 | |
| 48 | /** |
| 49 | * Get the number of members in the array |
| 50 | * |
| 51 | * <p>Note: the {@link #getLength() method always returns 1, because an array is an item}</p> |
| 52 | * |
| 53 | * @return the number of members in this array. |
| 54 | */ |
| 55 | |
| 56 | int arrayLength(); |
| 57 | |
| 58 | /** |
| 59 | * Ask whether the array is empty |
| 60 | * |
| 61 | * @return true if and only if the size of the array is zero |
| 62 | */ |
| 63 | |
| 64 | boolean isEmpty(); |
| 65 | |
| 66 | /** |
| 67 | * Get the list of all members of the array |
| 68 | * @return an iterator over the members of the array |
| 69 | */ |
| 70 | |
| 71 | Iterable<GroundedValue<?>> members(); |
| 72 | |
| 73 | /** |
| 74 | * Concatenate this array with another |
| 75 | * @param other the second array |
| 76 | * @return the concatenation of the two arrays; that is, an array |
| 77 | * containing first the members of this array, and then the members of the other array |
| 78 | */ |
| 79 | |
| 80 | ArrayItem concat(ArrayItem other); |
| 81 | |
| 82 | /** |
| 83 | * Remove a member from the array |
| 84 | * |
| 85 | * |
| 86 | * @param index the position of the member to be removed (zero-based) |
| 87 | * @return a new array in which the requested member has been removed. |
| 88 | * @throws IndexOutOfBoundsException if index is out of range |
| 89 | */ |
| 90 | |
| 91 | ArrayItem remove(int index); |
| 92 | |
| 93 | /** |
| 94 | * Remove zero or more members from the array |
| 95 | * |
| 96 | * @param positions the positions of the members to be removed (zero-based). |
| 97 | * A value that is out of range is ignored. |
| 98 | * @return a new array in which the requested member has been removed |
| 99 | */ |
| 100 | |
| 101 | ArrayItem removeSeveral(IntSet positions); |
| 102 | |
| 103 | /** |
| 104 | * Get a sub-array given a start and end position |
| 105 | * @param start the start position (zero based) |
| 106 | * @param end the end position (the position of the first item not to be returned) |
| 107 | * (zero based) |
| 108 | * @return a new array item containing the sub-array |
| 109 | * @throws IndexOutOfBoundsException if start, or start+end, is out of range |
| 110 | */ |
| 111 | |
| 112 | ArrayItem subArray(int start, int end); |
| 113 | |
| 114 | /** |
| 115 | * Insert a new member into an array |
| 116 | * @param position the 0-based position that the new item will assume |
| 117 | * @param member the new member to be inserted |
| 118 | * @throws IndexOutOfBoundsException if position is out of range |
| 119 | * @return a new array item with the new member inserted |
| 120 | */ |
| 121 | |
| 122 | ArrayItem insert(int position, GroundedValue<?> member); |
| 123 | |
| 124 | /** |
| 125 | * Get the lowest common item type of the members of the array |
| 126 | * |
| 127 | * @return the most specific type to which all the members belong. |
| 128 | */ |
| 129 | |
| 130 | SequenceType getMemberType(TypeHierarchy th); |
| 131 | |
| 132 | /** |
| 133 | * Provide a short string showing the contents of the item, suitable |
| 134 | * for use in error messages |
| 135 | * |
| 136 | * @return a depiction of the item suitable for use in error messages |
| 137 | */ |
| 138 | @Override |
| 139 | default String toShortString() { |
| 140 | StringBuilder sb = new StringBuilder(); |
| 141 | sb.append("array{"); |
| 142 | int count = 0; |
| 143 | for (GroundedValue member : members()) { |
| 144 | if (count++ > 2) { |
| 145 | sb.append(" ..."); |
| 146 | break; |
| 147 | } |
| 148 | sb.append(member.toShortString()); |
| 149 | sb.append(", "); |
| 150 | } |
| 151 | sb.append("}"); |
| 152 | return sb.toString(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get the genre of this item |
| 157 | * |
| 158 | * @return the genre: specifically, {@link Genre#ARRAY}. |
| 159 | */ |
| 160 | @Override |
| 161 | default Genre getGenre() { |
| 162 | return Genre.ARRAY; |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | // Copyright (c) 2014-2018 Saxonica Limited. |