| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public final class Int64Value extends IntegerValue { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public static final Int64Value MINUS_ONE = new Int64Value(-1); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static final Int64Value ZERO = new Int64Value(0); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static final Int64Value PLUS_ONE = new Int64Value(+1); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public static final Int64Value MAX_LONG = new Int64Value(Long.MAX_VALUE); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static final Int64Value MIN_LONG = new Int64Value(Long.MIN_VALUE); |
| 49 |
private long value; |
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public Int64Value(long value) { |
| 85 |
this.value = value; |
| 86 |
typeLabel = BuiltInAtomicType.INTEGER; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public Int64Value(long val, 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 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 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 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public AtomicValue copyAsSubType( 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 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public ValidationFailure convertToSubType( 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 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public ValidationFailure validateAgainstSubType( 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 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public void setSubType(AtomicType type) { |
| 247 |
typeLabel = type; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public boolean checkRange(BuiltInAtomicType type) { |
| 258 |
typeLabel = type; |
| 259 |
return checkRange(value, type); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public Comparable getSchemaComparable() { |
| 268 |
return new Int64Comparable(this); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 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( 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( 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 |
|
| 320 |
return (int) asLong(); |
| 321 |
} |
| 322 |
|
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 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 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public long longValue() { |
| 346 |
return value; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public boolean effectiveBooleanValue() { |
| 355 |
return value != 0; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 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 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 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 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public String getPrimitiveStringValue() { |
| 398 |
return Long.toString(value); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public double getDoubleValue() { |
| 408 |
return (double) value; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public float getFloatValue() { |
| 418 |
return (float) value; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public BigDecimal getDecimalValue() { |
| 428 |
return BigDecimal.valueOf(value); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 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 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
public NumericValue floor() { |
| 453 |
return this; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public NumericValue ceiling() { |
| 464 |
return this; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 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 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 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 |
|
| 534 |
} else { |
| 535 |
|
| 536 |
if (rval % (2 * factor) == 0) { |
| 537 |
|
| 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 |
|
| 551 |
|
| 552 |
|
| 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 |
|
| 563 |
|
| 564 |
|
| 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 |
|
| 579 |
|
| 580 |
|
| 581 |
public IntegerValue plus( IntegerValue other) { |
| 582 |
|
| 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 |
|
| 600 |
|
| 601 |
|
| 602 |
public IntegerValue minus( IntegerValue other) { |
| 603 |
|
| 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 |
|
| 621 |
|
| 622 |
|
| 623 |
public IntegerValue times( IntegerValue other) { |
| 624 |
|
| 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 |
|
| 638 |
|
| 639 |
|
| 640 |
public NumericValue div( IntegerValue other) throws XPathException { |
| 641 |
|
| 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 |
|
| 652 |
|
| 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 |
|
| 665 |
|
| 666 |
|
| 667 |
public IntegerValue mod( IntegerValue other) throws XPathException { |
| 668 |
|
| 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 |
|
| 686 |
|
| 687 |
|
| 688 |
public IntegerValue idiv( IntegerValue other) throws XPathException { |
| 689 |
|
| 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 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
private boolean isLong() { |
| 717 |
long top = value >> 31; |
| 718 |
return top != 0 && top != 0x1ffffffffL; |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
public BigInteger asBigInteger() { |
| 726 |
return BigInteger.valueOf(value); |
| 727 |
} |
| 728 |
|
| 729 |
} |
| 730 |
|