emul/compact/src/main/java/java/math/MathContext.java
branchjdk7-b147
changeset 1258 724f3e1ea53e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/compact/src/main/java/java/math/MathContext.java	Sat Sep 07 13:51:24 2013 +0200
     1.3 @@ -0,0 +1,326 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * Portions Copyright IBM Corporation, 1997, 2001. All Rights Reserved.
    1.31 + */
    1.32 +
    1.33 +package java.math;
    1.34 +import java.io.*;
    1.35 +
    1.36 +/**
    1.37 + * Immutable objects which encapsulate the context settings which
    1.38 + * describe certain rules for numerical operators, such as those
    1.39 + * implemented by the {@link BigDecimal} class.
    1.40 + *
    1.41 + * <p>The base-independent settings are:
    1.42 + * <ol>
    1.43 + * <li>{@code precision}:
    1.44 + * the number of digits to be used for an operation; results are
    1.45 + * rounded to this precision
    1.46 + *
    1.47 + * <li>{@code roundingMode}:
    1.48 + * a {@link RoundingMode} object which specifies the algorithm to be
    1.49 + * used for rounding.
    1.50 + * </ol>
    1.51 + *
    1.52 + * @see     BigDecimal
    1.53 + * @see     RoundingMode
    1.54 + * @author  Mike Cowlishaw
    1.55 + * @author  Joseph D. Darcy
    1.56 + * @since 1.5
    1.57 + */
    1.58 +
    1.59 +public final class MathContext implements Serializable {
    1.60 +
    1.61 +    /* ----- Constants ----- */
    1.62 +
    1.63 +    // defaults for constructors
    1.64 +    private static final int DEFAULT_DIGITS = 9;
    1.65 +    private static final RoundingMode DEFAULT_ROUNDINGMODE = RoundingMode.HALF_UP;
    1.66 +    // Smallest values for digits (Maximum is Integer.MAX_VALUE)
    1.67 +    private static final int MIN_DIGITS = 0;
    1.68 +
    1.69 +    // Serialization version
    1.70 +    private static final long serialVersionUID = 5579720004786848255L;
    1.71 +
    1.72 +    /* ----- Public Properties ----- */
    1.73 +    /**
    1.74 +     *  A {@code MathContext} object whose settings have the values
    1.75 +     *  required for unlimited precision arithmetic.
    1.76 +     *  The values of the settings are:
    1.77 +     *  <code>
    1.78 +     *  precision=0 roundingMode=HALF_UP
    1.79 +     *  </code>
    1.80 +     */
    1.81 +    public static final MathContext UNLIMITED =
    1.82 +        new MathContext(0, RoundingMode.HALF_UP);
    1.83 +
    1.84 +    /**
    1.85 +     *  A {@code MathContext} object with a precision setting
    1.86 +     *  matching the IEEE 754R Decimal32 format, 7 digits, and a
    1.87 +     *  rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
    1.88 +     *  IEEE 754R default.
    1.89 +     */
    1.90 +    public static final MathContext DECIMAL32 =
    1.91 +        new MathContext(7, RoundingMode.HALF_EVEN);
    1.92 +
    1.93 +    /**
    1.94 +     *  A {@code MathContext} object with a precision setting
    1.95 +     *  matching the IEEE 754R Decimal64 format, 16 digits, and a
    1.96 +     *  rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
    1.97 +     *  IEEE 754R default.
    1.98 +     */
    1.99 +    public static final MathContext DECIMAL64 =
   1.100 +        new MathContext(16, RoundingMode.HALF_EVEN);
   1.101 +
   1.102 +    /**
   1.103 +     *  A {@code MathContext} object with a precision setting
   1.104 +     *  matching the IEEE 754R Decimal128 format, 34 digits, and a
   1.105 +     *  rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
   1.106 +     *  IEEE 754R default.
   1.107 +     */
   1.108 +    public static final MathContext DECIMAL128 =
   1.109 +        new MathContext(34, RoundingMode.HALF_EVEN);
   1.110 +
   1.111 +    /* ----- Shared Properties ----- */
   1.112 +    /**
   1.113 +     * The number of digits to be used for an operation.  A value of 0
   1.114 +     * indicates that unlimited precision (as many digits as are
   1.115 +     * required) will be used.  Note that leading zeros (in the
   1.116 +     * coefficient of a number) are never significant.
   1.117 +     *
   1.118 +     * <p>{@code precision} will always be non-negative.
   1.119 +     *
   1.120 +     * @serial
   1.121 +     */
   1.122 +    final int precision;
   1.123 +
   1.124 +    /**
   1.125 +     * The rounding algorithm to be used for an operation.
   1.126 +     *
   1.127 +     * @see RoundingMode
   1.128 +     * @serial
   1.129 +     */
   1.130 +    final RoundingMode roundingMode;
   1.131 +
   1.132 +    /* ----- Constructors ----- */
   1.133 +
   1.134 +    /**
   1.135 +     * Constructs a new {@code MathContext} with the specified
   1.136 +     * precision and the {@link RoundingMode#HALF_UP HALF_UP} rounding
   1.137 +     * mode.
   1.138 +     *
   1.139 +     * @param setPrecision The non-negative {@code int} precision setting.
   1.140 +     * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
   1.141 +     *         than zero.
   1.142 +     */
   1.143 +    public MathContext(int setPrecision) {
   1.144 +        this(setPrecision, DEFAULT_ROUNDINGMODE);
   1.145 +        return;
   1.146 +    }
   1.147 +
   1.148 +    /**
   1.149 +     * Constructs a new {@code MathContext} with a specified
   1.150 +     * precision and rounding mode.
   1.151 +     *
   1.152 +     * @param setPrecision The non-negative {@code int} precision setting.
   1.153 +     * @param setRoundingMode The rounding mode to use.
   1.154 +     * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
   1.155 +     *         than zero.
   1.156 +     * @throws NullPointerException if the rounding mode argument is {@code null}
   1.157 +     */
   1.158 +    public MathContext(int setPrecision,
   1.159 +                       RoundingMode setRoundingMode) {
   1.160 +        if (setPrecision < MIN_DIGITS)
   1.161 +            throw new IllegalArgumentException("Digits < 0");
   1.162 +        if (setRoundingMode == null)
   1.163 +            throw new NullPointerException("null RoundingMode");
   1.164 +
   1.165 +        precision = setPrecision;
   1.166 +        roundingMode = setRoundingMode;
   1.167 +        return;
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Constructs a new {@code MathContext} from a string.
   1.172 +     *
   1.173 +     * The string must be in the same format as that produced by the
   1.174 +     * {@link #toString} method.
   1.175 +     *
   1.176 +     * <p>An {@code IllegalArgumentException} is thrown if the precision
   1.177 +     * section of the string is out of range ({@code < 0}) or the string is
   1.178 +     * not in the format created by the {@link #toString} method.
   1.179 +     *
   1.180 +     * @param val The string to be parsed
   1.181 +     * @throws IllegalArgumentException if the precision section is out of range
   1.182 +     * or of incorrect format
   1.183 +     * @throws NullPointerException if the argument is {@code null}
   1.184 +     */
   1.185 +    public MathContext(String val) {
   1.186 +        boolean bad = false;
   1.187 +        int setPrecision;
   1.188 +        if (val == null)
   1.189 +            throw new NullPointerException("null String");
   1.190 +        try { // any error here is a string format problem
   1.191 +            if (!val.startsWith("precision=")) throw new RuntimeException();
   1.192 +            int fence = val.indexOf(' ');    // could be -1
   1.193 +            int off = 10;                     // where value starts
   1.194 +            setPrecision = Integer.parseInt(val.substring(10, fence));
   1.195 +
   1.196 +            if (!val.startsWith("roundingMode=", fence+1))
   1.197 +                throw new RuntimeException();
   1.198 +            off = fence + 1 + 13;
   1.199 +            String str = val.substring(off, val.length());
   1.200 +            roundingMode = RoundingMode.valueOf(str);
   1.201 +        } catch (RuntimeException re) {
   1.202 +            throw new IllegalArgumentException("bad string format");
   1.203 +        }
   1.204 +
   1.205 +        if (setPrecision < MIN_DIGITS)
   1.206 +            throw new IllegalArgumentException("Digits < 0");
   1.207 +        // the other parameters cannot be invalid if we got here
   1.208 +        precision = setPrecision;
   1.209 +    }
   1.210 +
   1.211 +    /**
   1.212 +     * Returns the {@code precision} setting.
   1.213 +     * This value is always non-negative.
   1.214 +     *
   1.215 +     * @return an {@code int} which is the value of the {@code precision}
   1.216 +     *         setting
   1.217 +     */
   1.218 +    public int getPrecision() {
   1.219 +        return precision;
   1.220 +    }
   1.221 +
   1.222 +    /**
   1.223 +     * Returns the roundingMode setting.
   1.224 +     * This will be one of
   1.225 +     * {@link  RoundingMode#CEILING},
   1.226 +     * {@link  RoundingMode#DOWN},
   1.227 +     * {@link  RoundingMode#FLOOR},
   1.228 +     * {@link  RoundingMode#HALF_DOWN},
   1.229 +     * {@link  RoundingMode#HALF_EVEN},
   1.230 +     * {@link  RoundingMode#HALF_UP},
   1.231 +     * {@link  RoundingMode#UNNECESSARY}, or
   1.232 +     * {@link  RoundingMode#UP}.
   1.233 +     *
   1.234 +     * @return a {@code RoundingMode} object which is the value of the
   1.235 +     *         {@code roundingMode} setting
   1.236 +     */
   1.237 +
   1.238 +    public RoundingMode getRoundingMode() {
   1.239 +        return roundingMode;
   1.240 +    }
   1.241 +
   1.242 +    /**
   1.243 +     * Compares this {@code MathContext} with the specified
   1.244 +     * {@code Object} for equality.
   1.245 +     *
   1.246 +     * @param  x {@code Object} to which this {@code MathContext} is to
   1.247 +     *         be compared.
   1.248 +     * @return {@code true} if and only if the specified {@code Object} is
   1.249 +     *         a {@code MathContext} object which has exactly the same
   1.250 +     *         settings as this object
   1.251 +     */
   1.252 +    public boolean equals(Object x){
   1.253 +        MathContext mc;
   1.254 +        if (!(x instanceof MathContext))
   1.255 +            return false;
   1.256 +        mc = (MathContext) x;
   1.257 +        return mc.precision == this.precision
   1.258 +            && mc.roundingMode == this.roundingMode; // no need for .equals()
   1.259 +    }
   1.260 +
   1.261 +    /**
   1.262 +     * Returns the hash code for this {@code MathContext}.
   1.263 +     *
   1.264 +     * @return hash code for this {@code MathContext}
   1.265 +     */
   1.266 +    public int hashCode() {
   1.267 +        return this.precision + roundingMode.hashCode() * 59;
   1.268 +    }
   1.269 +
   1.270 +    /**
   1.271 +     * Returns the string representation of this {@code MathContext}.
   1.272 +     * The {@code String} returned represents the settings of the
   1.273 +     * {@code MathContext} object as two space-delimited words
   1.274 +     * (separated by a single space character, <tt>'&#92;u0020'</tt>,
   1.275 +     * and with no leading or trailing white space), as follows:
   1.276 +     * <ol>
   1.277 +     * <li>
   1.278 +     * The string {@code "precision="}, immediately followed
   1.279 +     * by the value of the precision setting as a numeric string as if
   1.280 +     * generated by the {@link Integer#toString(int) Integer.toString}
   1.281 +     * method.
   1.282 +     *
   1.283 +     * <li>
   1.284 +     * The string {@code "roundingMode="}, immediately
   1.285 +     * followed by the value of the {@code roundingMode} setting as a
   1.286 +     * word.  This word will be the same as the name of the
   1.287 +     * corresponding public constant in the {@link RoundingMode}
   1.288 +     * enum.
   1.289 +     * </ol>
   1.290 +     * <p>
   1.291 +     * For example:
   1.292 +     * <pre>
   1.293 +     * precision=9 roundingMode=HALF_UP
   1.294 +     * </pre>
   1.295 +     *
   1.296 +     * Additional words may be appended to the result of
   1.297 +     * {@code toString} in the future if more properties are added to
   1.298 +     * this class.
   1.299 +     *
   1.300 +     * @return a {@code String} representing the context settings
   1.301 +     */
   1.302 +    public java.lang.String toString() {
   1.303 +        return "precision=" +           precision + " " +
   1.304 +               "roundingMode=" +        roundingMode.toString();
   1.305 +    }
   1.306 +
   1.307 +    // Private methods
   1.308 +
   1.309 +    /**
   1.310 +     * Reconstitute the {@code MathContext} instance from a stream (that is,
   1.311 +     * deserialize it).
   1.312 +     *
   1.313 +     * @param s the stream being read.
   1.314 +     */
   1.315 +    private void readObject(java.io.ObjectInputStream s)
   1.316 +        throws java.io.IOException, ClassNotFoundException {
   1.317 +        s.defaultReadObject();     // read in all fields
   1.318 +        // validate possibly bad fields
   1.319 +        if (precision < MIN_DIGITS) {
   1.320 +            String message = "MathContext: invalid digits in stream";
   1.321 +            throw new java.io.StreamCorruptedException(message);
   1.322 +        }
   1.323 +        if (roundingMode == null) {
   1.324 +            String message = "MathContext: null roundingMode in stream";
   1.325 +            throw new java.io.StreamCorruptedException(message);
   1.326 +        }
   1.327 +    }
   1.328 +
   1.329 +}