| 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.trans.XPathException; |
| 11 | import net.sf.saxon.value.Closure; |
| 12 | |
| 13 | /** |
| 14 | * This interface represents an XDM Value, that is, a sequence of items. |
| 15 | * <p>Note that different implementations of Sequence might have very different |
| 16 | * performance characteristics, though all should exhibit the same behaviour. |
| 17 | * With some sequences, calling iterate() may trigger evaluation of the logic |
| 18 | * that computes the sequence, and calling iterate() again may cause re-evaluation.</p> |
| 19 | * <p>Users should avoid assuming that a sequence of length one will always |
| 20 | * be represented as an instance of Item. If you are confident that the sequence |
| 21 | * will be of length one, call the head() function to get the first item.</p> |
| 22 | * |
| 23 | * @param <T> the type of items that can exist in the sequence |
| 24 | * |
| 25 | * @since 9.5. Generified in 9.9. |
| 26 | */ |
| 27 | public interface Sequence<T extends Item<?>> { |
| 28 | |
| 29 | /** |
| 30 | * Get the first item in the sequence. |
| 31 | * |
| 32 | * @return the first item in the sequence if there is one, or null if the sequence |
| 33 | * is empty |
| 34 | * @throws XPathException in the situation where the sequence is evaluated lazily, and |
| 35 | * evaluation of the first item causes a dynamic error. |
| 36 | */ |
| 37 | |
| 38 | T head() throws XPathException; |
| 39 | |
| 40 | /** |
| 41 | * Get a {@link SequenceIterator} over all the items in the sequence |
| 42 | * |
| 43 | * @return an iterator (specifically, a Saxon {@link SequenceIterator}, which is |
| 44 | * not a {@link java.util.Iterator}) over all the items |
| 45 | * @throws XPathException in the situation where the sequence is evaluated lazily, and |
| 46 | * constructing an iterator over the items causes a dynamic error. |
| 47 | */ |
| 48 | |
| 49 | SequenceIterator<T> iterate() throws XPathException; |
| 50 | |
| 51 | /** |
| 52 | * Get the contents of this value in the form of a Java {@link java.lang.Iterable}, |
| 53 | * so that it can be used in a for-each expression. In the general case, obtaining |
| 54 | * the iterable may raise dynamic errors; but the {@link java.util.Iterator} obtained |
| 55 | * from the iterable will be error-free. Subclasses of {@link Sequence} that |
| 56 | * implement {@link GroundedValue} are directly iterable, so this method is a no-op |
| 57 | * in those cases. |
| 58 | * |
| 59 | * @return an Iterable containing the same sequence of items |
| 60 | */ |
| 61 | |
| 62 | default Iterable<T> asIterable() throws XPathException { |
| 63 | return iterate().materialize().asIterable(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Create a {@link GroundedValue} containing the same items as this Sequence. |
| 68 | * A {@code GroundedValue} typically contains the entire sequence in memory; at |
| 69 | * any rate, it guarantees that the entire sequence can be read without any |
| 70 | * possibility of XPath dynamic errors arising. |
| 71 | * @return a {@link GroundedValue} containing the same items as this Sequence |
| 72 | * @throws XPathException if evaluating the contents of the sequence fails with |
| 73 | * a dynamic error. |
| 74 | */ |
| 75 | |
| 76 | default GroundedValue<T> materialize() throws XPathException { |
| 77 | return iterate().materialize(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Ensure that the sequence is in a form where it can be evaluated more than once. Some |
| 82 | * sequences (for example {@link LazySequence} and {@link Closure} can only be evaluated |
| 83 | * once, and this operation causes these to be grounded. However, making it repeatable |
| 84 | * is not the same as making it grounded; it does not flush out all errors. Indeed, lazy |
| 85 | * evaluation relies on this property, because an expression that has been lifted out of |
| 86 | * a loop must not be evaluated unless the loop is executed at least once, to prevent spurious |
| 87 | * errors. |
| 88 | * @return An equivalent sequence that can be repeatedly evaluated |
| 89 | * @throws XPathException if evaluation fails |
| 90 | */ |
| 91 | |
| 92 | default Sequence<T> makeRepeatable() throws XPathException { |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | } |
| 98 |