Пример #1
0
  /**
   * 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();
  }
Пример #2
0
 /**
  * Returns the hash code for this {@code MathContext} instance.
  *
  * @return the hash code for this {@code MathContext}.
  */
 @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.364 -0500",
     hash_original_method = "CDDF05FC589C0F0146B62DD26D215A30",
     hash_generated_method = "B195C6308746B1DAC1EA450308C3E284")
 @Override
 public int hashCode() {
   // Make place for the necessary bits to represent 8 rounding modes
   return ((precision << 3) | roundingMode.ordinal());
 }
Пример #3
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;
  }
Пример #4
0
 /**
  * Returns the string representation of this {@code MathContext}. The {@code String} returned
  * represents the settings of the {@code MathContext} object as two space-delimited words
  * (separated by a single space character, <tt>'&#92;u0020'</tt>, and with no leading or trailing
  * white space), as follows:
  *
  * <ol>
  *   <li>The string {@code "precision="}, immediately followed by the value of the precision
  *       setting as a numeric string as if generated by the {@link Integer#toString(int)
  *       Integer.toString} method.
  *   <li>The string {@code "roundingMode="}, immediately followed by the value of the {@code
  *       roundingMode} setting as a word. This word will be the same as the name of the
  *       corresponding public constant in the {@link RoundingMode} enum.
  * </ol>
  *
  * <p>For example:
  *
  * <pre>
  * precision=9 roundingMode=HALF_UP
  * </pre>
  *
  * Additional words may be appended to the result of {@code toString} in the future if more
  * properties are added to this class.
  *
  * @return a {@code String} representing the context settings
  */
 public java.lang.String toString() {
   return "precision=" + precision + " " + "roundingMode=" + roundingMode.toString();
 }
Пример #5
0
 /**
  * Returns the hash code for this {@code MathContext}.
  *
  * @return hash code for this {@code MathContext}
  */
 public int hashCode() {
   return this.precision + roundingMode.hashCode() * 59;
 }