1 /* 2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved 28 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved 29 * 30 * The original version of this source code and documentation is copyrighted 31 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These 32 * materials are provided under terms of a License Agreement between Taligent 33 * and Sun. This technology is protected by multiple US and International 34 * patents. This notice and attribution to Taligent may not be removed. 35 * Taligent is a registered trademark of Taligent, Inc. 36 * 37 */ 38 39 package java.text; 40 41 import java.math.BigDecimal; 42 import java.math.BigInteger; 43 import java.math.RoundingMode; 44 import sun.misc.FloatingDecimal; 45 46 /** 47 * Digit List. Private to DecimalFormat. 48 * Handles the transcoding 49 * between numeric values and strings of characters. Only handles 50 * non-negative numbers. The division of labor between DigitList and 51 * DecimalFormat is that DigitList handles the radix 10 representation 52 * issues; DecimalFormat handles the locale-specific issues such as 53 * positive/negative, grouping, decimal point, currency, and so on. 54 * 55 * A DigitList is really a representation of a floating point value. 56 * It may be an integer value; we assume that a double has sufficient 57 * precision to represent all digits of a long. 58 * 59 * The DigitList representation consists of a string of characters, 60 * which are the digits radix 10, from '0' to '9'. It also has a radix 61 * 10 exponent associated with it. The value represented by a DigitList 62 * object can be computed by mulitplying the fraction f, where 0 <= f < 1, 63 * derived by placing all the digits of the list to the right of the 64 * decimal point, by 10^exponent. 65 * 66 * @see Locale 67 * @see Format 68 * @see NumberFormat 69 * @see DecimalFormat 70 * @see ChoiceFormat 71 * @see MessageFormat 72 * @author Mark Davis, Alan Liu 73 */ 74 final class DigitList implements Cloneable { 75 /** 76 * The maximum number of significant digits in an IEEE 754 double, that 77 * is, in a Java double. This must not be increased, or garbage digits 78 * will be generated, and should not be decreased, or accuracy will be lost. 79 */ 80 public static final int MAX_COUNT = 19; // == Long.toString(Long.MAX_VALUE).length() 81 82 /** 83 * These data members are intentionally public and can be set directly. 84 * 85 * The value represented is given by placing the decimal point before 86 * digits[decimalAt]. If decimalAt is < 0, then leading zeros between 87 * the decimal point and the first nonzero digit are implied. If decimalAt 88 * is > count, then trailing zeros between the digits[count-1] and the 89 * decimal point are implied. 90 * 91 * Equivalently, the represented value is given by f * 10^decimalAt. Here 92 * f is a value 0.1 <= f < 1 arrived at by placing the digits in Digits to 93 * the right of the decimal. 94 * 95 * DigitList is normalized, so if it is non-zero, figits[0] is non-zero. We 96 * don't allow denormalized numbers because our exponent is effectively of 97 * unlimited magnitude. The count value contains the number of significant 98 * digits present in digits[]. 99 * 100 * Zero is represented by any DigitList with count == 0 or with each digits[i] 101 * for all i <= count == '0'. 102 */ 103 public int decimalAt = 0; 104 public int count = 0; 105 public char[] digits = new char[MAX_COUNT]; 106 107 private char[] data; 108 private RoundingMode roundingMode = RoundingMode.HALF_EVEN; 109 private boolean isNegative = false; 110 111 /** 112 * Return true if the represented number is zero. 113 */ 114 boolean isZero() { 115 for (int i=0; i < count; ++i) { 116 if (digits[i] != '0') { 117 return false; 118 } 119 } 120 return true; 121 } 122 123 /** 124 * Set the rounding mode 125 */ 126 void setRoundingMode(RoundingMode r) { 127 roundingMode = r; 128 } 129 130 /** 131 * Clears out the digits. 132 * Use before appending them. 133 * Typically, you set a series of digits with append, then at the point 134 * you hit the decimal point, you set myDigitList.decimalAt = myDigitList.count; 135 * then go on appending digits. 136 */ 137 public void clear () { 138 decimalAt = 0; 139 count = 0; 140 } 141 142 /** 143 * Appends a digit to the list, extending the list when necessary. 144 */ 145 public void append(char digit) { 146 if (count == digits.length) { 147 char[] data = new char[count + 100]; 148 System.arraycopy(digits, 0, data, 0, count); 149 digits = data; 150 } 151 digits[count++] = digit; 152 } 153 154 /** 155 * Utility routine to get the value of the digit list 156 * If (count == 0) this throws a NumberFormatException, which 157 * mimics Long.parseLong(). 158 */ 159 public final double getDouble() { 160 if (count == 0) { 161 return 0.0; 162 } 163 164 StringBuffer temp = getStringBuffer(); 165 temp.append('.'); 166 temp.append(digits, 0, count); 167 temp.append('E'); 168 temp.append(decimalAt); 169 return Double.parseDouble(temp.toString()); 170 } 171 172 /** 173 * Utility routine to get the value of the digit list. 174 * If (count == 0) this returns 0, unlike Long.parseLong(). 175 */ 176 public final long getLong() { 177 // for now, simple implementation; later, do proper IEEE native stuff 178 179 if (count == 0) { 180 return 0; 181 } 182 183 // We have to check for this, because this is the one NEGATIVE value 184 // we represent. If we tried to just pass the digits off to parseLong, 185 // we'd get a parse failure. 186 if (isLongMIN_VALUE()) { 187 return Long.MIN_VALUE; 188 } 189 190 StringBuffer temp = getStringBuffer(); 191 temp.append(digits, 0, count); 192 for (int i = count; i < decimalAt; ++i) { 193 temp.append('0'); 194 } 195 return Long.parseLong(temp.toString()); 196 } 197 198 public final BigDecimal getBigDecimal() { 199 if (count == 0) { 200 if (decimalAt == 0) { 201 return BigDecimal.ZERO; 202 } else { 203 return new BigDecimal("0E" + decimalAt); 204 } 205 } 206 207 if (decimalAt == count) { 208 return new BigDecimal(digits, 0, count); 209 } else { 210 return new BigDecimal(digits, 0, count).scaleByPowerOfTen(decimalAt - count); 211 } 212 } 213 214 /** 215 * Return true if the number represented by this object can fit into 216 * a long. 217 * @param isPositive true if this number should be regarded as positive 218 * @param ignoreNegativeZero true if -0 should be regarded as identical to 219 * +0; otherwise they are considered distinct 220 * @return true if this number fits into a Java long 221 */ 222 boolean fitsIntoLong(boolean isPositive, boolean ignoreNegativeZero) { 223 // Figure out if the result will fit in a long. We have to 224 // first look for nonzero digits after the decimal point; 225 // then check the size. If the digit count is 18 or less, then 226 // the value can definitely be represented as a long. If it is 19 227 // then it may be too large. 228 229 // Trim trailing zeros. This does not change the represented value. 230 while (count > 0 && digits[count - 1] == '0') { 231 --count; 232 } 233 234 if (count == 0) { 235 // Positive zero fits into a long, but negative zero can only 236 // be represented as a double. - bug 4162852 237 return isPositive || ignoreNegativeZero; 238 } 239 240 if (decimalAt < count || decimalAt > MAX_COUNT) { 241 return false; 242 } 243 244 if (decimalAt < MAX_COUNT) return true; 245 246 // At this point we have decimalAt == count, and count == MAX_COUNT. 247 // The number will overflow if it is larger than 9223372036854775807 248 // or smaller than -9223372036854775808. 249 for (int i=0; i<count; ++i) { 250 char dig = digits[i], max = LONG_MIN_REP[i]; 251 if (dig > max) return false; 252 if (dig < max) return true; 253 } 254 255 // At this point the first count digits match. If decimalAt is less 256 // than count, then the remaining digits are zero, and we return true. 257 if (count < decimalAt) return true; 258 259 // Now we have a representation of Long.MIN_VALUE, without the leading 260 // negative sign. If this represents a positive value, then it does 261 // not fit; otherwise it fits. 262 return !isPositive; 263 } 264 265 /** 266 * Set the digit list to a representation of the given double value. 267 * This method supports fixed-point notation. 268 * @param isNegative Boolean value indicating whether the number is negative. 269 * @param source Value to be converted; must not be Inf, -Inf, Nan, 270 * or a value <= 0. 271 * @param maximumFractionDigits The most fractional digits which should 272 * be converted. 273 */ 274 final void set(boolean isNegative, double source, int maximumFractionDigits) { 275 set(isNegative, source, maximumFractionDigits, true); 276 } 277 278 /** 279 * Set the digit list to a representation of the given double value. 280 * This method supports both fixed-point and exponential notation. 281 * @param isNegative Boolean value indicating whether the number is negative. 282 * @param source Value to be converted; must not be Inf, -Inf, Nan, 283 * or a value <= 0. 284 * @param maximumDigits The most fractional or total digits which should 285 * be converted. 286 * @param fixedPoint If true, then maximumDigits is the maximum 287 * fractional digits to be converted. If false, total digits. 288 */ 289 final void set(boolean isNegative, double source, int maximumDigits, boolean fixedPoint) { 290 291 FloatingDecimal.BinaryToASCIIConverter fdConverter = FloatingDecimal.getBinaryToASCIIConverter(source); 292 boolean hasBeenRoundedUp = fdConverter.digitsRoundedUp(); 293 boolean allDecimalDigits = fdConverter.decimalDigitsExact(); 294 assert !fdConverter.isExceptional(); 295 String digitsString = fdConverter.toJavaFormatString(); 296 297 set(isNegative, digitsString, 298 hasBeenRoundedUp, allDecimalDigits, 299 maximumDigits, fixedPoint); 300 } 301 302 /** 303 * Generate a representation of the form DDDDD, DDDDD.DDDDD, or 304 * DDDDDE+/-DDDDD. 305 * @param roundedUp Boolean value indicating if the s digits were rounded-up. 306 * @param allDecimalDigits Boolean value indicating if the digits in s are 307 * an exact decimal representation of the double that was passed. 308 */ 309 private void set(boolean isNegative, String s, 310 boolean roundedUp, boolean allDecimalDigits, 311 int maximumDigits, boolean fixedPoint) { 312 this.isNegative = isNegative; 313 int len = s.length(); 314 char[] source = getDataChars(len); 315 s.getChars(0, len, source, 0); 316 317 decimalAt = -1; 318 count = 0; 319 int exponent = 0; 320 // Number of zeros between decimal point and first non-zero digit after 321 // decimal point, for numbers < 1. 322 int leadingZerosAfterDecimal = 0; 323 boolean nonZeroDigitSeen = false; 324 325 for (int i = 0; i < len; ) { 326 char c = source[i++]; 327 if (c == '.') { 328 decimalAt = count; 329 } else if (c == 'e' || c == 'E') { 330 exponent = parseInt(source, i, len); 331 break; 332 } else { 333 if (!nonZeroDigitSeen) { 334 nonZeroDigitSeen = (c != '0'); 335 if (!nonZeroDigitSeen && decimalAt != -1) 336 ++leadingZerosAfterDecimal; 337 } 338 if (nonZeroDigitSeen) { 339 digits[count++] = c; 340 } 341 } 342 } 343 if (decimalAt == -1) { 344 decimalAt = count; 345 } 346 if (nonZeroDigitSeen) { 347 decimalAt += exponent - leadingZerosAfterDecimal; 348 } 349 350 if (fixedPoint) { 351 // The negative of the exponent represents the number of leading 352 // zeros between the decimal and the first non-zero digit, for 353 // a value < 0.1 (e.g., for 0.00123, -decimalAt == 2). If this 354 // is more than the maximum fraction digits, then we have an underflow 355 // for the printed representation. 356 if (-decimalAt > maximumDigits) { 357 // Handle an underflow to zero when we round something like 358 // 0.0009 to 2 fractional digits. 359 count = 0; 360 return; 361 } else if (-decimalAt == maximumDigits) { 362 // If we round 0.0009 to 3 fractional digits, then we have to 363 // create a new one digit in the least significant location. 364 if (shouldRoundUp(0, roundedUp, allDecimalDigits)) { 365 count = 1; 366 ++decimalAt; 367 digits[0] = '1'; 368 } else { 369 count = 0; 370 } 371 return; 372 } 373 // else fall through 374 } 375 376 // Eliminate trailing zeros. 377 while (count > 1 && digits[count - 1] == '0') { 378 --count; 379 } 380 381 // Eliminate digits beyond maximum digits to be displayed. 382 // Round up if appropriate. 383 round(fixedPoint ? (maximumDigits + decimalAt) : maximumDigits, 384 roundedUp, allDecimalDigits); 385 } 386 387 /** 388 * Round the representation to the given number of digits. 389 * @param maximumDigits The maximum number of digits to be shown. 390 * @param alreadyRounded Boolean indicating if rounding up already happened. 391 * @param allDecimalDigits Boolean indicating if the digits provide an exact 392 * representation of the value. 393 * 394 * Upon return, count will be less than or equal to maximumDigits. 395 */ 396 private final void round(int maximumDigits, 397 boolean alreadyRounded, 398 boolean allDecimalDigits) { 399 // Eliminate digits beyond maximum digits to be displayed. 400 // Round up if appropriate. 401 if (maximumDigits >= 0 && maximumDigits < count) { 402 if (shouldRoundUp(maximumDigits, alreadyRounded, allDecimalDigits)) { 403 // Rounding up involved incrementing digits from LSD to MSD. 404 // In most cases this is simple, but in a worst case situation 405 // (9999..99) we have to adjust the decimalAt value. 406 for (;;) { 407 --maximumDigits; 408 if (maximumDigits < 0) { 409 // We have all 9's, so we increment to a single digit 410 // of one and adjust the exponent. 411 digits[0] = '1'; 412 ++decimalAt; 413 maximumDigits = 0; // Adjust the count 414 break; 415 } 416 417 ++digits[maximumDigits]; 418 if (digits[maximumDigits] <= '9') break; 419 // digits[maximumDigits] = '0'; // Unnecessary since we'll truncate this 420 } 421 ++maximumDigits; // Increment for use as count 422 } 423 count = maximumDigits; 424 425 // Eliminate trailing zeros. 426 while (count > 1 && digits[count-1] == '0') { 427 --count; 428 } 429 } 430 } 431 432 433 /** 434 * Return true if truncating the representation to the given number 435 * of digits will result in an increment to the last digit. This 436 * method implements the rounding modes defined in the 437 * java.math.RoundingMode class. 438 * [bnf] 439 * @param maximumDigits the number of digits to keep, from 0 to 440 * <code>count-1</code>. If 0, then all digits are rounded away, and 441 * this method returns true if a one should be generated (e.g., formatting 442 * 0.09 with "#.#"). 443 * @exception ArithmeticException if rounding is needed with rounding 444 * mode being set to RoundingMode.UNNECESSARY 445 * @return true if digit <code>maximumDigits-1</code> should be 446 * incremented 447 */ 448 private boolean shouldRoundUp(int maximumDigits, 449 boolean alreadyRounded, 450 boolean allDecimalDigits) { 451 if (maximumDigits < count) { 452 /* 453 * To avoid erroneous double-rounding or truncation when converting 454 * a binary double value to text, information about the exactness 455 * of the conversion result in FloatingDecimal, as well as any 456 * rounding done, is needed in this class. 457 * 458 * - For the HALF_DOWN, HALF_EVEN, HALF_UP rounding rules below: 459 * In the case of formating float or double, We must take into 460 * account what FloatingDecimal has done in the binary to decimal 461 * conversion. 462 * 463 * Considering the tie cases, FloatingDecimal may round-up the 464 * value (returning decimal digits equal to tie when it is below), 465 * or "truncate" the value to the tie while value is above it, 466 * or provide the exact decimal digits when the binary value can be 467 * converted exactly to its decimal representation given formating 468 * rules of FloatingDecimal ( we have thus an exact decimal 469 * representation of the binary value). 470 * 471 * - If the double binary value was converted exactly as a decimal 472 * value, then DigitList code must apply the expected rounding 473 * rule. 474 * 475 * - If FloatingDecimal already rounded up the decimal value, 476 * DigitList should neither round up the value again in any of 477 * the three rounding modes above. 478 * 479 * - If FloatingDecimal has truncated the decimal value to 480 * an ending '5' digit, DigitList should round up the value in 481 * all of the three rounding modes above. 482 * 483 * 484 * This has to be considered only if digit at maximumDigits index 485 * is exactly the last one in the set of digits, otherwise there are 486 * remaining digits after that position and we don't have to consider 487 * what FloatingDecimal did. 488 * 489 * - Other rounding modes are not impacted by these tie cases. 490 * 491 * - For other numbers that are always converted to exact digits 492 * (like BigInteger, Long, ...), the passed alreadyRounded boolean 493 * have to be set to false, and allDecimalDigits has to be set to 494 * true in the upper DigitList call stack, providing the right state 495 * for those situations.. 496 */ 497 498 switch(roundingMode) { 499 case UP: 500 for (int i=maximumDigits; i<count; ++i) { 501 if (digits[i] != '0') { 502 return true; 503 } 504 } 505 break; 506 case DOWN: 507 break; 508 case CEILING: 509 for (int i=maximumDigits; i<count; ++i) { 510 if (digits[i] != '0') { 511 return !isNegative; 512 } 513 } 514 break; 515 case FLOOR: 516 for (int i=maximumDigits; i<count; ++i) { 517 if (digits[i] != '0') { 518 return isNegative; 519 } 520 } 521 break; 522 case HALF_UP: 523 if (digits[maximumDigits] >= '5') { 524 // We should not round up if the rounding digits position is 525 // exactly the last index and if digits were already rounded. 526 if ((maximumDigits == (count - 1)) && 527 (alreadyRounded)) 528 return false; 529 530 // Value was exactly at or was above tie. We must round up. 531 return true; 532 } 533 break; 534 case HALF_DOWN: 535 if (digits[maximumDigits] > '5') { 536 return true; 537 } else if (digits[maximumDigits] == '5' ) { 538 if (maximumDigits == (count - 1)) { 539 // The rounding position is exactly the last index. 540 if (allDecimalDigits || alreadyRounded) 541 /* FloatingDecimal rounded up (value was below tie), 542 * or provided the exact list of digits (value was 543 * an exact tie). We should not round up, following 544 * the HALF_DOWN rounding rule. 545 */ 546 return false; 547 else 548 // Value was above the tie, we must round up. 549 return true; 550 } 551 552 // We must round up if it gives a non null digit after '5'. 553 for (int i=maximumDigits+1; i<count; ++i) { 554 if (digits[i] != '0') { 555 return true; 556 } 557 } 558 } 559 break; 560 case HALF_EVEN: 561 // Implement IEEE half-even rounding 562 if (digits[maximumDigits] > '5') { 563 return true; 564 } else if (digits[maximumDigits] == '5' ) { 565 if (maximumDigits == (count - 1)) { 566 // the rounding position is exactly the last index : 567 if (alreadyRounded) 568 // If FloatingDecimal rounded up (value was below tie), 569 // then we should not round up again. 570 return false; 571 572 if (!allDecimalDigits) 573 // Otherwise if the digits don't represent exact value, 574 // value was above tie and FloatingDecimal truncated 575 // digits to tie. We must round up. 576 return true; 577 else { 578 // This is an exact tie value, and FloatingDecimal 579 // provided all of the exact digits. We thus apply 580 // HALF_EVEN rounding rule. 581 return ((maximumDigits > 0) && 582 (digits[maximumDigits-1] % 2 != 0)); 583 } 584 } else { 585 // Rounds up if it gives a non null digit after '5' 586 for (int i=maximumDigits+1; i<count; ++i) { 587 if (digits[i] != '0') 588 return true; 589 } 590 } 591 } 592 break; 593 case UNNECESSARY: 594 for (int i=maximumDigits; i<count; ++i) { 595 if (digits[i] != '0') { 596 throw new ArithmeticException( 597 "Rounding needed with the rounding mode being set to RoundingMode.UNNECESSARY"); 598 } 599 } 600 break; 601 default: 602 assert false; 603 } 604 } 605 return false; 606 } 607 608 /** 609 * Utility routine to set the value of the digit list from a long 610 */ 611 final void set(boolean isNegative, long source) { 612 set(isNegative, source, 0); 613 } 614 615 /** 616 * Set the digit list to a representation of the given long value. 617 * @param isNegative Boolean value indicating whether the number is negative. 618 * @param source Value to be converted; must be >= 0 or == 619 * Long.MIN_VALUE. 620 * @param maximumDigits The most digits which should be converted. 621 * If maximumDigits is lower than the number of significant digits 622 * in source, the representation will be rounded. Ignored if <= 0. 623 */ 624 final void set(boolean isNegative, long source, int maximumDigits) { 625 this.isNegative = isNegative; 626 627 // This method does not expect a negative number. However, 628 // "source" can be a Long.MIN_VALUE (-9223372036854775808), 629 // if the number being formatted is a Long.MIN_VALUE. In that 630 // case, it will be formatted as -Long.MIN_VALUE, a number 631 // which is outside the legal range of a long, but which can 632 // be represented by DigitList. 633 if (source <= 0) { 634 if (source == Long.MIN_VALUE) { 635 decimalAt = count = MAX_COUNT; 636 System.arraycopy(LONG_MIN_REP, 0, digits, 0, count); 637 } else { 638 decimalAt = count = 0; // Values <= 0 format as zero 639 } 640 } else { 641 // Rewritten to improve performance. I used to call 642 // Long.toString(), which was about 4x slower than this code. 643 int left = MAX_COUNT; 644 int right; 645 while (source > 0) { 646 digits[--left] = (char)('0' + (source % 10)); 647 source /= 10; 648 } 649 decimalAt = MAX_COUNT - left; 650 // Don't copy trailing zeros. We are guaranteed that there is at 651 // least one non-zero digit, so we don't have to check lower bounds. 652 for (right = MAX_COUNT - 1; digits[right] == '0'; --right) 653 ; 654 count = right - left + 1; 655 System.arraycopy(digits, left, digits, 0, count); 656 } 657 if (maximumDigits > 0) round(maximumDigits, false, true); 658 } 659 660 /** 661 * Set the digit list to a representation of the given BigDecimal value. 662 * This method supports both fixed-point and exponential notation. 663 * @param isNegative Boolean value indicating whether the number is negative. 664 * @param source Value to be converted; must not be a value <= 0. 665 * @param maximumDigits The most fractional or total digits which should 666 * be converted. 667 * @param fixedPoint If true, then maximumDigits is the maximum 668 * fractional digits to be converted. If false, total digits. 669 */ 670 final void set(boolean isNegative, BigDecimal source, int maximumDigits, boolean fixedPoint) { 671 String s = source.toString(); 672 extendDigits(s.length()); 673 674 set(isNegative, s, 675 false, true, 676 maximumDigits, fixedPoint); 677 } 678 679 /** 680 * Set the digit list to a representation of the given BigInteger value. 681 * @param isNegative Boolean value indicating whether the number is negative. 682 * @param source Value to be converted; must be >= 0. 683 * @param maximumDigits The most digits which should be converted. 684 * If maximumDigits is lower than the number of significant digits 685 * in source, the representation will be rounded. Ignored if <= 0. 686 */ 687 final void set(boolean isNegative, BigInteger source, int maximumDigits) { 688 this.isNegative = isNegative; 689 String s = source.toString(); 690 int len = s.length(); 691 extendDigits(len); 692 s.getChars(0, len, digits, 0); 693 694 decimalAt = len; 695 int right; 696 for (right = len - 1; right >= 0 && digits[right] == '0'; --right) 697 ; 698 count = right + 1; 699 700 if (maximumDigits > 0) { 701 round(maximumDigits, false, true); 702 } 703 } 704 705 /** 706 * equality test between two digit lists. 707 */ 708 public boolean equals(Object obj) { 709 if (this == obj) // quick check 710 return true; 711 if (!(obj instanceof DigitList)) // (1) same object? 712 return false; 713 DigitList other = (DigitList) obj; 714 if (count != other.count || 715 decimalAt != other.decimalAt) 716 return false; 717 for (int i = 0; i < count; i++) 718 if (digits[i] != other.digits[i]) 719 return false; 720 return true; 721 } 722 723 /** 724 * Generates the hash code for the digit list. 725 */ 726 public int hashCode() { 727 int hashcode = decimalAt; 728 729 for (int i = 0; i < count; i++) { 730 hashcode = hashcode * 37 + digits[i]; 731 } 732 733 return hashcode; 734 } 735 736 /** 737 * Creates a copy of this object. 738 * @return a clone of this instance. 739 */ 740 public Object clone() { 741 try { 742 DigitList other = (DigitList) super.clone(); 743 char[] newDigits = new char[digits.length]; 744 System.arraycopy(digits, 0, newDigits, 0, digits.length); 745 other.digits = newDigits; 746 other.tempBuffer = null; 747 return other; 748 } catch (CloneNotSupportedException e) { 749 throw new InternalError(e); 750 } 751 } 752 753 /** 754 * Returns true if this DigitList represents Long.MIN_VALUE; 755 * false, otherwise. This is required so that getLong() works. 756 */ 757 private boolean isLongMIN_VALUE() { 758 if (decimalAt != count || count != MAX_COUNT) { 759 return false; 760 } 761 762 for (int i = 0; i < count; ++i) { 763 if (digits[i] != LONG_MIN_REP[i]) return false; 764 } 765 766 return true; 767 } 768 769 private static final int parseInt(char[] str, int offset, int strLen) { 770 char c; 771 boolean positive = true; 772 if ((c = str[offset]) == '-') { 773 positive = false; 774 offset++; 775 } else if (c == '+') { 776 offset++; 777 } 778 779 int value = 0; 780 while (offset < strLen) { 781 c = str[offset++]; 782 if (c >= '0' && c <= '9') { 783 value = value * 10 + (c - '0'); 784 } else { 785 break; 786 } 787 } 788 return positive ? value : -value; 789 } 790 791 // The digit part of -9223372036854775808L 792 private static final char[] LONG_MIN_REP = "9223372036854775808".toCharArray(); 793 794 public String toString() { 795 if (isZero()) { 796 return "0"; 797 } 798 StringBuffer buf = getStringBuffer(); 799 buf.append("0."); 800 buf.append(digits, 0, count); 801 buf.append("x10^"); 802 buf.append(decimalAt); 803 return buf.toString(); 804 } 805 806 private StringBuffer tempBuffer; 807 808 private StringBuffer getStringBuffer() { 809 if (tempBuffer == null) { 810 tempBuffer = new StringBuffer(MAX_COUNT); 811 } else { 812 tempBuffer.setLength(0); 813 } 814 return tempBuffer; 815 } 816 817 private void extendDigits(int len) { 818 if (len > digits.length) { 819 digits = new char[len]; 820 } 821 } 822 823 private final char[] getDataChars(int length) { 824 if (data == null || data.length < length) { 825 data = new char[length]; 826 } 827 return data; 828 } 829 }