| 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.ContextItemStaticInfo; |
| 11 | import net.sf.saxon.expr.parser.ExpressionTool; |
| 12 | import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 | import net.sf.saxon.expr.parser.RebindingMap; |
| 14 | import net.sf.saxon.trace.ExpressionPresenter; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.type.BuiltInAtomicType; |
| 17 | import net.sf.saxon.type.ItemType; |
| 18 | import net.sf.saxon.value.BooleanValue; |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * A position() eq last() expression, generated by the optimizer. |
| 23 | */ |
| 24 | |
| 25 | public final class IsLastExpression extends Expression { |
| 26 | |
| 27 | private boolean condition; |
| 28 | |
| 29 | /** |
| 30 | * Construct a condition that tests position() eq last() (if condition |
| 31 | * is true) or position() ne last() (if condition is false). |
| 32 | * |
| 33 | * @param condition true if we are testing "equals", false for "not equals". |
| 34 | */ |
| 35 | |
| 36 | public IsLastExpression(boolean condition) { |
| 37 | this.condition = condition; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the condition we are testing for |
| 42 | * |
| 43 | * @return true if we are testing "equals", false for "not equals". |
| 44 | */ |
| 45 | |
| 46 | public boolean getCondition() { |
| 47 | return condition; |
| 48 | } |
| 49 | |
| 50 | /*@NotNull*/ |
| 51 | public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) { |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | /*@NotNull*/ |
| 56 | public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) { |
| 57 | return this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Determine the special properties of this expression |
| 62 | * |
| 63 | * @return {@link StaticProperty#NO_NODES_NEWLY_CREATED}. |
| 64 | */ |
| 65 | |
| 66 | public int computeSpecialProperties() { |
| 67 | int p = super.computeSpecialProperties(); |
| 68 | return p | StaticProperty.NO_NODES_NEWLY_CREATED; |
| 69 | } |
| 70 | |
| 71 | public BooleanValue evaluateItem(XPathContext c) throws XPathException { |
| 72 | return BooleanValue.get(condition == c.isAtLast()); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Determine the data type of the expression |
| 77 | * |
| 78 | * @return Type.BOOLEAN |
| 79 | */ |
| 80 | |
| 81 | /*@NotNull*/ |
| 82 | public ItemType getItemType() { |
| 83 | return BuiltInAtomicType.BOOLEAN; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Determine the static cardinality |
| 88 | */ |
| 89 | |
| 90 | public int computeCardinality() { |
| 91 | return StaticProperty.EXACTLY_ONE; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the dependencies of this expression on the context |
| 96 | */ |
| 97 | |
| 98 | public int getIntrinsicDependencies() { |
| 99 | return StaticProperty.DEPENDS_ON_POSITION | StaticProperty.DEPENDS_ON_LAST; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Copy an expression. This makes a deep copy. |
| 104 | * |
| 105 | * @return the copy of the original expression |
| 106 | * @param rebindings |
| 107 | */ |
| 108 | |
| 109 | /*@NotNull*/ |
| 110 | public Expression copy(RebindingMap rebindings) { |
| 111 | IsLastExpression exp = new IsLastExpression(condition); |
| 112 | ExpressionTool.copyLocationInfo(this, exp); |
| 113 | return exp; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). |
| 118 | * This method indicates which of these methods is provided directly. The other methods will always be available |
| 119 | * indirectly, using an implementation that relies on one of the other methods. |
| 120 | * |
| 121 | * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or |
| 122 | * {@link #PROCESS_METHOD} |
| 123 | */ |
| 124 | @Override |
| 125 | public int getImplementationMethod() { |
| 126 | return EVALUATE_METHOD; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Is this expression the same as another expression? |
| 131 | * |
| 132 | * @param other the expression to be compared with this one |
| 133 | * @return true if the two expressions are statically equivalent |
| 134 | */ |
| 135 | @Override |
| 136 | public boolean equals(Object other) { |
| 137 | return other instanceof IsLastExpression && ((IsLastExpression)other).condition == condition; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Hashcode supporting equals() |
| 142 | */ |
| 143 | |
| 144 | public int computeHashCode() { |
| 145 | return condition ? 0x236b91a0 : 0x896b92a0; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get a name identifying the kind of expression, in terms meaningful to a user. |
| 150 | * |
| 151 | * @return a name identifying the kind of expression, in terms meaningful to a user. |
| 152 | * The name will always be in the form of a lexical XML QName, and should match the name used |
| 153 | * in export() output displaying the expression. |
| 154 | */ |
| 155 | @Override |
| 156 | public String getExpressionName() { |
| 157 | return "isLast"; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Diagnostic print of expression structure. The abstract expression tree |
| 162 | * is written to the supplied output destination. |
| 163 | */ |
| 164 | |
| 165 | public void export(ExpressionPresenter destination) throws XPathException { |
| 166 | destination.startElement("isLast", this); |
| 167 | destination.emitAttribute("test", condition ? "1" : "0"); |
| 168 | destination.endElement(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * <p>The toString() method for an expression attempts to give a representation of the expression |
| 173 | * in an XPath-like form.</p> |
| 174 | * <p>For subclasses of Expression that represent XPath expressions, the result should always be a string that |
| 175 | * parses as an XPath 3.0 expression.</p> |
| 176 | * |
| 177 | * @return a representation of the expression as a string |
| 178 | */ |
| 179 | @Override |
| 180 | public String toString() { |
| 181 | if (condition) { |
| 182 | return "position() eq last()"; |
| 183 | } else { |
| 184 | return "position() ne last()"; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get the (partial) name of a class that supports streaming of this kind of expression |
| 190 | * |
| 191 | * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE, |
| 192 | * or null if there is no such class |
| 193 | */ |
| 194 | @Override |
| 195 | public String getStreamerName() { |
| 196 | return "IsLastExpr"; |
| 197 | } |
| 198 | } |
| 199 |