001 /* MathContext.java --
002 Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.math;
040
041 import java.io.Serializable;
042
043 /**
044 * Immutable objects describing settings such as rounding mode and digit
045 * precision for numerical operations such as those in the BigDecimal class.
046 * @author Anthony Balkissoon abalkiss at redhat dot com
047 *
048 */
049 public final class MathContext implements Serializable
050 {
051 /** A MathContext for unlimited precision arithmetic * */
052 public static final MathContext UNLIMITED =
053 new MathContext(0, RoundingMode.HALF_UP);
054
055 /**
056 * A MathContext for the IEEE 754R Decimal32 format - 7 digit preicision and
057 * HALF_EVEN rounding.
058 */
059 public static final MathContext DECIMAL32 =
060 new MathContext(7, RoundingMode.HALF_EVEN);
061
062 /**
063 * A MathContext for the IEEE 754R Decimal64 format - 16 digit preicision and
064 * HALF_EVEN rounding.
065 */
066 public static final MathContext DECIMAL64 =
067 new MathContext(16, RoundingMode.HALF_EVEN);
068
069 /**
070 * A MathContext for the IEEE 754R Decimal128 format - 34 digit preicision and
071 * HALF_EVEN rounding.
072 */
073 public static final MathContext DECIMAL128 =
074 new MathContext(34, RoundingMode.HALF_EVEN);
075
076 /**
077 * This is the serialVersionUID reported here:
078 * java.sun.com/j2se/1.5.0/docs/api/serialized-form.html#java.math.MathContext
079 */
080 private static final long serialVersionUID = 5579720004786848255L;
081
082 private int precision;
083
084 private RoundingMode roundMode;
085
086 /**
087 * Constructs a new MathContext with the specified precision and with HALF_UP
088 * rounding.
089 * @param setPrecision the precision for the new MathContext
090 *
091 * @throws IllegalArgumentException if precision is < 0.
092 */
093 public MathContext(int setPrecision)
094 {
095 this(setPrecision, RoundingMode.HALF_UP);
096 }
097
098 /**
099 * Constructs a new MathContext with the specified precision and rounding
100 * mode.
101 * @param setPrecision the precision
102 * @param setRoundingMode the rounding mode
103 *
104 * @throws IllegalArgumentException if precision is < 0.
105 */
106 public MathContext(int setPrecision, RoundingMode setRoundingMode)
107 {
108 if (setPrecision < 0)
109 throw new IllegalArgumentException("Precision cannot be less than zero.");
110 precision = setPrecision;
111 roundMode = setRoundingMode;
112 }
113
114 /**
115 * Constructs a MathContext from a String that has the same form as one
116 * produced by the toString() method.
117 * @param val
118 *
119 * @throws IllegalArgumentException if the String is not in the correct
120 * format or if the precision specified is < 0.
121 */
122 public MathContext(String val)
123 {
124 try
125 {
126 int roundingModeIndex = val.indexOf("roundingMode", 10);
127 precision = Integer.parseInt(val.substring(10, roundingModeIndex - 1));
128 roundMode = RoundingMode.valueOf(val.substring(roundingModeIndex + 13));
129 }
130 catch (NumberFormatException nfe)
131 {
132 throw new IllegalArgumentException("String not in correct format");
133 }
134 catch (IllegalArgumentException iae)
135 {
136 throw new IllegalArgumentException("String not in correct format");
137 }
138 if (precision < 0)
139 throw new IllegalArgumentException("Precision cannot be less than 0.");
140 }
141
142 /**
143 * Returns true if x is a MathContext and has the same precision setting
144 * and rounding mode as this MathContext.
145 *
146 * @return true if the above conditions hold
147 */
148 public boolean equals(Object x)
149 {
150 if (!(x instanceof MathContext))
151 return false;
152 MathContext mc = (MathContext)x;
153 return mc.precision == this.precision
154 && mc.roundMode.equals(this.roundMode);
155 }
156
157 /**
158 * Returns the precision setting.
159 * @return the precision setting.
160 */
161 public int getPrecision()
162 {
163 return precision;
164 }
165
166 /**
167 * Returns the rounding mode setting. This will be one of
168 * RoundingMode.CEILING, RoundingMode.DOWN, RoundingMode.FLOOR,
169 * RoundingMode.HALF_DOWN, RoundingMode.HALF_EVEN, RoundingMode.HALF_UP,
170 * RoundingMode.UNNECESSARY, or RoundingMode.UP.
171 * @return the rounding mode setting.
172 */
173 public RoundingMode getRoundingMode()
174 {
175 return roundMode;
176 }
177
178 /**
179 * Returns "precision=p roundingMode=MODE" where p is an int giving the
180 * precision and MODE is UP, DOWN, HALF_UP, HALF_DOWN, HALF_EVEN, CEILING,
181 * FLOOR, or UNNECESSARY corresponding to rounding modes.
182 *
183 * @return a String describing this MathContext
184 */
185 public String toString()
186 {
187 return "precision="+precision+" roundingMode="+roundMode;
188 }
189
190 /**
191 * Returns the hashcode for this MathContext.
192 * @return the hashcode for this MathContext.
193 */
194 public int hashCode()
195 {
196 return precision ^ roundMode.hashCode();
197 }
198 }