| 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.value; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.expr.XPathContext; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.Sequence; |
| 14 | import net.sf.saxon.om.ZeroOrOne; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.tree.iter.SingletonIterator; |
| 17 | import net.sf.saxon.tree.iter.UnfailingIterator; |
| 18 | |
| 19 | /** |
| 20 | * A SingletonClosure represents a value that has not yet been evaluated: the value is represented |
| 21 | * by an expression, together with saved values of all the context variables that the |
| 22 | * expression depends on. The value of a SingletonClosure is always either a single item |
| 23 | * or an empty sequence. |
| 24 | * <p>The expression may depend on local variables and on the context item; these values |
| 25 | * are held in the saved XPathContext object that is kept as part of the Closure, and they |
| 26 | * will always be read from that object. The expression may also depend on global variables; |
| 27 | * these are unchanging, so they can be read from the Bindery in the normal way. Expressions |
| 28 | * that depend on other contextual information, for example the values of position(), last(), |
| 29 | * current(), current-group(), should not be evaluated using this mechanism: they should |
| 30 | * always be evaluated eagerly. This means that the Closure does not need to keep a copy |
| 31 | * of these context variables.</p> |
| 32 | */ |
| 33 | |
| 34 | public class SingletonClosure extends Closure implements Sequence<Item<?>> { |
| 35 | |
| 36 | private boolean built = false; |
| 37 | /*@Nullable*/ private Item value = null; |
| 38 | |
| 39 | /** |
| 40 | * Constructor should not be called directly, instances should be made using the make() method. |
| 41 | * |
| 42 | * @param exp the expression to be lazily evaluated |
| 43 | * @param context the context in which the expression should be evaluated |
| 44 | * @throws XPathException if an error occurs saving the dynamic context |
| 45 | */ |
| 46 | |
| 47 | public SingletonClosure(/*@NotNull*/ Expression exp, /*@NotNull*/ XPathContext context) throws XPathException { |
| 48 | expression = exp; |
| 49 | savedXPathContext = context.newContext(); |
| 50 | saveContext(exp, context); |
| 51 | //System.err.println("Creating SingletonClosure"); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Evaluate the expression in a given context to return an iterator over a sequence |
| 56 | */ |
| 57 | |
| 58 | /*@NotNull*/ |
| 59 | public UnfailingIterator<Item<?>> iterate() throws XPathException { |
| 60 | return SingletonIterator.makeIterator(asItem()); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Return the value in the form of an Item |
| 66 | * |
| 67 | * @return the value in the form of an Item |
| 68 | */ |
| 69 | |
| 70 | /*@Nullable*/ |
| 71 | public Item<?> asItem() throws XPathException { |
| 72 | if (!built) { |
| 73 | value = expression.evaluateItem(savedXPathContext); |
| 74 | built = true; |
| 75 | savedXPathContext = null; // release variables saved in the context to the garbage collector |
| 76 | } |
| 77 | return value; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the n'th item in the sequence (starting from 0). This is defined for all |
| 82 | * SequenceValues, but its real benefits come for a SequenceValue stored extensionally |
| 83 | */ |
| 84 | |
| 85 | /*@Nullable*/ |
| 86 | public Item<?> itemAt(int n) throws XPathException { |
| 87 | if (n != 0) { |
| 88 | return null; |
| 89 | } |
| 90 | return asItem(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the length of the sequence |
| 95 | */ |
| 96 | |
| 97 | public int getLength() throws XPathException { |
| 98 | return asItem() == null ? 0 : 1; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return a value containing all the items in the sequence returned by this |
| 103 | * SequenceIterator |
| 104 | * |
| 105 | * @return the corresponding value |
| 106 | */ |
| 107 | |
| 108 | public ZeroOrOne<Item<?>> materialize() throws XPathException { |
| 109 | return new ZeroOrOne<>(asItem()); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public SingletonClosure makeRepeatable() { |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | } |
| 118 |