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