Debian Codegraph @

net/sf/saxon/expr/CompareToIntegerConstant.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.*;
11 import net.sf.saxon.expr.sort.AtomicComparer;
12 import net.sf.saxon.expr.sort.DoubleSortComparer;
13 import net.sf.saxon.trace.ExpressionPresenter;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.BuiltInAtomicType;
16 import net.sf.saxon.type.ItemType;
17 import net.sf.saxon.value.BooleanValue;
18 import net.sf.saxon.value.Int64Value;
19 import net.sf.saxon.value.NumericValue;
20
21 /**
22 * This class implements a comparison of a numeric value to an integer constant using one of the operators
23 * eq, ne, lt, gt, le, ge. The semantics are identical to ValueComparison, but this is a fast path for an
24 * important common case.
25 */
26
27 public class CompareToIntegerConstant extends UnaryExpression implements ComparisonExpression {
28
29 private long comparand;
30 private int operator;
31
32 /**
33 * Create the expression
34 *
35 * @param operand the operand to be compared with an integer constant. This must
36 * have a static type of NUMERIC, and a cardinality of EXACTLY ONE
37 * @param operator the comparison operator,
38 * one of {@link Token#FEQ}, {@link Token#FNE}, {@link Token#FGE},
39 * {@link Token#FGT}, {@link Token#FLE}, {@link Token#FLT}
40 * @param comparand the integer constant
41 */
42
43 public CompareToIntegerConstant(Expression operand, int operator, long comparand) {
44 super(operand);
45 this.operator = operator;
46 this.comparand = comparand;
47 }
48
49 @Override
50 protected OperandRole getOperandRole() {
51 return OperandRole.SINGLE_ATOMIC;
52 }
53
54 public Expression getLhsExpression() {
55 return getBaseExpression();
56 }
57
58 public Operand getLhs() {
59 return getOperand();
60 }
61
62 public Expression getRhsExpression() {
63 return new Literal(new Int64Value(comparand));
64 }
65
66 public Operand getRhs() {
67 return new Operand(this, getRhsExpression(), OperandRole.SINGLE_ATOMIC);
68 }
69
70 /**
71 * Get the integer value on the rhs of the expression
72 *
73 * @return the integer constant
74 */
75
76 public long getComparand() {
77 return comparand;
78 }
79
80 /**
81 * Get the comparison operator
82 *
83 * @return one of {@link Token#FEQ}, {@link Token#FNE}, {@link Token#FGE},
84 * {@link Token#FGT}, {@link Token#FLE}, {@link Token#FLT}
85 */
86
87 public int getComparisonOperator() {
88 return operator;
89 }
90
91 /**
92 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
93 * This method indicates which of these methods is provided directly. The other methods will always be available
94 * indirectly, using an implementation that relies on one of the other methods.
95 *
96 * @return the value {@link #EVALUATE_METHOD}
97 */
98
99 public int getImplementationMethod() {
100 return EVALUATE_METHOD;
101 }
102
103 public int computeSpecialProperties() {
104 return StaticProperty.NO_NODES_NEWLY_CREATED;
105 }
106
107 /**
108 * Copy an expression. This makes a deep copy.
109 *
110 * @return the copy of the original expression
111 * @param rebindings
112 */
113
114 /*@NotNull*/
115 public Expression copy(RebindingMap rebindings) {
116 CompareToIntegerConstant c2 = new CompareToIntegerConstant(getLhsExpression().copy(rebindings), operator, comparand);
117 ExpressionTool.copyLocationInfo(this, c2);
118 return c2;
119 }
120
121 /**
122 * Is this expression the same as another expression?
123 *
124 * @param other the expression to be compared with this one
125 * @return true if the two expressions are statically equivalent
126 */
127 @Override
128 public boolean equals(Object other) {
129 return other instanceof CompareToIntegerConstant && ((CompareToIntegerConstant)other).getLhsExpression().isEqual(getLhsExpression())
130 && ((CompareToIntegerConstant)other).comparand == comparand
131 && ((CompareToIntegerConstant)other).operator == operator;
132 }
133
134 /**
135 * Hashcode supporting equals()
136 */
137
138 public int computeHashCode() {
139 int h = 0x836b12a0;
140 return h + getLhsExpression().hashCode() ^ (int)comparand;
141 }
142
143
144
145 /**
146 * Evaluate an expression as a single item. This always returns either a single Item or
147 * null (denoting the empty sequence). No conversion is done. This method should not be
148 * used unless the static type of the expression is a subtype of "item" or "item?": that is,
149 * it should not be called if the expression may return a sequence. There is no guarantee that
150 * this condition will be detected.
151 *
152 * @param context The context in which the expression is to be evaluated
153 * @return the node or atomic value that results from evaluating the
154 * expression; or null to indicate that the result is an empty
155 * sequence
156 * @throws net.sf.saxon.trans.XPathException
157 * if any dynamic error occurs evaluating the
158 * expression
159 */
160
161 public BooleanValue evaluateItem(XPathContext context) throws XPathException {
162 return BooleanValue.get(effectiveBooleanValue(context));
163 }
164
165 /**
166 * Get the effective boolean value of the expression. This returns false if the value
167 * is the empty sequence, a zero-length string, a number equal to zero, or the boolean
168 * false. Otherwise it returns true.
169 *
170 * @param context The context in which the expression is to be evaluated
171 * @return the effective boolean value
172 * @throws net.sf.saxon.trans.XPathException
173 * if any dynamic error occurs evaluating the
174 * expression
175 */
176
177 public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
178 NumericValue n = (NumericValue) getLhsExpression().evaluateItem(context);
179 if (n.isNaN()) {
180 return operator == Token.FNE;
181 }
182 int c = n.compareTo(comparand);
183 switch (operator) {
184 case Token.FEQ:
185 return c == 0;
186 case Token.FNE:
187 return c != 0;
188 case Token.FGT:
189 return c > 0;
190 case Token.FLT:
191 return c < 0;
192 case Token.FGE:
193 return c >= 0;
194 case Token.FLE:
195 return c <= 0;
196 default:
197 throw new UnsupportedOperationException("Unknown operator " + operator);
198 }
199 }
200
201 public int computeCardinality() {
202 return StaticProperty.EXACTLY_ONE;
203 }
204
205 /**
206 * Perform optimisation of an expression and its subexpressions. This is the third and final
207 * phase of static optimization.
208 * <p>This method is called after all references to functions and variables have been resolved
209 * to the declaration of the function or variable, and after all type checking has been done.</p>
210 *
211 * @param visitor the expression visitor
212 * @param contextInfo the static type of "." at the point where this expression is invoked.
213 * The parameter is set to null if it is known statically that the context item will be undefined.
214 * If the type of the context item is not known statically, the argument is set to
215 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
216 * @return the original expression, rewritten if appropriate to optimize execution
217 * @throws XPathException if an error is discovered during this phase
218 * (typically a type error)
219 */
220
221 /*@NotNull*/
222 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
223 getOperand().optimize(visitor, contextInfo);
224 if (getLhsExpression() instanceof Literal) {
225 return Literal.makeLiteral(BooleanValue.get(effectiveBooleanValue(null)), this);
226 }
227 return this;
228 }
229
230 /**
231 * Get a name identifying the kind of expression, in terms meaningful to a user.
232 *
233 * @return a name identifying the kind of expression, in terms meaningful to a user.
234 * The name will always be in the form of a lexical XML QName, and should match the name used
235 * in export() output displaying the expression.
236 */
237 @Override
238 public String getExpressionName() {
239 return "compareToInt";
240 }
241
242
243 /**
244 * Determine the data type of the expression, if possible. All expression return
245 * sequences, in general; this method determines the type of the items within the
246 * sequence, assuming that (a) this is known in advance, and (b) it is the same for
247 * all items in the sequence.
248 * <p>This method should always return a result, though it may be the best approximation
249 * that is available at the time.</p>
250 *
251 * @return a value such as Type.STRING, Type.BOOLEAN, Type.NUMBER,
252 * Type.NODE, or Type.ITEM (meaning not known at compile time)
253 */
254
255 /*@NotNull*/
256 public ItemType getItemType() {
257 return BuiltInAtomicType.BOOLEAN;
258 }
259
260 /**
261 * Diagnostic print of expression structure. The abstract expression tree
262 * is written to the supplied output destination.
263 */
264
265 public void export(ExpressionPresenter destination) throws XPathException {
266 destination.startElement("compareToInt", this);
267 destination.emitAttribute("op", Token.tokens[operator]);
268 destination.emitAttribute("val", comparand + "");
269 getLhsExpression().export(destination);
270 destination.endElement();
271 }
272
273 /**
274 * <p>The toString() method for an expression attempts to give a representation of the expression
275 * in an XPath-like form.</p>
276 * <p>For subclasses of Expression that represent XPath expressions, the result should always be a string that
277 * parses as an XPath 3.0 expression</p>
278 *
279 * @return a representation of the expression as a string
280 */
281 @Override
282 public String toString() {
283 return ExpressionTool.parenthesize(getLhsExpression()) + " " +
284 Token.tokens[operator] + " " + comparand;
285 }
286
287 /**
288 * Produce a short string identifying the expression for use in error messages
289 *
290 * @return a short string, sufficient to identify the expression
291 */
292 @Override
293 public String toShortString() {
294 return getLhsExpression().toShortString() + " " + Token.tokens[operator] + " " + comparand;
295 }
296
297 /**
298 * Get the AtomicComparer used to compare atomic values. This encapsulates any collation that is used
299 */
300
301 public AtomicComparer getAtomicComparer() {
302 return DoubleSortComparer.getInstance();
303 // Note: this treats NaN=NaN as true, but it doesn't matter, because the rhs will never be NaN.
304 }
305
306 /**
307 * Get the primitive (singleton) operator used: one of Token.FEQ, Token.FNE, Token.FLT, Token.FGT,
308 * Token.FLE, Token.FGE
309 */
310
311 public int getSingletonOperator() {
312 return operator;
313 }
314
315 /**
316 * Determine whether untyped atomic values should be converted to the type of the other operand
317 *
318 * @return true if untyped values should be converted to the type of the other operand, false if they
319 * should be converted to strings.
320 */
321
322 public boolean convertsUntypedToOther() {
323 return true;
324 }
325 }
326