1 7 8 17 18 package java.math; 19 import java.io.*; 20 21 42 43 public final class MathContext implements Serializable { 44 45 46 47 private static final int DEFAULT_DIGITS = 9; 49 private static final RoundingMode DEFAULT_ROUNDINGMODE = RoundingMode.HALF_UP; 50 private static final int MIN_DIGITS = 0; 52 53 private static final long serialVersionUID = 5579720004786848255L; 55 56 57 65 public static final MathContext UNLIMITED = 66 new MathContext (0, RoundingMode.HALF_UP); 67 68 74 public static final MathContext DECIMAL32 = 75 new MathContext (7, RoundingMode.HALF_EVEN); 76 77 83 public static final MathContext DECIMAL64 = 84 new MathContext (16, RoundingMode.HALF_EVEN); 85 86 92 public static final MathContext DECIMAL128 = 93 new MathContext (34, RoundingMode.HALF_EVEN); 94 95 96 106 final int precision; 107 108 114 final RoundingMode roundingMode; 115 116 125 transient BigInteger roundingMax = null; 126 transient BigInteger roundingMin = null; 127 private static final int MAX_LOOKASIDE = 1000; 128 129 130 131 140 public MathContext(int setPrecision) { 141 this(setPrecision, DEFAULT_ROUNDINGMODE); 142 return; 143 } 144 145 154 public MathContext(int setPrecision, 155 RoundingMode setRoundingMode) { 156 if (setPrecision < MIN_DIGITS) 157 throw new IllegalArgumentException ("Digits < 0"); 158 if (setRoundingMode == null) 159 throw new NullPointerException ("null RoundingMode"); 160 161 precision = setPrecision; 162 if (precision > 0 && precision <= MAX_LOOKASIDE) { 163 roundingMax = BigInteger.TEN.pow(precision); 164 roundingMin = roundingMax.negate(); 165 } 166 167 roundingMode = setRoundingMode; 168 return; 169 } 170 171 185 public MathContext(String val) { 186 boolean bad = false; 187 int setPrecision; 188 if (val == null) 189 throw new NullPointerException ("null String"); 190 try { if (!val.startsWith("precision=")) throw new RuntimeException (); 192 int fence = val.indexOf(' '); int off = 10; setPrecision = Integer.parseInt(val.substring(10, fence)); 195 196 if (!val.startsWith("roundingMode=", fence+1)) 197 throw new RuntimeException (); 198 off = fence + 1 + 13; 199 String str = val.substring(off, val.length()); 200 roundingMode = RoundingMode.valueOf(str); 201 } catch (RuntimeException re) { 202 throw new IllegalArgumentException ("bad string format"); 203 } 204 205 if (setPrecision < MIN_DIGITS) 206 throw new IllegalArgumentException ("Digits < 0"); 207 precision = setPrecision; 209 if (precision > 0 && precision <= MAX_LOOKASIDE) { 210 roundingMax = BigInteger.TEN.pow(precision); 211 roundingMin = roundingMax.negate(); 212 } 213 } 214 215 222 public int getPrecision() { 223 return precision; 224 } 225 226 241 242 public RoundingMode getRoundingMode() { 243 return roundingMode; 244 } 245 246 256 public boolean equals(Object x){ 257 MathContext mc; 258 if (!(x instanceof MathContext )) 259 return false; 260 mc = (MathContext ) x; 261 return mc.precision == this.precision 262 && mc.roundingMode == this.roundingMode; } 264 265 270 public int hashCode() { 271 return this.precision + roundingMode.hashCode() * 59; 272 } 273 274 306 public java.lang.String toString() { 307 return "precision=" + precision + " " + 308 "roundingMode=" + roundingMode.toString(); 309 } 310 311 313 319 private synchronized void readObject(java.io.ObjectInputStream s) 320 throws java.io.IOException , ClassNotFoundException { 321 s.defaultReadObject(); if (precision < MIN_DIGITS) { 324 String message = "MathContext: invalid digits in stream"; 325 throw new java.io.StreamCorruptedException (message); 326 } 327 if (roundingMode == null) { 328 String message = "MathContext: null roundingMode in stream"; 329 throw new java.io.StreamCorruptedException (message); 330 } 331 if (precision <= MAX_LOOKASIDE) { 333 roundingMax = BigInteger.TEN.pow(precision); 334 roundingMin = roundingMax.negate(); 335 } 336 } 337 338 } 339 | Popular Tags |