/** * Constructs a new {@code MathContext} from a string. * * <p>The string must be in the same format as that produced by the {@link #toString} method. * * <p>An {@code IllegalArgumentException} is thrown if the precision section of the string is out * of range ({@code < 0}) or the string is not in the format created by the {@link #toString} * method. * * @param val The string to be parsed * @throws IllegalArgumentException if the precision section is out of range or of incorrect * format * @throws NullPointerException if the argument is {@code null} */ public MathContext(String val) { boolean bad = false; int setPrecision; if (val == null) throw new NullPointerException("null String"); try { // any error here is a string format problem if (!val.startsWith("precision=")) throw new RuntimeException(); int fence = val.indexOf(' '); // could be -1 int off = 10; // where value starts setPrecision = Integer.parseInt(val.substring(10, fence)); if (!val.startsWith("roundingMode=", fence + 1)) throw new RuntimeException(); off = fence + 1 + 13; String str = val.substring(off, val.length()); roundingMode = RoundingMode.valueOf(str); } catch (RuntimeException re) { throw new IllegalArgumentException("bad string format"); } if (setPrecision < MIN_DIGITS) throw new IllegalArgumentException("Digits < 0"); // the other parameters cannot be invalid if we got here precision = setPrecision; }