Frames | No Frames |
1: /* MathContext.java -- 2: Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package java.math; 40: 41: import java.io.Serializable; 42: 43: /** 44: * Immutable objects describing settings such as rounding mode and digit 45: * precision for numerical operations such as those in the BigDecimal class. 46: * @author Anthony Balkissoon abalkiss at redhat dot com 47: * 48: */ 49: public final class MathContext implements Serializable 50: { 51: 52: /** 53: * This is the serialVersionUID reported here: 54: * java.sun.com/j2se/1.5.0/docs/api/serialized-form.html#java.math.MathContext 55: */ 56: private static final long serialVersionUID = 5579720004786848255L; 57: 58: private int precision; 59: 60: /** 61: * Constructs a new MathContext with the specified precision and with HALF_UP 62: * rounding. 63: * @param setPrecision the precision for the new MathContext 64: * 65: * @throws IllegalArgumentException if precision is < 0. 66: */ 67: public MathContext(int setPrecision) 68: { 69: if (setPrecision < 0) 70: throw new IllegalArgumentException("Precision cannot be less than zero."); 71: precision = setPrecision; 72: } 73: 74: /** 75: * Constructs a MathContext from a String that has the same form as one 76: * produced by the toString() method. 77: * @param val 78: * 79: * @throws IllegalArgumentException if the String is not in the correct 80: * format or if the precision specified is < 0. 81: */ 82: public MathContext(String val) 83: { 84: try 85: { 86: int roundingModeIndex = val.indexOf("roundingMode", 10); 87: precision = Integer.parseInt(val.substring(10, roundingModeIndex - 1)); 88: } 89: catch (NumberFormatException nfe) 90: { 91: throw new IllegalArgumentException("String not in correct format"); 92: } 93: catch (IllegalArgumentException iae) 94: { 95: throw new IllegalArgumentException("String not in correct format"); 96: } 97: if (precision < 0) 98: throw new IllegalArgumentException("Precision cannot be less than 0."); 99: } 100: 101: /** 102: * Returns true if x is a MathContext and has the same precision setting 103: * and rounding mode as this MathContext. 104: * 105: * @return true if the above conditions hold 106: */ 107: public boolean equals(Object x) 108: { 109: if (!(x instanceof MathContext)) 110: return false; 111: MathContext mc = (MathContext)x; 112: return mc.precision == this.precision; 113: } 114: 115: /** 116: * Returns the precision setting. 117: * @return the precision setting. 118: */ 119: public int getPrecision() 120: { 121: return precision; 122: } 123: 124: /** 125: * Returns "precision=p roundingMode=MODE" where p is an int giving the 126: * precision and MODE is UP, DOWN, HALF_UP, HALF_DOWN, HALF_EVEN, CEILING, 127: * FLOOR, or UNNECESSARY corresponding to rounding modes. 128: * 129: * @return a String describing this MathContext 130: */ 131: public String toString() 132: { 133: return "precision="+precision; 134: } 135: 136: /** 137: * Returns the hashcode for this MathContext. 138: * @return the hashcode for this MathContext. 139: */ 140: public int hashCode() 141: { 142: return precision; 143: } 144: }