| 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.Err; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | import net.sf.saxon.tree.iter.UnfailingIterator; |
| 14 | |
| 15 | import java.util.Iterator; |
| 16 | |
| 17 | /** |
| 18 | * A value that exists in memory and that can be directly addressed |
| 19 | * @since 9.5. Generified in 9.9. |
| 20 | */ |
| 21 | public interface GroundedValue<T extends Item<?>> extends Sequence<T>, Iterable<T> { |
| 22 | |
| 23 | /** |
| 24 | * Get an iterator over all the items in the sequence. This differs from the superclass method |
| 25 | * in not allowing an exception, either during this method call, or in the subsequent processing |
| 26 | * of the returned iterator. |
| 27 | * |
| 28 | * @return an iterator (meaning a Saxon {@link SequenceIterator} rather than a Java |
| 29 | * {@link java.util.Iterator}) over all the items in this Sequence. |
| 30 | */ |
| 31 | |
| 32 | UnfailingIterator<T> iterate(); |
| 33 | |
| 34 | /** |
| 35 | * Get the n'th item in the value, counting from zero (0) |
| 36 | * |
| 37 | * @param n the index of the required item, with zero (0) representing the first item in the sequence |
| 38 | * @return the n'th item if it exists, or null if the requested position is out of range |
| 39 | */ |
| 40 | |
| 41 | T itemAt(int n); |
| 42 | |
| 43 | /** |
| 44 | * Get the first item of the sequence. This differs from the parent interface in not allowing an exception |
| 45 | * @return the first item of the sequence, or null if the sequence is empty |
| 46 | */ |
| 47 | |
| 48 | T head(); |
| 49 | |
| 50 | /** |
| 51 | * Get a subsequence of the value |
| 52 | * |
| 53 | * @param start the index of the first item to be included in the result, counting from zero. |
| 54 | * A negative value is taken as zero. If the value is beyond the end of the sequence, an empty |
| 55 | * sequence is returned |
| 56 | * @param length the number of items to be included in the result. Specify Integer.MAX_VALUE to |
| 57 | * get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence |
| 58 | * is returned. If the length goes off the end of the sequence, the result returns items up to the end |
| 59 | * of the sequence |
| 60 | * @return the required subsequence. |
| 61 | */ |
| 62 | |
| 63 | GroundedValue<T> subsequence(int start, int length); |
| 64 | |
| 65 | /** |
| 66 | * Get the size of the value (the number of items in the value, considered as a sequence) |
| 67 | * |
| 68 | * @return the number of items in the sequence. Note that for a single item, including a map or array, |
| 69 | * the result is always 1 (one). |
| 70 | */ |
| 71 | |
| 72 | int getLength(); |
| 73 | |
| 74 | /** |
| 75 | * Get the effective boolean value of this sequence |
| 76 | * |
| 77 | * @return the effective boolean value |
| 78 | * @throws XPathException if the sequence has no effective boolean value (for example a sequence of two integers) |
| 79 | */ |
| 80 | |
| 81 | default boolean effectiveBooleanValue() throws XPathException { |
| 82 | return ExpressionTool.effectiveBooleanValue(iterate()); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get the string value of this sequence. The string value of an item is the result of applying the string() |
| 87 | * function. The string value of a sequence is the space-separated result of applying the string-join() function |
| 88 | * using a single space as the separator |
| 89 | * |
| 90 | * @return the string value of the sequence. |
| 91 | * @throws XPathException if the sequence contains items that have no string value (for example, function items) |
| 92 | */ |
| 93 | |
| 94 | String getStringValue() throws XPathException; |
| 95 | |
| 96 | /** |
| 97 | * Get the string value of this sequence. The string value of an item is the result of applying the string() |
| 98 | * function. The string value of a sequence is the space-separated result of applying the string-join() function |
| 99 | * using a single space as the separator |
| 100 | * |
| 101 | * @return the string value of the sequence. |
| 102 | * @throws XPathException if the sequence contains items that have no string value (for example, function items) |
| 103 | */ |
| 104 | |
| 105 | CharSequence getStringValueCS() throws XPathException; |
| 106 | |
| 107 | /** |
| 108 | * Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be |
| 109 | * EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance |
| 110 | * of AtomicValue. If the value is a single item of any other kind, the result will be an instance |
| 111 | * of Item. Otherwise, the result will typically be unchanged. |
| 112 | * |
| 113 | * @return the simplified sequence |
| 114 | */ |
| 115 | |
| 116 | default GroundedValue<T> reduce() { |
| 117 | return this; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Create a {@link GroundedValue} containing the same items as this Sequence. |
| 122 | * Since this Sequence is already a {@code GroundedValue} this method returns |
| 123 | * this {@code GroundedValue} unchanged. |
| 124 | * |
| 125 | * @return this {@link GroundedValue} |
| 126 | */ |
| 127 | |
| 128 | default GroundedValue<T> materialize() { |
| 129 | return this; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Produce a short representation of the value of the sequence, suitable for use in error messages |
| 134 | * @return a short representation of the value |
| 135 | */ |
| 136 | |
| 137 | default String toShortString() { |
| 138 | return Err.depictSequence(this).toString(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get the contents of this value in the form of a Java {@link java.lang.Iterable}, |
| 143 | * so that it can be used in a for-each expression. For {@link GroundedValue} and all |
| 144 | * its subclasses, the class implements {@link java.lang.Iterable} directly, so this |
| 145 | * method returns this object itself. |
| 146 | * |
| 147 | * @return an Iterable containing the same sequence of items |
| 148 | */ |
| 149 | |
| 150 | default Iterable<T> asIterable() { |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get an iterator (a Java {@link java.util.Iterator}) over the items in this sequence. |
| 156 | * @return an iterator over the items in this sequence. |
| 157 | */ |
| 158 | |
| 159 | default Iterator<T> iterator() { |
| 160 | final UnfailingIterator<T> base = iterate(); |
| 161 | return new Iterator<T>() { |
| 162 | |
| 163 | T pending = null; |
| 164 | |
| 165 | @Override |
| 166 | public boolean hasNext() { |
| 167 | pending = base.next(); |
| 168 | return pending != null; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public T next() { |
| 173 | return pending; |
| 174 | } |
| 175 | }; |
| 176 | } |
| 177 | } |
| 178 |