Debian Codegraph @

net/sf/saxon/expr/Calculator.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.Token;
11 import net.sf.saxon.om.StandardNames;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.type.AtomicType;
14 import net.sf.saxon.type.BuiltInAtomicType;
15 import net.sf.saxon.type.Converter;
16 import net.sf.saxon.type.Type;
17 import net.sf.saxon.value.*;
18 import net.sf.saxon.z.IntHashMap;
19
20 import java.math.BigDecimal;
21 import java.math.BigInteger;
22 import java.math.RoundingMode;
23
24 /**
25 * This class evaluates arithmetic expressions; it acts as a helper class to the ArithmeticExpression
26 * class. There are many subclasses for the different kinds of arithmetic expression, and static methods
27 * that allow the right subclass to be selected, either at compile time or at run time.
28 */
29
30 public abstract class Calculator {
31
32 public static final int PLUS = 0;
33 public static final int MINUS = 1;
34 public static final int TIMES = 2;
35 public static final int DIV = 3;
36 public static final int MOD = 4;
37 public static final int IDIV = 5;
38
39 /**
40 * Get the token number corresponding to the operator number
41 * @param operator the Calculator operator code
42 * @return the corresponding token
43 */
44
45 public static int getTokenFromOperator(int operator) {
46 return tokens[operator];
47 }
48
49 private static final int[] tokens = new int[] {Token.PLUS, Token.MINUS, Token.MULT, Token.DIV, Token.MOD, Token.IDIV};
50
51 /**
52 * Get a short code to identify the calculator in expression export files
53 * @return a short identifying code that can be used to reconstruct the calculator
54 */
55
56 public String code() {
57 String name = getClass().getSimpleName();
58 return name
59 .replaceAll("Any", "a")
60 .replaceAll("Double", "d")
61 .replaceAll("Float", "f")
62 .replaceAll("Decimal", "c")
63 .replaceAll("Integer", "i")
64 .replaceAll("Numeric", "n")
65 .replaceAll("DateTime", "t")
66 .replaceAll("Duration", "u")
67 .replaceAll("Plus", "+")
68 .replaceAll("Minus", "-")
69 .replaceAll("Times", "*")
70 .replaceAll("Div", "/")
71 .replaceAll("Idiv", "~")
72 .replaceAll("Mod", "%");
73 }
74
75 /**
76 * Calculators used for the six operators when the static type information does not allow
77 * a more specific calculator to be chosen
78 */
79
80 public final static Calculator[] ANY_ANY = {
81 new AnyPlusAny(),
82 new AnyMinusAny(),
83 new AnyTimesAny(),
84 new AnyDivAny(),
85 new AnyModAny(),
86 new AnyIdivAny()
87 };
88
89 // NOTE: these fields are public because they are referenced from the Java code generated by the XQuery compiler
90
91 /**
92 * Calculators used when the first operand is a double
93 */
94
95 public final static Calculator[] DOUBLE_DOUBLE = {
96 new DoublePlusDouble(),
97 new DoubleMinusDouble(),
98 new DoubleTimesDouble(),
99 new DoubleDivDouble(),
100 new DoubleModDouble(),
101 new DoubleIdivDouble()
102 };
103
104 public final static Calculator[] DOUBLE_FLOAT = DOUBLE_DOUBLE;
105 public final static Calculator[] DOUBLE_DECIMAL = DOUBLE_DOUBLE;
106 public final static Calculator[] DOUBLE_INTEGER = DOUBLE_DOUBLE;
107
108 /**
109 * Calculators used when the first operand is a float
110 */
111
112 public final static Calculator[] FLOAT_DOUBLE = DOUBLE_DOUBLE;
113 public final static Calculator[] FLOAT_FLOAT = {
114 new FloatPlusFloat(),
115 new FloatMinusFloat(),
116 new FloatTimesFloat(),
117 new FloatDivFloat(),
118 new FloatModFloat(),
119 new FloatIdivFloat()
120 };
121 public final static Calculator[] FLOAT_DECIMAL = FLOAT_FLOAT;
122 //public final static Calculator[] FLOAT_PDECIMAL = FLOAT_FLOAT;
123 public final static Calculator[] FLOAT_INTEGER = FLOAT_FLOAT;
124
125 /**
126 * Calculators used when the first operand is a decimal
127 */
128
129 public final static Calculator[] DECIMAL_DOUBLE = DOUBLE_DOUBLE;
130 public final static Calculator[] DECIMAL_FLOAT = FLOAT_FLOAT;
131 public final static Calculator[] DECIMAL_DECIMAL = {
132 new DecimalPlusDecimal(),
133 new DecimalMinusDecimal(),
134 new DecimalTimesDecimal(),
135 new DecimalDivDecimal(),
136 new DecimalModDecimal(),
137 new DecimalIdivDecimal()
138 };
139 public final static Calculator[] DECIMAL_INTEGER = DECIMAL_DECIMAL;
140
141 /**
142 * Calculators used when the first operand is an integer
143 */
144
145 public final static Calculator[] INTEGER_DOUBLE = DOUBLE_DOUBLE;
146 public final static Calculator[] INTEGER_FLOAT = FLOAT_FLOAT;
147 public final static Calculator[] INTEGER_DECIMAL = DECIMAL_DECIMAL;
148 public final static Calculator[] INTEGER_INTEGER = {
149 new IntegerPlusInteger(),
150 new IntegerMinusInteger(),
151 new IntegerTimesInteger(),
152 new IntegerDivInteger(),
153 new IntegerModInteger(),
154 new IntegerIdivInteger()
155 };
156
157 /**
158 * Calculators used when both operands are xs:dateTime, xs:date, or xs:time
159 */
160
161 /*@Nullable*/ public final static Calculator[] DATETIME_DATETIME = {
162 null,
163 new DateTimeMinusDateTime(),
164 null, null, null, null
165 };
166
167 /**
168 * Calculators used when the first operand is xs:dateTime, xs:date, or xs:time,
169 * and the second is a duration
170 */
171
172 public final static Calculator[] DATETIME_DURATION = {
173 new DateTimePlusDuration(),
174 new DateTimeMinusDuration(),
175 null, null, null, null
176 };
177
178 /**
179 * Calculators used when the second operand is xs:dateTime, xs:date, or xs:time,
180 * and the first is a duration
181 */
182
183 public final static Calculator[] DURATION_DATETIME = {
184 new DurationPlusDateTime(),
185 null, null, null, null, null
186 };
187
188 /**
189 * Calculators used when the both operands are durations
190 */
191
192 public final static Calculator[] DURATION_DURATION = {
193 new DurationPlusDuration(),
194 new DurationMinusDuration(),
195 null,
196 new DurationDivDuration(),
197 null, null
198 };
199
200 /**
201 * Calculators used when the first operand is a duration and the second is numeric
202 */
203
204 public final static Calculator[] DURATION_NUMERIC = {
205 null, null,
206 new DurationTimesNumeric(),
207 new DurationDivNumeric(),
208 null, null
209 };
210
211 /**
212 * Calculators used when the second operand is a duration and the first is numeric
213 */
214
215 public final static Calculator[] NUMERIC_DURATION = {
216 null, null,
217 new NumericTimesDuration(),
218 null, null, null
219 };
220
221 /**
222 * Table mapping argument types to the Calculator class used to implement them
223 */
224
225 private static IntHashMap<Calculator[]> table = new IntHashMap<>(100);
226 private static IntHashMap<String> nameTable = new IntHashMap<>(100);
227
228 private static void def(int typeA, int typeB, Calculator[] calculatorSet, String setName) {
229 int key = (typeA & 0xffff) << 16 | (typeB & 0xffff);
230 table.put(key, calculatorSet);
231 nameTable.put(key, setName);
232 // As well as the entries added directly, we also add derived entries for other types
233 // considered primitive
234 if (typeA == StandardNames.XS_DURATION) {
235 def(StandardNames.XS_DAY_TIME_DURATION, typeB, calculatorSet, setName);
236 def(StandardNames.XS_YEAR_MONTH_DURATION, typeB, calculatorSet, setName);
237 }
238 if (typeB == StandardNames.XS_DURATION) {
239 def(typeA, StandardNames.XS_DAY_TIME_DURATION, calculatorSet, setName);
240 def(typeA, StandardNames.XS_YEAR_MONTH_DURATION, calculatorSet, setName);
241 }
242 if (typeA == StandardNames.XS_DATE_TIME) {
243 def(StandardNames.XS_DATE, typeB, calculatorSet, setName);
244 def(StandardNames.XS_TIME, typeB, calculatorSet, setName);
245 }
246 if (typeB == StandardNames.XS_DATE_TIME) {
247 def(typeA, StandardNames.XS_DATE, calculatorSet, setName);
248 def(typeA, StandardNames.XS_TIME, calculatorSet, setName);
249 }
250 if (typeA == StandardNames.XS_DOUBLE) {
251 def(StandardNames.XS_UNTYPED_ATOMIC, typeB, calculatorSet, setName);
252 }
253 if (typeB == StandardNames.XS_DOUBLE) {
254 def(typeA, StandardNames.XS_UNTYPED_ATOMIC, calculatorSet, setName);
255 }
256 }
257
258 /**
259 * Factory method to get a calculator for a given combination of types
260 *
261 * @param typeA fingerprint of the primitive type of the first operand
262 * @param typeB fingerprint of the primitive type of the second operand
263 * @param operator the arithmetic operator in use
264 * @param mustResolve indicates that a concrete Calculator is required (rather than
265 * an ANY_ANY calculator which needs to be further resolved at run-time)
266 * @return null if no suitable Calculator can be found.
267 */
268
269 public static Calculator getCalculator(int typeA, int typeB, int operator, boolean mustResolve) {
270 int key = (typeA & 0xffff) << 16 | (typeB & 0xffff);
271 Calculator[] set = table.get(key);
272 if (set == null) {
273 if (mustResolve) {
274 return null;
275 } else {
276 return ANY_ANY[operator];
277 }
278 } else {
279 return set[operator];
280 }
281 }
282
283 /**
284 * Get a calculator given the short code used in the exported expression tree
285 * @param code the short code, e.g. i+i for IntegerPlusInteger
286 * @return the appropriate Calculator
287 */
288
289 public static Calculator reconstructCalculator(String code) {
290 int typeA = typeFromCode(code.charAt(0));
291 int typeB = typeFromCode(code.charAt(2));
292 int operator = operatorFromCode(code.charAt(1));
293 return getCalculator(typeA, typeB, operator, false);
294 }
295
296 private static int typeFromCode(char code) {
297 switch (code) {
298 case 'a':
299 return StandardNames.XS_ANY_ATOMIC_TYPE;
300 case 'd':
301 return StandardNames.XS_DOUBLE;
302 case 'i':
303 return StandardNames.XS_INTEGER;
304 case 'f':
305 return StandardNames.XS_FLOAT;
306 case 'c':
307 return StandardNames.XS_DECIMAL;
308 case 'n':
309 return StandardNames.XS_NUMERIC;
310 case 't':
311 return StandardNames.XS_DATE_TIME;
312 case 'u':
313 return StandardNames.XS_DURATION;
314 default:
315 throw new AssertionError();
316 }
317 }
318
319 public static int operatorFromCode(char code) {
320 switch (code) {
321 case '+':
322 return PLUS;
323 case '-':
324 return MINUS;
325 case '*':
326 return TIMES;
327 case '/':
328 return DIV;
329 case '~':
330 return IDIV;
331 case '%':
332 return MOD;
333 default:
334 throw new AssertionError();
335 }
336 }
337
338 /**
339 * Get the name of the calculator set for a given combination of types
340 *
341 * @param typeA the fingerprint of the primitive type of the first operand
342 * @param typeB the fingerprint of the primitive type of the second operand
343 * @return null if no suitable Calculator can be found.
344 */
345
346 public static String getCalculatorSetName(int typeA, int typeB) {
347 int key = (typeA & 0xffff) << 16 | (typeB & 0xffff);
348 return nameTable.get(key);
349 }
350
351 static {
352 def(StandardNames.XS_DOUBLE, StandardNames.XS_DOUBLE, DOUBLE_DOUBLE, "DOUBLE_DOUBLE");
353 def(StandardNames.XS_DOUBLE, StandardNames.XS_FLOAT, DOUBLE_FLOAT, "DOUBLE_FLOAT");
354 def(StandardNames.XS_DOUBLE, StandardNames.XS_DECIMAL, DOUBLE_DECIMAL, "DOUBLE_DECIMAL");
355 def(StandardNames.XS_DOUBLE, StandardNames.XS_INTEGER, DOUBLE_INTEGER, "DOUBLE_INTEGER");
356 def(StandardNames.XS_FLOAT, StandardNames.XS_DOUBLE, FLOAT_DOUBLE, "FLOAT_DOUBLE");
357 def(StandardNames.XS_FLOAT, StandardNames.XS_FLOAT, FLOAT_FLOAT, "FLOAT_FLOAT");
358 def(StandardNames.XS_FLOAT, StandardNames.XS_DECIMAL, FLOAT_DECIMAL, "FLOAT_DECIMAL");
359 def(StandardNames.XS_FLOAT, StandardNames.XS_INTEGER, FLOAT_INTEGER, "FLOAT_INTEGER");
360 def(StandardNames.XS_DECIMAL, StandardNames.XS_DOUBLE, DECIMAL_DOUBLE, "DECIMAL_DOUBLE");
361 def(StandardNames.XS_DECIMAL, StandardNames.XS_FLOAT, DECIMAL_FLOAT, "DECIMAL_FLOAT");
362 def(StandardNames.XS_DECIMAL, StandardNames.XS_DECIMAL, DECIMAL_DECIMAL, "DECIMAL_DECIMAL");
363 def(StandardNames.XS_DECIMAL, StandardNames.XS_INTEGER, DECIMAL_INTEGER, "DECIMAL_INTEGER");
364 def(StandardNames.XS_INTEGER, StandardNames.XS_DOUBLE, INTEGER_DOUBLE, "INTEGER_DOUBLE");
365 def(StandardNames.XS_INTEGER, StandardNames.XS_FLOAT, INTEGER_FLOAT, "INTEGER_FLOAT");
366 def(StandardNames.XS_INTEGER, StandardNames.XS_DECIMAL, INTEGER_DECIMAL, "INTEGER_DECIMAL");
367 def(StandardNames.XS_INTEGER, StandardNames.XS_INTEGER, INTEGER_INTEGER, "INTEGER_INTEGER");
368 def(StandardNames.XS_DATE_TIME, StandardNames.XS_DATE_TIME, DATETIME_DATETIME, "DATETIME_DATETIME");
369 def(StandardNames.XS_DATE_TIME, StandardNames.XS_DURATION, DATETIME_DURATION, "DATETIME_DURATION");
370 def(StandardNames.XS_DURATION, StandardNames.XS_DATE_TIME, DURATION_DATETIME, "DURATION_DATETIME");
371 def(StandardNames.XS_DURATION, StandardNames.XS_DURATION, DURATION_DURATION, "DURATION_DURATION");
372 def(StandardNames.XS_DURATION, StandardNames.XS_DOUBLE, DURATION_NUMERIC, "DURATION_NUMERIC");
373 def(StandardNames.XS_DURATION, StandardNames.XS_FLOAT, DURATION_NUMERIC, "DURATION_NUMERIC");
374 def(StandardNames.XS_DURATION, StandardNames.XS_DECIMAL, DURATION_NUMERIC, "DURATION_NUMERIC");
375 def(StandardNames.XS_DURATION, StandardNames.XS_INTEGER, DURATION_NUMERIC, "DURATION_NUMERIC");
376 def(StandardNames.XS_DOUBLE, StandardNames.XS_DURATION, NUMERIC_DURATION, "NUMERIC_DURATION");
377 def(StandardNames.XS_FLOAT, StandardNames.XS_DURATION, NUMERIC_DURATION, "NUMERIC_DURATION");
378 def(StandardNames.XS_DECIMAL, StandardNames.XS_DURATION, NUMERIC_DURATION, "NUMERIC_DURATION");
379 def(StandardNames.XS_INTEGER, StandardNames.XS_DURATION, NUMERIC_DURATION, "NUMERIC_DURATION");
380 }
381
382 /**
383 * Perform an arithmetic operation
384 *
385 * @param a the first operand. Must not be null, and must be an instance of the type implied by the
386 * class name.
387 * @param b the second operand. Must not be null, and must be an instance of the type implied by the
388 * class name.
389 * @param c the XPath dynamic evaluation context
390 * @return the result of the computation, as a value of the correct primitive type
391 * @throws XPathException in the event of an arithmetic error
392 */
393
394 public abstract AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException;
395
396 /**
397 * Get the type of the result of the calculator, given arguments types typeA and typeB
398 *
399 * @param typeA the type of the first operand
400 * @param typeB the type of the second operand
401 * @return the type of the result
402 */
403
404 public abstract AtomicType getResultType(AtomicType typeA, AtomicType typeB);
405
406 /**
407 * Arithmetic: anyAtomicType + AnyAtomicType
408 */
409
410 public static class AnyPlusAny extends Calculator {
411 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
412 Calculator calc = getCalculator(
413 a.getItemType().getPrimitiveType(), b.getItemType().getPrimitiveType(), PLUS, true);
414 if (calc == null) {
415 throw new XPathException("Unsuitable types for + operation (" +
416 Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ")", "XPTY0004", c);
417 } else {
418 return calc.compute(a, b, c);
419 }
420 }
421
422 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
423 return BuiltInAtomicType.ANY_ATOMIC;
424 }
425 }
426
427 /**
428 * Arithmetic: anyAtomicType - AnyAtomicType
429 */
430
431 public static class AnyMinusAny extends Calculator {
432 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
433 Calculator calc = getCalculator(
434 a.getItemType().getPrimitiveType(), b.getItemType().getPrimitiveType(), MINUS, true);
435 if (calc == null) {
436 throw new XPathException("Unsuitable types for - operation (" +
437 Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ")", "XPTY0004", c);
438 } else {
439 return calc.compute(a, b, c);
440 }
441 }
442
443 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
444 return BuiltInAtomicType.ANY_ATOMIC;
445 }
446 }
447
448 /**
449 * Arithmetic: anyAtomicType * AnyAtomicType
450 */
451
452 public static class AnyTimesAny extends Calculator {
453 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
454 Calculator calc = getCalculator(
455 a.getItemType().getPrimitiveType(), b.getItemType().getPrimitiveType(), TIMES, true);
456 if (calc == null) {
457 throw new XPathException("Unsuitable types for * operation (" +
458 Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ")", "XPTY0004", c);
459 } else {
460 return calc.compute(a, b, c);
461 }
462 }
463
464 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
465 return BuiltInAtomicType.ANY_ATOMIC;
466 }
467 }
468
469 /**
470 * Arithmetic: anyAtomicType div AnyAtomicType
471 */
472
473 public static class AnyDivAny extends Calculator {
474 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
475 Calculator calc = getCalculator(
476 a.getItemType().getPrimitiveType(), b.getItemType().getPrimitiveType(), DIV, true);
477 if (calc == null) {
478 throw new XPathException("Unsuitable types for div operation (" +
479 Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ")", "XPTY0004", c);
480 } else {
481 return calc.compute(a, b, c);
482 }
483 }
484
485 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
486 return BuiltInAtomicType.ANY_ATOMIC;
487 }
488 }
489
490 /**
491 * Arithmetic: anyAtomicType mod AnyAtomicType
492 */
493
494 public static class AnyModAny extends Calculator {
495 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
496 Calculator calc = getCalculator(
497 a.getItemType().getPrimitiveType(), b.getItemType().getPrimitiveType(), MOD, true);
498 if (calc == null) {
499 throw new XPathException("Unsuitable types for mod operation (" +
500 Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ")", "XPTY0004", c);
501 } else {
502 return calc.compute(a, b, c);
503 }
504 }
505
506 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
507 return BuiltInAtomicType.ANY_ATOMIC;
508 }
509 }
510
511 /**
512 * Arithmetic: anyAtomicType idiv AnyAtomicType
513 */
514
515 public static class AnyIdivAny extends Calculator {
516 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
517 Calculator calc = getCalculator(
518 a.getItemType().getPrimitiveType(), b.getItemType().getPrimitiveType(), IDIV, true);
519 if (calc == null) {
520 throw new XPathException("Unsuitable types for idiv operation (" +
521 Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ")", "XPTY0004", c);
522 } else {
523 return calc.compute(a, b, c);
524 }
525 }
526
527 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
528 return BuiltInAtomicType.ANY_ATOMIC;
529 }
530 }
531
532 /**
533 * Arithmetic: double + double (including types that promote to double)
534 */
535
536 public static class DoublePlusDouble extends Calculator {
537 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
538 return new DoubleValue(((NumericValue) a).getDoubleValue() + ((NumericValue) b).getDoubleValue());
539 }
540
541 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
542 return BuiltInAtomicType.DOUBLE;
543 }
544 }
545
546 /**
547 * Arithmetic: double - double (including types that promote to double)
548 */
549
550 public static class DoubleMinusDouble extends Calculator {
551 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
552 return new DoubleValue(((NumericValue) a).getDoubleValue() - ((NumericValue) b).getDoubleValue());
553 }
554
555 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
556 return BuiltInAtomicType.DOUBLE;
557 }
558 }
559
560 /**
561 * Arithmetic: double * double (including types that promote to double)
562 */
563
564 public static class DoubleTimesDouble extends Calculator {
565 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
566 return new DoubleValue(((NumericValue) a).getDoubleValue() * ((NumericValue) b).getDoubleValue());
567 }
568
569 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
570 return BuiltInAtomicType.DOUBLE;
571 }
572 }
573
574 /**
575 * Arithmetic: double div double (including types that promote to double)
576 */
577
578 public static class DoubleDivDouble extends Calculator {
579 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
580 return new DoubleValue(((NumericValue) a).getDoubleValue() / ((NumericValue) b).getDoubleValue());
581 }
582
583 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
584 return BuiltInAtomicType.DOUBLE;
585 }
586 }
587
588 /**
589 * Arithmetic: double mod double (including types that promote to double)
590 */
591
592 public static class DoubleModDouble extends Calculator {
593 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
594 return new DoubleValue(((NumericValue) a).getDoubleValue() % ((NumericValue) b).getDoubleValue());
595 }
596
597 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
598 return BuiltInAtomicType.DOUBLE;
599 }
600 }
601
602 /**
603 * Arithmetic: double idiv double (including types that promote to double)
604 */
605
606 private static class DoubleIdivDouble extends Calculator {
607 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
608 double A = ((NumericValue) a).getDoubleValue();
609 double B = ((NumericValue) b).getDoubleValue();
610 if (B == 0.0) {
611 throw new XPathException("Integer division by zero", "FOAR0001", c);
612 }
613 if (Double.isNaN(A) || Double.isInfinite(A)) {
614 throw new XPathException("First operand of idiv is NaN or infinity", "FOAR0002", c);
615 }
616 if (Double.isNaN(B)) {
617 throw new XPathException("Second operand of idiv is NaN", "FOAR0002", c);
618 }
619 return IntegerValue.makeIntegerValue(new DoubleValue(A / B)).asAtomic();
620 }
621
622 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
623 return BuiltInAtomicType.INTEGER;
624 }
625 }
626
627 /**
628 * Arithmetic: float + float (including types that promote to float)
629 */
630
631 public static class FloatPlusFloat extends Calculator {
632 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
633 return new FloatValue(((NumericValue) a).getFloatValue() + ((NumericValue) b).getFloatValue());
634 }
635
636 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
637 return BuiltInAtomicType.FLOAT;
638 }
639 }
640
641 /**
642 * Arithmetic: float - float (including types that promote to float)
643 */
644
645 public static class FloatMinusFloat extends Calculator {
646 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
647 return new FloatValue(((NumericValue) a).getFloatValue() - ((NumericValue) b).getFloatValue());
648 }
649
650 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
651 return BuiltInAtomicType.FLOAT;
652 }
653 }
654
655 /**
656 * Arithmetic: float * float (including types that promote to float)
657 */
658
659 public static class FloatTimesFloat extends Calculator {
660 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
661 return new FloatValue(((NumericValue) a).getFloatValue() * ((NumericValue) b).getFloatValue());
662 }
663
664 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
665 return BuiltInAtomicType.FLOAT;
666 }
667 }
668
669 /**
670 * Arithmetic: float div float (including types that promote to float)
671 */
672
673 public static class FloatDivFloat extends Calculator {
674 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
675 return new FloatValue(((NumericValue) a).getFloatValue() / ((NumericValue) b).getFloatValue());
676 }
677
678 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
679 return BuiltInAtomicType.FLOAT;
680 }
681 }
682
683 /**
684 * Arithmetic: float mod float (including types that promote to float)
685 */
686
687 public static class FloatModFloat extends Calculator {
688 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
689 return new FloatValue(((NumericValue) a).getFloatValue() % ((NumericValue) b).getFloatValue());
690 }
691
692 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
693 return BuiltInAtomicType.FLOAT;
694 }
695 }
696
697 /**
698 * Arithmetic: float idiv float (including types that promote to float)
699 */
700
701 public static class FloatIdivFloat extends Calculator {
702 public IntegerValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
703 float A = ((NumericValue) a).getFloatValue();
704 float B = ((NumericValue) b).getFloatValue();
705 if (B == 0.0) {
706 throw new XPathException("Integer division by zero", "FOAR0001", c);
707 }
708 if (Float.isNaN(A) || Float.isInfinite(A)) {
709 throw new XPathException("First operand of idiv is NaN or infinity", "FOAR0002", c);
710 }
711 if (Float.isNaN(B)) {
712 throw new XPathException("Second operand of idiv is NaN", "FOAR0002", c);
713 }
714 float quotient = A / B;
715 if (Float.isInfinite(quotient)) {
716 // bug 29171, test cbcl-numeric-idivide-008
717 return new DecimalIdivDecimal().compute(
718 new BigDecimalValue(((NumericValue) a).getDecimalValue()),
719 new BigDecimalValue(((NumericValue) b).getDecimalValue()),
720 c);
721 } else {
722 return (IntegerValue)Converter.FloatToInteger.INSTANCE.convert(new FloatValue(quotient)).asAtomic();
723 }
724 }
725
726 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
727 return BuiltInAtomicType.INTEGER;
728 }
729 }
730
731 /**
732 * Arithmetic: decimal + decimal (including types that promote to decimal, that is, integer)
733 */
734
735 public static class DecimalPlusDecimal extends Calculator {
736 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
737 if (a instanceof IntegerValue && b instanceof IntegerValue) {
738 return ((IntegerValue) a).plus((IntegerValue) b);
739 } else {
740 return new BigDecimalValue(
741 ((NumericValue) a).getDecimalValue().add(((NumericValue) b).getDecimalValue()));
742 }
743 }
744
745 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
746 return BuiltInAtomicType.DECIMAL;
747 }
748 }
749
750 /**
751 * Arithmetic: decimal - decimal (including types that promote to decimal, that is, integer)
752 */
753
754 public static class DecimalMinusDecimal extends Calculator {
755 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
756 if (a instanceof IntegerValue && b instanceof IntegerValue) {
757 return ((IntegerValue) a).minus((IntegerValue) b);
758 } else {
759 return new BigDecimalValue(
760 ((NumericValue) a).getDecimalValue().subtract(((NumericValue) b).getDecimalValue()));
761 }
762 }
763
764 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
765 return BuiltInAtomicType.DECIMAL;
766 }
767 }
768
769 /**
770 * Arithmetic: decimal * decimal (including types that promote to decimal, that is, integer)
771 */
772
773 public static class DecimalTimesDecimal extends Calculator {
774 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
775 if (a instanceof IntegerValue && b instanceof IntegerValue) {
776 return ((IntegerValue) a).times((IntegerValue) b);
777 } else {
778 return new BigDecimalValue(
779 ((NumericValue) a).getDecimalValue().multiply(((NumericValue) b).getDecimalValue()));
780 }
781 }
782
783 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
784 return BuiltInAtomicType.DECIMAL;
785 }
786 }
787
788 /**
789 * Arithmetic: decimal div decimal (including types that promote to decimal, that is, integer)
790 */
791
792 public static class DecimalDivDecimal extends Calculator {
793 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
794 return decimalDivide((NumericValue) a, (NumericValue) b);
795 }
796
797 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
798 return BuiltInAtomicType.DECIMAL;
799 }
800 }
801
802 public static BigDecimalValue decimalDivide(NumericValue a, NumericValue b) throws XPathException {
803 final BigDecimal A = a.getDecimalValue();
804 final BigDecimal B = b.getDecimalValue();
805 // int scale = Math.max(DecimalValue.DIVIDE_PRECISION,
806 // Math.max(A.scale(), B.scale()));
807 int scale = Math.max(BigDecimalValue.DIVIDE_PRECISION, A.scale() - B.scale() + BigDecimalValue.DIVIDE_PRECISION);
808
809 try {
810 BigDecimal result = A.divide(B, scale, RoundingMode.HALF_DOWN);
811 return new BigDecimalValue(result);
812 } catch (ArithmeticException err) {
813 if (b.compareTo(0) == 0) {
814 throw new XPathException("Decimal divide by zero", "FOAR0001");
815 } else {
816 throw err;
817 }
818 }
819 }
820
821 /**
822 * Arithmetic: decimal mod decimal (including types that promote to decimal, that is, integer)
823 */
824
825 public static class DecimalModDecimal extends Calculator {
826 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
827 if (a instanceof IntegerValue && b instanceof IntegerValue) {
828 return ((IntegerValue) a).mod((IntegerValue) b);
829 }
830
831 final BigDecimal A = ((NumericValue) a).getDecimalValue();
832 final BigDecimal B = ((NumericValue) b).getDecimalValue();
833 try {
834 //BigDecimal quotient = A.divide(B, 0, BigDecimal.ROUND_DOWN);
835 //BigDecimal remainder = A.subtract(quotient.multiply(B));
836 return new BigDecimalValue(A.remainder(B));
837 } catch (ArithmeticException err) {
838 if (((NumericValue) b).compareTo(0) == 0) {
839 throw new XPathException("Decimal modulo zero", "FOAR0001", c);
840 } else {
841 throw err;
842 }
843 }
844 }
845
846 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
847 return BuiltInAtomicType.DECIMAL;
848 }
849 }
850
851 /**
852 * Arithmetic: decimal idiv decimal (including types that promote to decimal, that is, integer)
853 */
854
855 public static class DecimalIdivDecimal extends Calculator {
856 public IntegerValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
857 if (a instanceof IntegerValue && b instanceof IntegerValue) {
858 return ((IntegerValue) a).idiv((IntegerValue) b);
859 }
860
861 final BigDecimal A = ((NumericValue) a).getDecimalValue();
862 final BigDecimal B = ((NumericValue) b).getDecimalValue();
863 if (B.signum() == 0) {
864 throw new XPathException("Integer division by zero", "FOAR0001", c);
865 }
866 //BigInteger quot = A.divide(B, 0, BigDecimal.ROUND_DOWN).toBigInteger();
867 BigInteger quot = A.divideToIntegralValue(B).toBigInteger();
868 return BigIntegerValue.makeIntegerValue(quot);
869 }
870
871 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
872 return BuiltInAtomicType.INTEGER;
873 }
874 }
875
876 /**
877 * Arithmetic: integer + integer
878 */
879
880 public static class IntegerPlusInteger extends Calculator {
881 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
882 return ((IntegerValue) a).plus((IntegerValue) b);
883 }
884
885 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
886 return BuiltInAtomicType.INTEGER;
887 }
888 }
889
890 /**
891 * Arithmetic: integer - integer
892 */
893
894 public static class IntegerMinusInteger extends Calculator {
895 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
896 return ((IntegerValue) a).minus((IntegerValue) b);
897 }
898
899 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
900 return BuiltInAtomicType.INTEGER;
901 }
902 }
903
904 /**
905 * Arithmetic: integer * integer
906 */
907
908 public static class IntegerTimesInteger extends Calculator {
909 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
910 return ((IntegerValue) a).times((IntegerValue) b);
911 }
912
913 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
914 return BuiltInAtomicType.INTEGER;
915 }
916 }
917
918 /**
919 * Arithmetic: integer div integer
920 */
921
922 public static class IntegerDivInteger extends Calculator {
923 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
924 return ((IntegerValue) a).div((IntegerValue) b);
925 }
926
927 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
928 return BuiltInAtomicType.DECIMAL;
929 }
930 }
931
932 /**
933 * Arithmetic: integer mod integer
934 */
935
936 public static class IntegerModInteger extends Calculator {
937 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
938 return ((IntegerValue) a).mod((IntegerValue) b);
939 }
940
941 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
942 return BuiltInAtomicType.INTEGER;
943 }
944 }
945
946 /**
947 * Arithmetic: integer idiv integer
948 */
949
950 public static class IntegerIdivInteger extends Calculator {
951 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
952 return ((IntegerValue) a).idiv((IntegerValue) b);
953 }
954
955 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
956 return BuiltInAtomicType.INTEGER;
957 }
958 }
959
960 /**
961 * Arithmetic: date/time/dateTime - date/time/dateTime
962 */
963
964 private static class DateTimeMinusDateTime extends Calculator {
965 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
966 return ((CalendarValue) a).subtract((CalendarValue) b, c);
967 }
968
969 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
970 return BuiltInAtomicType.DAY_TIME_DURATION;
971 }
972 }
973
974 /**
975 * Arithmetic: date/time/dateTime + duration
976 */
977
978 private static class DateTimePlusDuration extends Calculator {
979 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
980 return ((CalendarValue) a).add((DurationValue) b);
981 }
982
983 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
984 return typeA;
985 }
986 }
987
988 /**
989 * Arithmetic: date/time/dateTime - duration
990 */
991
992 private static class DateTimeMinusDuration extends Calculator {
993 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
994 return ((CalendarValue) a).add(((DurationValue) b).negate());
995 }
996
997 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
998 return typeA;
999 }
1000 }
1001
1002 /**
1003 * Arithmetic: duration + date/time/dateTime
1004 */
1005
1006 private static class DurationPlusDateTime extends Calculator {
1007 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
1008 return ((CalendarValue) b).add((DurationValue) a);
1009 }
1010
1011 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
1012 return typeB;
1013 }
1014 }
1015
1016 /**
1017 * Arithmetic: duration + duration
1018 */
1019
1020 private static class DurationPlusDuration extends Calculator {
1021 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
1022 return ((DurationValue) a).add((DurationValue) b);
1023 }
1024
1025 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
1026 return typeA;
1027 }
1028 }
1029
1030 /**
1031 * Arithmetic: duration - duration
1032 */
1033
1034 private static class DurationMinusDuration extends Calculator {
1035 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
1036 return ((DurationValue) a).subtract((DurationValue) b);
1037 }
1038
1039 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
1040 return typeA;
1041 }
1042 }
1043
1044 /**
1045 * Arithmetic: duration div duration
1046 */
1047
1048 private static class DurationDivDuration extends Calculator {
1049 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
1050 return ((DurationValue) a).divide((DurationValue) b);
1051 }
1052
1053 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
1054 return BuiltInAtomicType.DECIMAL;
1055 }
1056 }
1057
1058 /**
1059 * Arithmetic: duration * number
1060 */
1061
1062 private static class DurationTimesNumeric extends Calculator {
1063 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
1064 if (b instanceof Int64Value) {
1065 return ((DurationValue) a).multiply(((Int64Value) b).longValue());
1066 } else {
1067 return ((DurationValue) a).multiply(((NumericValue) b).getDoubleValue());
1068 }
1069 }
1070
1071 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
1072 return typeA;
1073 }
1074 }
1075
1076 /**
1077 * Arithmetic: number * duration
1078 */
1079
1080 private static class NumericTimesDuration extends Calculator {
1081 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
1082 if (a instanceof Int64Value) {
1083 return ((DurationValue) b).multiply(((Int64Value) a).longValue());
1084 } else {
1085 return ((DurationValue) b).multiply(((NumericValue) a).getDoubleValue());
1086 }
1087 }
1088
1089 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
1090 return typeB;
1091 }
1092 }
1093
1094 /**
1095 * Arithmetic: duration div number
1096 */
1097
1098 private static class DurationDivNumeric extends Calculator {
1099 public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
1100 double d = 1.0 / ((NumericValue) b).getDoubleValue();
1101 return ((DurationValue) a).multiply(d);
1102 }
1103
1104 public AtomicType getResultType(AtomicType typeA, AtomicType typeB) {
1105 return typeA;
1106 }
1107 }
1108
1109 }
1110