Source for java.math.MathContext

   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:   /** A MathContext for unlimited precision arithmetic * */
  52:   public static final MathContext UNLIMITED = 
  53:     new MathContext(0, RoundingMode.HALF_UP);
  54:   
  55:   /**
  56:    * A MathContext for the IEEE 754R Decimal32 format - 7 digit preicision and
  57:    * HALF_EVEN rounding.
  58:    */
  59:   public static final MathContext DECIMAL32 = 
  60:     new MathContext(7, RoundingMode.HALF_EVEN);
  61:   
  62:   /**
  63:    * A MathContext for the IEEE 754R Decimal64 format - 16 digit preicision and
  64:    * HALF_EVEN rounding.
  65:    */
  66:   public static final MathContext DECIMAL64 = 
  67:     new MathContext(16, RoundingMode.HALF_EVEN);
  68:   
  69:   /**
  70:    * A MathContext for the IEEE 754R Decimal128 format - 34 digit preicision and
  71:    * HALF_EVEN rounding.
  72:    */
  73:   public static final MathContext DECIMAL128 = 
  74:     new MathContext(34, RoundingMode.HALF_EVEN);
  75:   
  76:   /**
  77:    * This is the serialVersionUID reported here:
  78:    * java.sun.com/j2se/1.5.0/docs/api/serialized-form.html#java.math.MathContext
  79:    */
  80:   private static final long serialVersionUID = 5579720004786848255L;
  81:   
  82:   private int precision;
  83:   
  84:   private RoundingMode roundMode;
  85:   
  86:   /**
  87:    * Constructs a new MathContext with the specified precision and with HALF_UP
  88:    * rounding.
  89:    * @param setPrecision the precision for the new MathContext
  90:    * 
  91:    * @throws IllegalArgumentException if precision is < 0.
  92:    */
  93:   public MathContext(int setPrecision)
  94:   {
  95:     this(setPrecision, RoundingMode.HALF_UP);
  96:   }
  97:   
  98:   /**
  99:    * 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: }