| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.value; |
| 9 |
|
| 10 |
import net.sf.saxon.om.SequenceTool; |
| 11 |
import net.sf.saxon.trans.Err; |
| 12 |
import net.sf.saxon.trans.XPathException; |
| 13 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 14 |
import net.sf.saxon.type.*; |
| 15 |
|
| 16 |
import java.math.BigDecimal; |
| 17 |
import java.math.BigInteger; |
| 18 |
import java.math.RoundingMode; |
| 19 |
import java.util.regex.Pattern; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public final class BigDecimalValue extends DecimalValue { |
| 29 |
|
| 30 |
public static final int DIVIDE_PRECISION = 18; |
| 31 |
|
| 32 |
private BigDecimal value; |
| 33 |
private Double doubleValue; |
| 34 |
|
| 35 |
public static final BigDecimal BIG_DECIMAL_ONE_MILLION = BigDecimal.valueOf(1_000_000); |
| 36 |
public static final BigDecimal BIG_DECIMAL_ONE_BILLION = BigDecimal.valueOf(1_000_000_000); |
| 37 |
|
| 38 |
public static final BigDecimalValue ZERO = new BigDecimalValue(BigDecimal.valueOf(0)); |
| 39 |
public static final BigDecimalValue ONE = new BigDecimalValue(BigDecimal.valueOf(1)); |
| 40 |
public static final BigDecimalValue TWO = new BigDecimalValue(BigDecimal.valueOf(2)); |
| 41 |
public static final BigDecimalValue THREE = new BigDecimalValue(BigDecimal.valueOf(3)); |
| 42 |
public static final BigDecimal MAX_INT = BigDecimal.valueOf(Integer.MAX_VALUE); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public BigDecimalValue(BigDecimal value) { |
| 51 |
this.value = value.stripTrailingZeros(); |
| 52 |
typeLabel = BuiltInAtomicType.DECIMAL; |
| 53 |
} |
| 54 |
|
| 55 |
private static final Pattern decimalPattern = Pattern.compile("(\\-|\\+)?((\\.[0-9]+)|([0-9]+(\\.[0-9]*)?))"); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static ConversionResult makeDecimalValue(CharSequence in, boolean validate) { |
| 67 |
|
| 68 |
try { |
| 69 |
return parse(in); |
| 70 |
} catch (NumberFormatException err) { |
| 71 |
ValidationFailure e = new ValidationFailure( |
| 72 |
"Cannot convert string " + Err.wrap(Whitespace.trim(in), Err.VALUE) + |
| 73 |
" to xs:decimal: " + err.getMessage()); |
| 74 |
e.setErrorCode("FORG0001"); |
| 75 |
return e; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public static BigDecimalValue parse(CharSequence in) throws NumberFormatException { |
| 89 |
FastStringBuffer digits = new FastStringBuffer(in.length()); |
| 90 |
int scale = 0; |
| 91 |
int state = 0; |
| 92 |
|
| 93 |
|
| 94 |
boolean foundDigit = false; |
| 95 |
int len = in.length(); |
| 96 |
for (int i = 0; i < len; i++) { |
| 97 |
char c = in.charAt(i); |
| 98 |
switch (c) { |
| 99 |
case ' ': |
| 100 |
case '\t': |
| 101 |
case '\r': |
| 102 |
case '\n': |
| 103 |
if (state != 0) { |
| 104 |
state = 5; |
| 105 |
} |
| 106 |
break; |
| 107 |
case '+': |
| 108 |
if (state != 0) { |
| 109 |
throw new NumberFormatException("unexpected sign"); |
| 110 |
} |
| 111 |
state = 1; |
| 112 |
break; |
| 113 |
case '-': |
| 114 |
if (state != 0) { |
| 115 |
throw new NumberFormatException("unexpected sign"); |
| 116 |
} |
| 117 |
state = 1; |
| 118 |
digits.append(c); |
| 119 |
break; |
| 120 |
case '0': |
| 121 |
case '1': |
| 122 |
case '2': |
| 123 |
case '3': |
| 124 |
case '4': |
| 125 |
case '5': |
| 126 |
case '6': |
| 127 |
case '7': |
| 128 |
case '8': |
| 129 |
case '9': |
| 130 |
if (state == 0) { |
| 131 |
state = 1; |
| 132 |
} else if (state >= 3) { |
| 133 |
scale++; |
| 134 |
} |
| 135 |
if (state == 5) { |
| 136 |
throw new NumberFormatException("contains embedded whitespace"); |
| 137 |
} |
| 138 |
digits.append(c); |
| 139 |
foundDigit = true; |
| 140 |
break; |
| 141 |
case '.': |
| 142 |
if (state == 5) { |
| 143 |
throw new NumberFormatException("contains embedded whitespace"); |
| 144 |
} |
| 145 |
if (state >= 3) { |
| 146 |
throw new NumberFormatException("more than one decimal point"); |
| 147 |
} |
| 148 |
state = 3; |
| 149 |
break; |
| 150 |
default: |
| 151 |
throw new NumberFormatException("invalid character '" + c + "'"); |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
if (!foundDigit) { |
| 157 |
throw new NumberFormatException("no digits in value"); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
while (scale > 0) { |
| 162 |
if (digits.charAt(digits.length() - 1) == '0') { |
| 163 |
digits.setLength(digits.length() - 1); |
| 164 |
scale--; |
| 165 |
} else { |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
if (digits.isEmpty() || (digits.length() == 1 && digits.charAt(0) == '-')) { |
| 170 |
return BigDecimalValue.ZERO; |
| 171 |
} |
| 172 |
BigInteger bigInt = new BigInteger(digits.toString()); |
| 173 |
BigDecimal bigDec = new BigDecimal(bigInt, scale); |
| 174 |
return new BigDecimalValue(bigDec); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public static boolean castableAsDecimal(CharSequence in) { |
| 185 |
CharSequence trimmed = Whitespace.trimWhitespace(in); |
| 186 |
return decimalPattern.matcher(trimmed).matches(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public BigDecimalValue(double in) throws ValidationException { |
| 197 |
try { |
| 198 |
BigDecimal d = new BigDecimal(in); |
| 199 |
value = d.stripTrailingZeros(); |
| 200 |
} catch (NumberFormatException err) { |
| 201 |
|
| 202 |
ValidationFailure e = new ValidationFailure( |
| 203 |
"Cannot convert double " + Err.wrap(in + "", Err.VALUE) + " to decimal"); |
| 204 |
e.setErrorCode("FOCA0002"); |
| 205 |
throw e.makeException(); |
| 206 |
} |
| 207 |
typeLabel = BuiltInAtomicType.DECIMAL; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public BigDecimalValue(long in) { |
| 217 |
value = BigDecimal.valueOf(in); |
| 218 |
typeLabel = BuiltInAtomicType.DECIMAL; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public AtomicValue copyAsSubType(AtomicType typeLabel) { |
| 229 |
BigDecimalValue v = new BigDecimalValue(value); |
| 230 |
v.typeLabel = typeLabel; |
| 231 |
return v; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public BuiltInAtomicType getPrimitiveType() { |
| 242 |
return BuiltInAtomicType.DECIMAL; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
@Override |
| 252 |
public double getDoubleValue() { |
| 253 |
if (doubleValue == null) { |
| 254 |
double d = value.doubleValue(); |
| 255 |
doubleValue = d; |
| 256 |
return d; |
| 257 |
} else { |
| 258 |
return doubleValue; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
@Override |
| 268 |
public float getFloatValue() { |
| 269 |
return (float) value.doubleValue(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
@Override |
| 281 |
public long longValue() throws XPathException { |
| 282 |
return (long) value.doubleValue(); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public BigDecimal getDecimalValue() { |
| 290 |
return value; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
public int hashCode() { |
| 300 |
BigDecimal round = value.setScale(0, RoundingMode.DOWN); |
| 301 |
long value = round.longValue(); |
| 302 |
if (value > Integer.MIN_VALUE && value < Integer.MAX_VALUE) { |
| 303 |
return (int) value; |
| 304 |
} else { |
| 305 |
return Double.valueOf(getDoubleValue()).hashCode(); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
public boolean effectiveBooleanValue() { |
| 310 |
return value.signum() != 0; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public CharSequence getCanonicalLexicalRepresentation() { |
| 329 |
String s = getStringValue(); |
| 330 |
if (s.indexOf('.') < 0) { |
| 331 |
s += ".0"; |
| 332 |
} |
| 333 |
return s; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public CharSequence getPrimitiveStringValue() { |
| 344 |
return decimalToString(value, new FastStringBuffer(FastStringBuffer.C16)); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public static FastStringBuffer decimalToString(BigDecimal value, FastStringBuffer fsb) { |
| 356 |
|
| 357 |
|
| 358 |
int scale = value.scale(); |
| 359 |
if (scale == 0) { |
| 360 |
fsb.append(value.toString()); |
| 361 |
return fsb; |
| 362 |
} else if (scale < 0) { |
| 363 |
String s = value.abs().unscaledValue().toString(); |
| 364 |
if (s.equals("0")) { |
| 365 |
fsb.append('0'); |
| 366 |
return fsb; |
| 367 |
} |
| 368 |
|
| 369 |
if (value.signum() < 0) { |
| 370 |
fsb.append('-'); |
| 371 |
} |
| 372 |
fsb.append(s); |
| 373 |
for (int i = 0; i < -scale; i++) { |
| 374 |
fsb.append('0'); |
| 375 |
} |
| 376 |
return fsb; |
| 377 |
} else { |
| 378 |
String s = value.abs().unscaledValue().toString(); |
| 379 |
if (s.equals("0")) { |
| 380 |
fsb.append('0'); |
| 381 |
return fsb; |
| 382 |
} |
| 383 |
int len = s.length(); |
| 384 |
|
| 385 |
if (value.signum() < 0) { |
| 386 |
fsb.append('-'); |
| 387 |
} |
| 388 |
if (scale >= len) { |
| 389 |
fsb.append("0."); |
| 390 |
for (int i = len; i < scale; i++) { |
| 391 |
fsb.append('0'); |
| 392 |
} |
| 393 |
fsb.append(s); |
| 394 |
} else { |
| 395 |
fsb.append(s.substring(0, len - scale)); |
| 396 |
fsb.append('.'); |
| 397 |
fsb.append(s.substring(len - scale)); |
| 398 |
} |
| 399 |
return fsb; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public NumericValue negate() { |
| 408 |
return new BigDecimalValue(value.negate()); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public NumericValue floor() { |
| 416 |
return new BigDecimalValue(value.setScale(0, RoundingMode.FLOOR)); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public NumericValue ceiling() { |
| 424 |
return new BigDecimalValue(value.setScale(0, RoundingMode.CEILING)); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public NumericValue round(int scale) { |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
if (scale >= value.scale()) { |
| 440 |
|
| 441 |
return this; |
| 442 |
} |
| 443 |
|
| 444 |
switch (value.signum()) { |
| 445 |
case -1: |
| 446 |
return new BigDecimalValue(value.setScale(scale, RoundingMode.HALF_DOWN)); |
| 447 |
case 0: |
| 448 |
return this; |
| 449 |
case +1: |
| 450 |
return new BigDecimalValue(value.setScale(scale, RoundingMode.HALF_UP)); |
| 451 |
default: |
| 452 |
|
| 453 |
return this; |
| 454 |
} |
| 455 |
|
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
public NumericValue roundHalfToEven(int scale) { |
| 463 |
if (scale >= value.scale()) { |
| 464 |
return this; |
| 465 |
} |
| 466 |
BigDecimal scaledValue = value.setScale(scale, RoundingMode.HALF_EVEN); |
| 467 |
return new BigDecimalValue(scaledValue.stripTrailingZeros()); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public int signum() { |
| 477 |
return value.signum(); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public boolean isWholeNumber() { |
| 486 |
return value.scale() == 0 || |
| 487 |
value.compareTo(value.setScale(0, RoundingMode.DOWN)) == 0; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
@Override |
| 497 |
public int asSubscript() { |
| 498 |
if (isWholeNumber() && value.compareTo(BigDecimal.ZERO) > 0 && value.compareTo(MAX_INT) <= 0) { |
| 499 |
try { |
| 500 |
return (int)longValue(); |
| 501 |
} catch (XPathException e) { |
| 502 |
return -1; |
| 503 |
} |
| 504 |
} else { |
| 505 |
return -1; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
public NumericValue abs() { |
| 517 |
if (value.signum() > 0) { |
| 518 |
return this; |
| 519 |
} else { |
| 520 |
return new BigDecimalValue(value.negate()); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
public int compareTo(NumericValue other) { |
| 529 |
if (NumericValue.isInteger((NumericValue) other)) { |
| 530 |
|
| 531 |
try { |
| 532 |
return value.compareTo(((NumericValue) other).getDecimalValue()); |
| 533 |
} catch (XPathException err) { |
| 534 |
throw new AssertionError("Conversion of integer to decimal should never fail"); |
| 535 |
} |
| 536 |
} else if (other instanceof BigDecimalValue) { |
| 537 |
return value.compareTo(((BigDecimalValue) other).value); |
| 538 |
} else if (other instanceof FloatValue) { |
| 539 |
return 0 - ((FloatValue) other).compareTo(this); |
| 540 |
} else { |
| 541 |
return super.compareTo(other); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
public int compareTo(long other) { |
| 553 |
if (other == 0) { |
| 554 |
return value.signum(); |
| 555 |
} |
| 556 |
return value.compareTo(BigDecimal.valueOf(other)); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
public Comparable getSchemaComparable() { |
| 574 |
return new DecimalComparable(this); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
protected static class DecimalComparable implements Comparable { |
| 583 |
|
| 584 |
protected BigDecimalValue value; |
| 585 |
|
| 586 |
public DecimalComparable(BigDecimalValue value) { |
| 587 |
this.value = value; |
| 588 |
} |
| 589 |
|
| 590 |
public BigDecimal asBigDecimal() { |
| 591 |
return value.getDecimalValue(); |
| 592 |
} |
| 593 |
|
| 594 |
public int compareTo(Object o) { |
| 595 |
if (o instanceof DecimalComparable) { |
| 596 |
return asBigDecimal().compareTo(((DecimalComparable) o).asBigDecimal()); |
| 597 |
} else if (o instanceof Int64Value.Int64Comparable) { |
| 598 |
return asBigDecimal().compareTo(BigDecimal.valueOf(((Int64Value.Int64Comparable) o).asLong())); |
| 599 |
} else if (o instanceof BigIntegerValue.BigIntegerComparable) { |
| 600 |
return asBigDecimal().compareTo(new BigDecimal(((BigIntegerValue.BigIntegerComparable) o).asBigInteger())); |
| 601 |
} else { |
| 602 |
return SequenceTool.INDETERMINATE_ORDERING; |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
public boolean equals(Object o) { |
| 607 |
return compareTo(o) == 0; |
| 608 |
} |
| 609 |
|
| 610 |
public int hashCode() { |
| 611 |
|
| 612 |
if (value.isWholeNumber()) { |
| 613 |
IntegerValue iv = Converter.DecimalToInteger.INSTANCE.convert(value); |
| 614 |
return iv.getSchemaComparable().hashCode(); |
| 615 |
|
| 616 |
} |
| 617 |
return value.hashCode(); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
public boolean isIdentical( AtomicValue v) { |
| 635 |
return (v instanceof DecimalValue) && equals(v); |
| 636 |
} |
| 637 |
|
| 638 |
} |
| 639 |
|