src/java.base/share/classes/java/math/MathContext.java
author darcy
Thu, 29 Aug 2019 16:31:34 -0700
changeset 57956 e0b8b019d2f5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8229997: Apply java.io.Serial annotations in java.base Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
    23
 * questions.
2
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
 * Portions Copyright IBM Corporation, 1997, 2001. All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package java.math;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Immutable objects which encapsulate the context settings which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * describe certain rules for numerical operators, such as those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * implemented by the {@link BigDecimal} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>The base-independent settings are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <li>{@code precision}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the number of digits to be used for an operation; results are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * rounded to this precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <li>{@code roundingMode}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * a {@link RoundingMode} object which specifies the algorithm to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * used for rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see     BigDecimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see     RoundingMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author  Mike Cowlishaw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author  Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public final class MathContext implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /* ----- Constants ----- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // defaults for constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final int DEFAULT_DIGITS = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final RoundingMode DEFAULT_ROUNDINGMODE = RoundingMode.HALF_UP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Smallest values for digits (Maximum is Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int MIN_DIGITS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // Serialization version
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
    67
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final long serialVersionUID = 5579720004786848255L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* ----- Public Properties ----- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *  A {@code MathContext} object whose settings have the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *  required for unlimited precision arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *  The values of the settings are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *  <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *  precision=0 roundingMode=HALF_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *  </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final MathContext UNLIMITED =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        new MathContext(0, RoundingMode.HALF_UP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *  A {@code MathContext} object with a precision setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *  matching the IEEE 754R Decimal32 format, 7 digits, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *  rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *  IEEE 754R default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static final MathContext DECIMAL32 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        new MathContext(7, RoundingMode.HALF_EVEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *  A {@code MathContext} object with a precision setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *  matching the IEEE 754R Decimal64 format, 16 digits, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *  rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *  IEEE 754R default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static final MathContext DECIMAL64 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        new MathContext(16, RoundingMode.HALF_EVEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *  A {@code MathContext} object with a precision setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *  matching the IEEE 754R Decimal128 format, 34 digits, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *  rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *  IEEE 754R default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final MathContext DECIMAL128 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        new MathContext(34, RoundingMode.HALF_EVEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /* ----- Shared Properties ----- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * The number of digits to be used for an operation.  A value of 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * indicates that unlimited precision (as many digits as are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * required) will be used.  Note that leading zeros (in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * coefficient of a number) are never significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>{@code precision} will always be non-negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    final int precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The rounding algorithm to be used for an operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see RoundingMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    final RoundingMode roundingMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /* ----- Constructors ----- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Constructs a new {@code MathContext} with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * precision and the {@link RoundingMode#HALF_UP HALF_UP} rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param setPrecision The non-negative {@code int} precision setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *         than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public MathContext(int setPrecision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this(setPrecision, DEFAULT_ROUNDINGMODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Constructs a new {@code MathContext} with a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * precision and rounding mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param setPrecision The non-negative {@code int} precision setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param setRoundingMode The rounding mode to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *         than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws NullPointerException if the rounding mode argument is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public MathContext(int setPrecision,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                       RoundingMode setRoundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (setPrecision < MIN_DIGITS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new IllegalArgumentException("Digits < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (setRoundingMode == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new NullPointerException("null RoundingMode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        precision = setPrecision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        roundingMode = setRoundingMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Constructs a new {@code MathContext} from a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * The string must be in the same format as that produced by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * {@link #toString} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <p>An {@code IllegalArgumentException} is thrown if the precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * section of the string is out of range ({@code < 0}) or the string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * not in the format created by the {@link #toString} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param val The string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @throws IllegalArgumentException if the precision section is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * or of incorrect format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws NullPointerException if the argument is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public MathContext(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        boolean bad = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int setPrecision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (val == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new NullPointerException("null String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        try { // any error here is a string format problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (!val.startsWith("precision=")) throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            int fence = val.indexOf(' ');    // could be -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            int off = 10;                     // where value starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            setPrecision = Integer.parseInt(val.substring(10, fence));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (!val.startsWith("roundingMode=", fence+1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            off = fence + 1 + 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            String str = val.substring(off, val.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            roundingMode = RoundingMode.valueOf(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } catch (RuntimeException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new IllegalArgumentException("bad string format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (setPrecision < MIN_DIGITS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new IllegalArgumentException("Digits < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // the other parameters cannot be invalid if we got here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        precision = setPrecision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Returns the {@code precision} setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * This value is always non-negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return an {@code int} which is the value of the {@code precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *         setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public int getPrecision() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Returns the roundingMode setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * This will be one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * {@link  RoundingMode#CEILING},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * {@link  RoundingMode#DOWN},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * {@link  RoundingMode#FLOOR},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * {@link  RoundingMode#HALF_DOWN},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * {@link  RoundingMode#HALF_EVEN},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * {@link  RoundingMode#HALF_UP},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * {@link  RoundingMode#UNNECESSARY}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * {@link  RoundingMode#UP}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return a {@code RoundingMode} object which is the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *         {@code roundingMode} setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public RoundingMode getRoundingMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return roundingMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Compares this {@code MathContext} with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * {@code Object} for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param  x {@code Object} to which this {@code MathContext} is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *         be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return {@code true} if and only if the specified {@code Object} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *         a {@code MathContext} object which has exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *         settings as this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public boolean equals(Object x){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        MathContext mc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (!(x instanceof MathContext))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        mc = (MathContext) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return mc.precision == this.precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            && mc.roundingMode == this.roundingMode; // no need for .equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Returns the hash code for this {@code MathContext}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return hash code for this {@code MathContext}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return this.precision + roundingMode.hashCode() * 59;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Returns the string representation of this {@code MathContext}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * The {@code String} returned represents the settings of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * {@code MathContext} object as two space-delimited words
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 25859
diff changeset
   272
     * (separated by a single space character, <code>'&#92;u0020'</code>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * and with no leading or trailing white space), as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * The string {@code "precision="}, immediately followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * by the value of the precision setting as a numeric string as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * generated by the {@link Integer#toString(int) Integer.toString}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * The string {@code "roundingMode="}, immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * followed by the value of the {@code roundingMode} setting as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * word.  This word will be the same as the name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * corresponding public constant in the {@link RoundingMode}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * enum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * precision=9 roundingMode=HALF_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Additional words may be appended to the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * {@code toString} in the future if more properties are added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return a {@code String} representing the context settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public java.lang.String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return "precision=" +           precision + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
               "roundingMode=" +        roundingMode.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    // Private methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Reconstitute the {@code MathContext} instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param s the stream being read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
   313
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        s.defaultReadObject();     // read in all fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // validate possibly bad fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (precision < MIN_DIGITS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            String message = "MathContext: invalid digits in stream";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new java.io.StreamCorruptedException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (roundingMode == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            String message = "MathContext: null roundingMode in stream";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new java.io.StreamCorruptedException(message);
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
}