Debian Codegraph @

net/sf/saxon/expr/LastItemExpression.java

raw symbols SCIP
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.expr;
9
10 import net.sf.saxon.expr.parser.ExpressionTool;
11 import net.sf.saxon.expr.parser.RebindingMap;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.SequenceIterator;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.iter.ReversibleIterator;
16
17 /**
18 * A LastItemExpression returns the last item in the sequence returned by a given
19 * base expression. The evaluation strategy is to read the input sequence with a one-item lookahead.
20 */
21
22 public final class LastItemExpression extends SingleItemFilter {
23
24 /**
25 * Constructor
26 *
27 * @param base A sequence expression denoting sequence whose first item is to be returned
28 */
29
30 public LastItemExpression(Expression base) {
31 super(base);
32 }
33
34 /**
35 * Copy an expression. This makes a deep copy.
36 *
37 * @return the copy of the original expression
38 * @param rebindings
39 */
40
41 /*@NotNull*/
42 public Expression copy(RebindingMap rebindings) {
43 LastItemExpression exp = new LastItemExpression(getBaseExpression().copy(rebindings));
44 ExpressionTool.copyLocationInfo(this, exp);
45 return exp;
46 }
47
48 /**
49 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
50 * This method indicates which of these methods is provided directly. The other methods will always be available
51 * indirectly, using an implementation that relies on one of the other methods.
52 *
53 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
54 * {@link #PROCESS_METHOD}
55 */
56 @Override
57 public int getImplementationMethod() {
58 return EVALUATE_METHOD;
59 }
60
61 /**
62 * Evaluate the expression
63 */
64
65 /*@Nullable*/
66 public Item evaluateItem(XPathContext context) throws XPathException {
67 SequenceIterator forwards = getBaseExpression().iterate(context);
68 if (forwards instanceof ReversibleIterator) {
69 return ((ReversibleIterator) forwards).getReverseIterator().next();
70 } else {
71 Item current = null;
72 while (true) {
73 Item item = forwards.next();
74 if (item == null) {
75 return current;
76 }
77 current = item;
78 }
79 }
80 }
81
82
83 public String getExpressionName() {
84 return "lastOf";
85 }
86
87 }
88