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