Debian Codegraph @

net/sf/saxon/expr/IntegerRangeTest.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.om.SequenceIterator;
12 import net.sf.saxon.trace.ExpressionPresenter;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.*;
15 import net.sf.saxon.value.*;
16
17 /**
18 * An IntegerRangeTest is an expression of the form
19 * E = N to M
20 * where N and M are both expressions of type integer.
21 * The result of the expression is true if any value in the result of E is a numeric value in the
22 * range N to M. Any untyped atomic values in E are converted to integers; values that cannot be converted
23 * result in an error.
24 */
25
26 public class IntegerRangeTest extends Expression {
27
28 private Operand valueOp;
29 private Operand minOp;
30 private Operand maxOp;
31
32 /**
33 * Construct a IntegerRangeTest
34 *
35 * @param value the integer value to be tested to see if it is in the range min to max inclusive
36 * @param min the lowest permitted value
37 * @param max the highest permitted value
38 */
39
40 public IntegerRangeTest(Expression value, Expression min, Expression max) {
41 valueOp = new Operand(this, value, OperandRole.ATOMIC_SEQUENCE);
42 minOp = new Operand(this, min, OperandRole.SINGLE_ATOMIC);
43 maxOp = new Operand(this, max, OperandRole.SINGLE_ATOMIC);
44 }
45
46 @Override
47 public Iterable<Operand> operands() {
48 return operandList(valueOp, minOp, maxOp);
49 }
50
51 public Expression getValue() {
52 return valueOp.getChildExpression();
53 }
54
55 public void setValue(Expression value) {
56 valueOp.setChildExpression(value);
57 }
58
59 public Expression getMin() {
60 return minOp.getChildExpression();
61 }
62
63 public void setMin(Expression min) {
64 minOp.setChildExpression(min);
65 }
66
67 public Expression getMax() {
68 return maxOp.getChildExpression();
69 }
70
71 public void setMax(Expression max) {
72 maxOp.setChildExpression(max);
73 }
74
75 /**
76 * Type-check the expression
77 */
78
79 /*@NotNull*/
80 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
81 // Already done, we only get one of these expressions after the operands have been analyzed
82 return this;
83 }
84
85 /**
86 * Perform optimisation of an expression and its subexpressions.
87 * <p>This method is called after all references to functions and variables have been resolved
88 * to the declaration of the function or variable, and after all type checking has been done.</p>
89 *
90 * @param visitor an expression visitor
91 * @param contextItemType the static type of "." at the point where this expression is invoked.
92 * The parameter is set to null if it is known statically that the context item will be undefined.
93 * If the type of the context item is not known statically, the argument is set to
94 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
95 * @return the original expression, rewritten if appropriate to optimize execution
96 * @throws XPathException if an error is discovered during this phase
97 * (typically a type error)
98 */
99
100 /*@NotNull*/
101 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
102 if (Literal.isEmptySequence(getMin()) || Literal.isEmptySequence(getMax()) || Literal.isEmptySequence(getValue())) {
103 return new Literal(BooleanValue.FALSE);
104 }
105 if (getMin() instanceof Literal && getMax() instanceof Literal && getValue() instanceof Literal) {
106 BooleanValue result = evaluateItem(visitor.makeDynamicContext());
107 return new Literal(result);
108 }
109 return this;
110 }
111
112 /**
113 * Get the data type of the items returned
114 */
115
116 /*@NotNull*/
117 public ItemType getItemType() {
118 return BuiltInAtomicType.BOOLEAN;
119 }
120
121 /**
122 * Determine the static cardinality
123 */
124
125 public int computeCardinality() {
126 return StaticProperty.EXACTLY_ONE;
127 }
128
129 /**
130 * Copy an expression. This makes a deep copy.
131 *
132 * @return the copy of the original expression
133 * @param rebindings variables that need to be re-bound
134 */
135
136 /*@NotNull*/
137 public Expression copy(RebindingMap rebindings) {
138 IntegerRangeTest exp = new IntegerRangeTest(
139 getValue().copy(rebindings), getMin().copy(rebindings), getMax().copy(rebindings));
140 ExpressionTool.copyLocationInfo(this, exp);
141 return exp;
142 }
143
144 /**
145 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
146 * This method indicates which of these methods is provided directly. The other methods will always be available
147 * indirectly, using an implementation that relies on one of the other methods.
148 *
149 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
150 * {@link #PROCESS_METHOD}
151 */
152 @Override
153 public int getImplementationMethod() {
154 return EVALUATE_METHOD;
155 }
156
157 /**
158 * Is this expression the same as another expression?
159 *
160 * @param other the expression to be compared with this one
161 * @return true if the two expressions are statically equivalent
162 */
163 @Override
164 public boolean equals(Object other) {
165 return other instanceof IntegerRangeTest && ((IntegerRangeTest) other).getValue().isEqual(getValue())
166 && ((IntegerRangeTest) other).getMin().isEqual(getMin())
167 && ((IntegerRangeTest) other).getMax().isEqual(getMax());
168 }
169
170 /**
171 * Hashcode supporting equals()
172 */
173
174 public int computeHashCode() {
175 int h = getValue().hashCode() + 77;
176 h ^= getMin().hashCode() ^ getMax().hashCode();
177 return h;
178 }
179
180 /**
181 * Evaluate the expression
182 */
183
184 public BooleanValue evaluateItem(XPathContext c) throws XPathException {
185 IntegerValue minVal = null;
186 IntegerValue maxVal = null;
187 StringConverter toDouble = null;
188 SequenceIterator iter = getValue().iterate(c);
189 AtomicValue atom;
190 while ((atom = (AtomicValue) iter.next()) != null) {
191 if (minVal == null) {
192 minVal = (IntegerValue) getMin().evaluateItem(c);
193 if (minVal == null) {
194 return BooleanValue.FALSE;
195 }
196 maxVal = (IntegerValue) getMax().evaluateItem(c);
197 if (maxVal == null || maxVal.compareTo(minVal) < 0) { // bug 3666
198 return BooleanValue.FALSE;
199 }
200 }
201 NumericValue v;
202 if (atom instanceof UntypedAtomicValue) {
203 if (toDouble == null) {
204 toDouble = BuiltInAtomicType.DOUBLE.getStringConverter(c.getConfiguration().getConversionRules());
205 }
206 ConversionResult result = toDouble.convertString(atom.getStringValueCS());
207 if (result instanceof ValidationFailure) {
208 XPathException e = new XPathException("Failed to convert untypedAtomic value {" +
209 atom.getStringValueCS() + "} to xs:integer", "FORG0001");
210 e.setLocation(getLocation());
211 throw e;
212 } else {
213 v = (DoubleValue) result.asAtomic();
214 }
215 } else if (atom instanceof NumericValue) {
216 v = (NumericValue) atom;
217 } else {
218 XPathException e = new XPathException("Cannot compare value of type " +
219 atom.getUType() + " to xs:integer", "XPTY0004");
220 e.setIsTypeError(true);
221 e.setLocation(getLocation());
222 throw e;
223 }
224 if (v.isWholeNumber() && v.compareTo(minVal) >= 0 && v.compareTo(maxVal) <= 0) {
225 return BooleanValue.TRUE;
226 }
227 }
228 return BooleanValue.FALSE;
229 }
230
231 /**
232 * Get a name identifying the kind of expression, in terms meaningful to a user.
233 *
234 * @return a name identifying the kind of expression, in terms meaningful to a user.
235 * The name will always be in the form of a lexical XML QName, and should match the name used
236 * in export() output displaying the expression.
237 */
238 @Override
239 public String getExpressionName() {
240 return "intRangeTest";
241 }
242
243 /**
244 * Diagnostic print of expression structure. The abstract expression tree
245 * is written to the supplied output destination.
246 */
247
248 public void export(ExpressionPresenter destination) throws XPathException {
249 destination.startElement("intRangeTest", this);
250 getValue().export(destination);
251 getMin().export(destination);
252 getMax().export(destination);
253 destination.endElement();
254 }
255
256 /**
257 * <p>The toString() method for an expression attempts to give a representation of the expression
258 * in an XPath-like form.</p>
259 * <p>For subclasses of Expression that represent XPath expressions, the result should always be a string that
260 * parses as an XPath 3.0 expression. </p>
261 *
262 * @return a representation of the expression as a string
263 */
264 @Override
265 public String toString() {
266 return ExpressionTool.parenthesize(getValue()) + " = (" +
267 ExpressionTool.parenthesize(getMin()) + " to " +
268 ExpressionTool.parenthesize(getMax()) + ")";
269 }
270
271
272 }
273