net.sf.saxon.expr.IsLastExpression Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Saxon-HE Show documentation
Show all versions of Saxon-HE Show documentation
The XSLT and XQuery Processor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2023 Saxonica Limited
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.expr;
import net.sf.saxon.expr.elab.BooleanEvaluator;
import net.sf.saxon.expr.elab.BooleanElaborator;
import net.sf.saxon.expr.elab.Elaborator;
import net.sf.saxon.expr.parser.ContextItemStaticInfo;
import net.sf.saxon.expr.parser.ExpressionTool;
import net.sf.saxon.expr.parser.ExpressionVisitor;
import net.sf.saxon.expr.parser.RebindingMap;
import net.sf.saxon.trace.ExpressionPresenter;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.type.BuiltInAtomicType;
import net.sf.saxon.type.ItemType;
import net.sf.saxon.value.BooleanValue;
/**
* A position() eq last() expression, generated by the optimizer.
*/
public final class IsLastExpression extends Expression {
private final boolean condition;
/**
* Construct a condition that tests position() eq last() (if condition
* is true) or position() ne last() (if condition is false).
*
* @param condition true if we are testing "equals", false for "not equals".
*/
public IsLastExpression(boolean condition) {
this.condition = condition;
}
/**
* Get the condition we are testing for
*
* @return true if we are testing "equals", false for "not equals".
*/
public boolean getCondition() {
return condition;
}
/*@NotNull*/
@Override
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) {
return this;
}
/*@NotNull*/
@Override
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) {
return this;
}
/**
* Determine the special properties of this expression
*
* @return {@link StaticProperty#NO_NODES_NEWLY_CREATED}.
*/
@Override
protected int computeSpecialProperties() {
int p = super.computeSpecialProperties();
return p | StaticProperty.NO_NODES_NEWLY_CREATED;
}
@Override
public BooleanValue evaluateItem(XPathContext c) throws XPathException {
return BooleanValue.get(condition == c.isAtLast());
}
@Override
public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
return condition == context.isAtLast();
}
/**
* Determine the data type of the expression
*
* @return Type.BOOLEAN
*/
/*@NotNull*/
@Override
public ItemType getItemType() {
return BuiltInAtomicType.BOOLEAN;
}
/**
* Determine the static cardinality
*/
@Override
protected int computeCardinality() {
return StaticProperty.EXACTLY_ONE;
}
/**
* Get the dependencies of this expression on the context
*/
@Override
public int getIntrinsicDependencies() {
return StaticProperty.DEPENDS_ON_POSITION | StaticProperty.DEPENDS_ON_LAST;
}
/**
* Copy an expression. This makes a deep copy.
*
* @return the copy of the original expression
* @param rebindings variables that must be rebound (not used)
*/
/*@NotNull*/
@Override
public Expression copy(RebindingMap rebindings) {
IsLastExpression exp = new IsLastExpression(condition);
ExpressionTool.copyLocationInfo(this, exp);
return exp;
}
/**
* An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
* This method indicates which of these methods is provided directly. The other methods will always be available
* indirectly, using an implementation that relies on one of the other methods.
*
* @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
* {@link #PROCESS_METHOD}
*/
@Override
public int getImplementationMethod() {
return EVALUATE_METHOD;
}
/**
* Is this expression the same as another expression?
*
* @param other the expression to be compared with this one
* @return true if the two expressions are statically equivalent
*/
@Override
public boolean equals(Object other) {
return other instanceof IsLastExpression && ((IsLastExpression)other).condition == condition;
}
/**
* Hashcode supporting equals()
*/
@Override
protected int computeHashCode() {
return condition ? 0x236b91a0 : 0x396b92a0;
}
/**
* Get a name identifying the kind of expression, in terms meaningful to a user.
*
* @return a name identifying the kind of expression, in terms meaningful to a user.
* The name will always be in the form of a lexical XML QName, and should match the name used
* in export() output displaying the expression.
*/
@Override
public String getExpressionName() {
return "isLast";
}
/**
* Diagnostic print of expression structure. The abstract expression tree
* is written to the supplied output destination.
*/
@Override
public void export(ExpressionPresenter destination) throws XPathException {
destination.startElement("isLast", this);
destination.emitAttribute("test", condition ? "1" : "0");
destination.endElement();
}
/**
* The toString() method for an expression attempts to give a representation of the expression
* in an XPath-like form.
* For subclasses of Expression that represent XPath expressions, the result should always be a string that
* parses as an XPath 3.0 expression.
*
* @return a representation of the expression as a string
*/
@Override
public String toString() {
if (condition) {
return "position() eq last()";
} else {
return "position() ne last()";
}
}
/**
* Get the (partial) name of a class that supports streaming of this kind of expression
*
* @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
* or null if there is no such class
*/
@Override
public String getStreamerName() {
return "IsLastExpr";
}
/**
* Make an elaborator for this expression
*
* @return a suitable elaborator
*/
@Override
public Elaborator getElaborator() {
return new IsLastElaborator();
}
/**
* Elaborator for an "isLast" expression, equivalent to {@code position() = last()} or {@code position() != last()}
*/
public static class IsLastElaborator extends BooleanElaborator {
public BooleanEvaluator elaborateForBoolean() {
IsLastExpression expr = (IsLastExpression) getExpression();
if (expr.getCondition()) {
return XPathContext::isAtLast;
} else {
return context -> !context.isAtLast();
}
}
}
}