Debian Codegraph @

net/sf/saxon/value/Int64Value.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.value;
9
10 import net.sf.saxon.expr.Calculator;
11 import net.sf.saxon.om.SequenceTool;
12 import net.sf.saxon.om.StandardNames;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.AtomicType;
15 import net.sf.saxon.type.BuiltInAtomicType;
16 import net.sf.saxon.type.ValidationFailure;
17
18 import java.math.BigDecimal;
19 import java.math.BigInteger;
20
21 /**
22 * An integer value: note this is a subtype of decimal in XML Schema, not a primitive type.
23 * This class supports integer values in the range permitted by a Java "long",
24 * and also supports the built-in subtypes of xs:integer.
25 */
26
27 public final class Int64Value extends IntegerValue {
28
29 /**
30 * IntegerValue representing the value -1
31 */
32 public static final Int64Value MINUS_ONE = new Int64Value(-1);
33 /**
34 * IntegerValue representing the value zero
35 */
36 public static final Int64Value ZERO = new Int64Value(0);
37 /**
38 * IntegerValue representing the value +1
39 */
40 public static final Int64Value PLUS_ONE = new Int64Value(+1);
41 /**
42 * IntegerValue representing the maximum value for a long
43 */
44 public static final Int64Value MAX_LONG = new Int64Value(Long.MAX_VALUE);
45 /**
46 * IntegerValue representing the minimum value for a long
47 */
48 public static final Int64Value MIN_LONG = new Int64Value(Long.MIN_VALUE);
49 private long value;
50
51 /**
52 * Array of small integer values
53 */
54 private static final Int64Value[] SMALL_INTEGERS = {
55 new Int64Value(0),
56 new Int64Value(1),
57 new Int64Value(2),
58 new Int64Value(3),
59 new Int64Value(4),
60 new Int64Value(5),
61 new Int64Value(6),
62 new Int64Value(7),
63 new Int64Value(8),
64 new Int64Value(9),
65 new Int64Value(10),
66 new Int64Value(11),
67 new Int64Value(12),
68 new Int64Value(13),
69 new Int64Value(14),
70 new Int64Value(15),
71 new Int64Value(16),
72 new Int64Value(17),
73 new Int64Value(18),
74 new Int64Value(19),
75 new Int64Value(20)
76 };
77
78 /**
79 * Constructor supplying a long
80 *
81 * @param value the value of the IntegerValue
82 */
83
84 public Int64Value(long value) {
85 this.value = value;
86 typeLabel = BuiltInAtomicType.INTEGER;
87 }
88
89 /**
90 * Constructor for a subtype, supplying a long and a type label.
91 *
92 * @param val The supplied value, as an integer
93 * @param type the required item type, a subtype of xs:integer
94 * @param check Set to true if the method is required to check that the value is in range;
95 * false if the caller can guarantee that the value has already been checked.
96 * @throws XPathException if the supplied value is out of range for the
97 * target type
98 */
99
100 public Int64Value(long val, /*@NotNull*/ BuiltInAtomicType type, boolean check) throws XPathException {
101 value = val;
102 typeLabel = type;
103 if (check && !checkRange(value, type)) {
104 XPathException err = new XPathException("Integer value " + val +
105 " is out of range for the requested type " + type.getDescription());
106 err.setErrorCode("XPTY0004");
107 err.setIsTypeError(true);
108 throw err;
109 }
110 }
111
112 /**
113 * Factory method: allows Int64Value objects to be reused. Note that
114 * a value obtained using this method must not be modified to set a type label, because
115 * the value is in general shared.
116 *
117 * @param value the integer value
118 * @return an Int64Value with this integer value
119 */
120
121 public static Int64Value makeIntegerValue(long value) {
122 if (value <= 20 && value >= 0) {
123 return SMALL_INTEGERS[(int) value];
124 } else {
125 return new Int64Value(value);
126 }
127 }
128
129 /**
130 * Factory method to create a derived value, with no checking of the value against the
131 * derived type
132 *
133 * @param val the integer value
134 * @param type the subtype of xs:integer
135 * @return the constructed value
136 */
137
138 /*@NotNull*/
139 public static Int64Value makeDerived(long val, AtomicType type) {
140 Int64Value v = new Int64Value(val);
141 v.typeLabel = type;
142 return v;
143 }
144
145 /**
146 * Factory method returning the integer -1, 0, or +1 according as the argument
147 * is negative, zero, or positive
148 *
149 * @param val the value to be tested
150 * @return the Int64Value representing -1, 0, or +1
151 */
152
153 public static Int64Value signum(long val) {
154 if (val == 0) {
155 return ZERO;
156 } else {
157 return val < 0 ? MINUS_ONE : PLUS_ONE;
158 }
159 }
160
161 /**
162 * Test whether a number is a possible subscript into a sequence, that is,
163 * a whole number greater than zero and less than 2^31
164 *
165 * @return the number as an int if it is a possible subscript, or -1 otherwise
166 */
167 @Override
168 public int asSubscript() {
169 if (value > 0 && value <= Integer.MAX_VALUE) {
170 return (int)value;
171 } else {
172 return -1;
173 }
174 }
175
176 /**
177 * Create a copy of this atomic value, with a different type label
178 *
179 * @param typeLabel the type label of the new copy. The caller is responsible for checking that
180 * the value actually conforms to this type.
181 */
182
183 /*@NotNull*/
184 public AtomicValue copyAsSubType(/*@NotNull*/ AtomicType typeLabel) {
185 if (typeLabel.getPrimitiveType() == StandardNames.XS_INTEGER) {
186 Int64Value v = new Int64Value(value);
187 v.typeLabel = typeLabel;
188 return v;
189 } else {
190 return new BigDecimalValue(value);
191 }
192 }
193
194 /**
195 * Convert the value to a subtype of xs:integer
196 *
197 * @param subtype the target subtype
198 * @param validate true if validation is required; false if the caller already knows that the value is valid
199 * @return null if the conversion succeeds; a ValidationFailure describing the failure if it fails. Note
200 * that the exception is returned, not thrown.
201 */
202
203 /*@Nullable*/
204 public ValidationFailure convertToSubType(/*@NotNull*/ BuiltInAtomicType subtype, boolean validate) {
205 if (!validate) {
206 setSubType(subtype);
207 return null;
208 } else if (checkRange(subtype)) {
209 return null;
210 } else {
211 ValidationFailure err = new ValidationFailure("String " + value +
212 " cannot be converted to integer subtype " + subtype.getDescription());
213 err.setErrorCode("FORG0001");
214 return err;
215 }
216 }
217
218
219 /**
220 * This class allows subtypes of xs:integer to be held, as well as xs:integer values.
221 * This method sets the required type label. Note that this method modifies the value in situ.
222 *
223 * @param type the subtype of integer required
224 * @return null if the operation succeeds, or a ValidationException if the value is out of range
225 */
226
227 /*@Nullable*/
228 public ValidationFailure validateAgainstSubType(/*@NotNull*/ BuiltInAtomicType type) {
229 if (checkRange(value, type)) {
230 return null;
231 } else {
232 ValidationFailure err = new ValidationFailure("Value " + value +
233 " cannot be converted to integer subtype " + type.getDescription());
234 err.setErrorCode("FORG0001");
235 return err;
236 }
237 }
238
239 /**
240 * This class allows subtypes of xs:integer to be held, as well as xs:integer values.
241 * This method sets the required type label. It is the caller's responsibility to check that
242 * the value is within range.
243 *
244 * @param type the type label to be assigned
245 */
246 public void setSubType(AtomicType type) {
247 typeLabel = type;
248 }
249
250 /**
251 * This class allows subtypes of xs:integer to be held, as well as xs:integer values.
252 * This method checks that the value is within range, and also sets the type label.
253 *
254 * @param type the subtype of integer required
255 * @return true if successful, false if value is out of range for the subtype
256 */
257 public boolean checkRange(BuiltInAtomicType type) {
258 typeLabel = type;
259 return checkRange(value, type);
260 }
261
262 /**
263 * Get an object that implements XML Schema comparison semantics
264 */
265
266 /*@NotNull*/
267 public Comparable getSchemaComparable() {
268 return new Int64Comparable(this);
269 }
270
271 /**
272 * A Comparable that performs comparison of an Int64Value either with another
273 * Int64Value or with some other representation of an XPath numeric value
274 */
275
276 protected static class Int64Comparable implements Comparable {
277
278 protected Int64Value value;
279
280 public Int64Comparable(Int64Value value) {
281 this.value = value;
282 }
283
284 public long asLong() {
285 return value.longValue();
286 }
287
288 public int compareTo(/*@NotNull*/ Object o) {
289 if (o instanceof Int64Comparable) {
290 long long0 = value.longValue();
291 long long1 = ((Int64Comparable) o).value.longValue();
292 if (long0 <= long1) {
293 if (long0 == long1) {
294 return 0;
295 } else {
296 return -1;
297 }
298 } else {
299 return 1;
300 }
301 } else if (o instanceof BigIntegerValue.BigIntegerComparable) {
302 return value.asBigInteger().compareTo(((BigIntegerValue.BigIntegerComparable) o).asBigInteger());
303 } else if (o instanceof BigDecimalValue.DecimalComparable) {
304 return value.getDecimalValue().compareTo(((BigDecimalValue.DecimalComparable) o).asBigDecimal());
305 } else {
306 return SequenceTool.INDETERMINATE_ORDERING;
307 }
308 }
309
310 public boolean equals(/*@NotNull*/ Object o) {
311 if (o instanceof Int64Comparable) {
312 return asLong() == ((Int64Comparable) o).asLong();
313 } else {
314 return compareTo(o) == 0;
315 }
316 }
317
318 public int hashCode() {
319 // Must align with hashCodes for other subtypes of xs:decimal
320 return (int) asLong();
321 }
322
323 }
324
325 /**
326 * Get the hashCode. This must conform to the rules for other NumericValue hashcodes
327 *
328 * @see NumericValue#hashCode
329 */
330
331 public int hashCode() {
332 if (value > Integer.MIN_VALUE && value < Integer.MAX_VALUE) {
333 return (int) value;
334 } else {
335 return Double.valueOf(getDoubleValue()).hashCode();
336 }
337 }
338
339 /**
340 * Get the value
341 *
342 * @return the value of the xs:integer, as a Java long
343 */
344
345 public long longValue() {
346 return value;
347 }
348
349 /**
350 * Return the effective boolean value of this integer
351 *
352 * @return false if the integer is zero, otherwise true
353 */
354 public boolean effectiveBooleanValue() {
355 return value != 0;
356 }
357
358 /**
359 * Compare the value to another numeric value
360 *
361 * @param other the numeric value to be compared to this value
362 * @return -1 if this value is less than the other, 0 if they are equal,
363 * +1 if this value is greater
364 */
365
366 public int compareTo(NumericValue other) {
367 if (other instanceof Int64Value) {
368 return Long.compare(value, ((Int64Value) other).value);
369 } else if (other instanceof BigIntegerValue) {
370 return BigInteger.valueOf(value).compareTo(((BigIntegerValue) other).asBigInteger());
371 } else if (other instanceof BigDecimalValue) {
372 return new BigDecimal(value).compareTo(((BigDecimalValue)other).getDecimalValue());
373 } else {
374 return super.compareTo(other);
375 }
376 }
377
378 /**
379 * Compare the value to a long
380 *
381 * @param other the value to be compared with
382 * @return -1 if this is less, 0 if this is equal, +1 if this is greater or if this is NaN
383 */
384
385 public int compareTo(long other) {
386 if (value == other) return 0;
387 if (value < other) return -1;
388 return 1;
389 }
390
391 /**
392 * Get the value as a String
393 *
394 * @return a String representation of the value
395 */
396
397 public String getPrimitiveStringValue() {
398 return Long.toString(value);
399 }
400
401 /**
402 * Get the numeric value as a double
403 *
404 * @return A double representing this numeric value; NaN if it cannot be
405 * converted
406 */
407 public double getDoubleValue() {
408 return (double) value;
409 }
410
411 /**
412 * Get the numeric value converted to a float
413 *
414 * @return a float representing this numeric value; NaN if it cannot be converted
415 */
416
417 public float getFloatValue() {
418 return (float) value;
419 }
420
421 /**
422 * Get the numeric value converted to a decimal
423 *
424 * @return a decimal representing this numeric value;
425 */
426
427 public BigDecimal getDecimalValue() {
428 return BigDecimal.valueOf(value);
429 }
430
431 /**
432 * Negate the value
433 *
434 * @return the result of inverting the sign of the value
435 */
436
437 public NumericValue negate() {
438 if (value == Long.MIN_VALUE) {
439 return BigIntegerValue.makeIntegerValue(BigInteger.valueOf(value)).negate();
440 } else {
441 return new Int64Value(-value);
442 }
443 }
444
445 /**
446 * Implement the XPath floor() function
447 *
448 * @return the integer value, unchanged
449 */
450
451 /*@NotNull*/
452 public NumericValue floor() {
453 return this;
454 }
455
456 /**
457 * Implement the XPath ceiling() function
458 *
459 * @return the integer value, unchanged
460 */
461
462 /*@NotNull*/
463 public NumericValue ceiling() {
464 return this;
465 }
466
467 /**
468 * Implement the XPath round() function
469 *
470 * @param scale the scale (for example scale=2 rounds to 2 decimal places, scale=-2
471 * rounds to a multiple of 100); default value is zero which rounds to an integer
472 * @return the integer value, unchanged
473 */
474
475 public NumericValue round(int scale) {
476 if (scale >= 0 || value == 0) {
477 return this;
478 } else {
479 if (scale < -15) {
480 return new BigIntegerValue(value).round(scale);
481 }
482
483 long absolute = Math.abs(value);
484 long factor = 1;
485 for (long i = 1; i <= -scale; i++) {
486 factor *= 10;
487 }
488 long modulus = absolute % factor;
489 long rval = absolute - modulus;
490 long d = modulus * 2;
491
492 if (value > 0) {
493 if (d >= factor) {
494 rval += factor;
495 }
496 } else {
497 if (d > factor) {
498 rval += factor;
499 }
500 rval = -rval;
501 }
502 return new Int64Value(rval);
503 }
504 }
505
506 /**
507 * Implement the XPath round-to-half-even() function
508 *
509 * @param scale number of digits required after the decimal point; the
510 * value -2 (for example) means round to a multiple of 100
511 * @return if the scale is &gt;=0, return this value unchanged. Otherwise
512 * round it to a multiple of 10**-scale
513 */
514
515 public NumericValue roundHalfToEven(int scale) {
516 if (scale >= 0) {
517 return this;
518 } else {
519 if (scale < -15) {
520 return new BigIntegerValue(value).roundHalfToEven(scale);
521 }
522 long absolute = Math.abs(value);
523 long factor = 1;
524 for (long i = 1; i <= -scale; i++) {
525 factor *= 10;
526 }
527 long modulus = absolute % factor;
528 long rval = absolute - modulus;
529 long d = modulus * 2;
530 if (d > factor) {
531 rval += factor;
532 } else if (d < factor) {
533 // no-op
534 } else {
535 // round to even
536 if (rval % (2 * factor) == 0) {
537 // no-op
538 } else {
539 rval += factor;
540 }
541 }
542 if (value < 0) {
543 rval = -rval;
544 }
545 return new Int64Value(rval);
546 }
547 }
548
549 /**
550 * Determine whether the value is negative, zero, or positive
551 *
552 * @return -1 if negative, 0 if zero, +1 if positive, NaN if NaN
553 */
554
555 public int signum() {
556 if (value > 0) return +1;
557 if (value == 0) return 0;
558 return -1;
559 }
560
561 /**
562 * Get the absolute value as defined by the XPath abs() function
563 *
564 * @return the absolute value
565 */
566
567 public NumericValue abs() {
568 if (value > 0) {
569 return this;
570 } else if (value == Long.MIN_VALUE) {
571 return new BigIntegerValue(new BigInteger("9223372036854775808"));
572 } else {
573 return makeIntegerValue(-value);
574 }
575 }
576
577 /**
578 * Add another integer
579 */
580
581 public IntegerValue plus(/*@NotNull*/ IntegerValue other) {
582 // if either of the values is large, we use BigInteger arithmetic to be on the safe side
583 if (other instanceof Int64Value) {
584 long topa = (value >> 60) & 0xf;
585 if (topa != 0 && topa != 0xf) {
586 return new BigIntegerValue(value).plus(new BigIntegerValue(((Int64Value) other).value));
587 }
588 long topb = (((Int64Value) other).value >> 60) & 0xf;
589 if (topb != 0 && topb != 0xf) {
590 return new BigIntegerValue(value).plus(new BigIntegerValue(((Int64Value) other).value));
591 }
592 return makeIntegerValue(value + ((Int64Value) other).value);
593 } else {
594 return new BigIntegerValue(value).plus(other);
595 }
596 }
597
598 /**
599 * Subtract another integer
600 */
601
602 public IntegerValue minus(/*@NotNull*/ IntegerValue other) {
603 // if either of the values is large, we use BigInteger arithmetic to be on the safe side
604 if (other instanceof Int64Value) {
605 long topa = (value >> 60) & 0xf;
606 if (topa != 0 && topa != 0xf) {
607 return new BigIntegerValue(value).minus(new BigIntegerValue(((Int64Value) other).value));
608 }
609 long topb = (((Int64Value) other).value >> 60) & 0xf;
610 if (topb != 0 && topb != 0xf) {
611 return new BigIntegerValue(value).minus(new BigIntegerValue(((Int64Value) other).value));
612 }
613 return makeIntegerValue(value - ((Int64Value) other).value);
614 } else {
615 return new BigIntegerValue(value).minus(other);
616 }
617 }
618
619 /**
620 * Multiply by another integer
621 */
622
623 public IntegerValue times(/*@NotNull*/ IntegerValue other) {
624 // if either of the values is large, we use BigInteger arithmetic to be on the safe side
625 if (other instanceof Int64Value) {
626 if (isLong() || ((Int64Value) other).isLong()) {
627 return new BigIntegerValue(value).times(new BigIntegerValue(((Int64Value) other).value));
628 } else {
629 return makeIntegerValue(value * ((Int64Value) other).value);
630 }
631 } else {
632 return new BigIntegerValue(value).times(other);
633 }
634 }
635
636 /**
637 * Divide by another integer
638 */
639
640 public NumericValue div(/*@NotNull*/ IntegerValue other) throws XPathException {
641 // if either of the values is large, we use BigInteger arithmetic to be on the safe side
642 if (other instanceof Int64Value) {
643 long quotient = ((Int64Value) other).value;
644 if (quotient == 0) {
645 throw new XPathException("Integer division by zero", "FOAR0001");
646 }
647 if (isLong() || ((Int64Value) other).isLong()) {
648 return new BigIntegerValue(value).div(new BigIntegerValue(quotient));
649 }
650
651 // the result of dividing two integers is a decimal; but if
652 // one divides exactly by the other, we implement it as an integer
653 if (value % quotient == 0) {
654 return makeIntegerValue(value / quotient);
655 } else {
656 return Calculator.decimalDivide(new BigDecimalValue(value), new BigDecimalValue(quotient));
657 }
658 } else {
659 return new BigIntegerValue(value).div(other);
660 }
661 }
662
663 /**
664 * Take modulo another integer
665 */
666
667 public IntegerValue mod(/*@NotNull*/ IntegerValue other) throws XPathException {
668 // if either of the values is large, we use BigInteger arithmetic to be on the safe side
669 if (other instanceof Int64Value) {
670 long quotient = ((Int64Value) other).value;
671 if (quotient == 0) {
672 throw new XPathException("Integer modulo zero", "FOAR0001");
673 }
674 if (isLong() || ((Int64Value) other).isLong()) {
675 return new BigIntegerValue(value).mod(new BigIntegerValue(((Int64Value) other).value));
676 } else {
677 return makeIntegerValue(value % quotient);
678 }
679 } else {
680 return new BigIntegerValue(value).mod(other);
681 }
682 }
683
684 /**
685 * Integer divide by another integer
686 */
687
688 public IntegerValue idiv(/*@NotNull*/ IntegerValue other) throws XPathException {
689 // if either of the values is large, we use BigInteger arithmetic to be on the safe side
690 if (other instanceof Int64Value) {
691 if (isLong() || ((Int64Value) other).isLong()) {
692 return new BigIntegerValue(value).idiv(new BigIntegerValue(((Int64Value) other).value));
693 }
694 try {
695 return makeIntegerValue(value / ((Int64Value) other).value);
696 } catch (ArithmeticException err) {
697 XPathException e;
698 if ("/ by zero".equals(err.getMessage())) {
699 e = new XPathException("Integer division by zero", "FOAR0001");
700 } else {
701 e = new XPathException("Integer division failure", err);
702 }
703 throw e;
704 }
705 } else {
706 return new BigIntegerValue(value).idiv(other);
707 }
708 }
709
710 /**
711 * Test whether this value needs a long to hold it. Specifically, whether
712 * the absolute value is > 2^31.
713 * @return true if the value is too big to fit in a 32-bit int
714 */
715
716 private boolean isLong() {
717 long top = value >> 31;
718 return top != 0 && top != 0x1ffffffffL;
719 }
720
721 /**
722 * Get the value as a BigInteger
723 */
724
725 public BigInteger asBigInteger() {
726 return BigInteger.valueOf(value);
727 }
728
729 }
730