/**
   * Constructs a new {@code MathContext} from a string. The string has to specify the precision and
   * the rounding mode to be used and has to follow the following syntax:
   * "precision=<precision> roundingMode=<roundingMode>" This is the same form as the
   * one returned by the {@link #toString} method.
   *
   * @throws IllegalArgumentException if the string is not in the correct format or if the precision
   *     specified is < 0.
   */
  @DSComment("From safe class list")
  @DSSafe(DSCat.SAFE_LIST)
  @DSGenerator(
      tool_name = "Doppelganger",
      tool_version = "2.0",
      generated_on = "2013-12-30 12:56:34.348 -0500",
      hash_original_method = "1D8B76B7C700FBF158167DD4C05A3C30",
      hash_generated_method = "96CB7A0DB097A7BF33DE16B65A96E3E8")
  public MathContext(String s) {
    int precisionLength = "precision=".length();
    int roundingModeLength = "roundingMode=".length();

    int spaceIndex;
    if (!s.startsWith("precision=") || (spaceIndex = s.indexOf(' ', precisionLength)) == -1) {
      throw invalidMathContext("Missing precision", s);
    }
    String precisionString = s.substring(precisionLength, spaceIndex);
    try {
      this.precision = Integer.parseInt(precisionString);
    } catch (NumberFormatException nfe) {
      throw invalidMathContext("Bad precision", s);
    }

    int roundingModeStart = spaceIndex + 1;
    if (!s.regionMatches(roundingModeStart, "roundingMode=", 0, roundingModeLength)) {
      throw invalidMathContext("Missing rounding mode", s);
    }
    roundingModeStart += roundingModeLength;
    this.roundingMode = RoundingMode.valueOf(s.substring(roundingModeStart));

    checkValid();
  }
Exemple #2
0
  /**
   * 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;
  }