Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8353327

Specification of ChoiceFormat#parse(String, ParsePosition) is inadequate

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 25
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      There are two conformance considerations regarding compatibility.

      1) It is now specified that the Number sub-type returned is always Double. Hypothetically, it may be possible that some implementation normalizes the return value to a type other than Double. While this behavior could exist, `parse` returns the `limit` associated with the `format` parsed. As ChoiceFormat `limit`s are of type Double, it would be odd to expect any other Number sub-type.

      2) The new specification specifies that a parse failure returns `Double.NaN`. While this has been the long standing behavior for the reference implementation, an external implementation could have other behavior (e.g. throw exception). However, it would be consistent with the other NumberFormat subclasses to return an error value for `parse(String, ParsePosition)`, thus the decision to make this behavior normative.
      Show
      There are two conformance considerations regarding compatibility. 1) It is now specified that the Number sub-type returned is always Double. Hypothetically, it may be possible that some implementation normalizes the return value to a type other than Double. While this behavior could exist, `parse` returns the `limit` associated with the `format` parsed. As ChoiceFormat `limit`s are of type Double, it would be odd to expect any other Number sub-type. 2) The new specification specifies that a parse failure returns `Double.NaN`. While this has been the long standing behavior for the reference implementation, an external implementation could have other behavior (e.g. throw exception). However, it would be consistent with the other NumberFormat subclasses to return an error value for `parse(String, ParsePosition)`, thus the decision to make this behavior normative.
    • Java API
    • SE

      Summary

      Update the inadequate ChoiceFormat#parse(String, ParsePosition) specification.

      Problem

      The specification for ChoiceFormat#parse(String, ParsePosition) simply states "Parses a Number from the input text" which does not indicate at all how the Number is returned. As the method returns Number and is named parse, the current wording does not provide any new information.

      Solution

      Provide a specification which reflects how the value is returned.

      Additionally, provide wording which states that the Number sub-type is always Double, which allows callers to better utilize the returned value. There is no benefit in allowing flexibility of the sub-type for other implementations, since this method should return a ChoiceFormat limit, which is of type Double.

      Specification

           /**
      -     * Parses a Number from the input text.
      +     * Parses the input text starting at the index given by the {@code ParsePosition}
      +     * as a {@code Double}. The value returned is the {@code limit} corresponding
      +     * to the {@code format} that is the longest substring of the input text.
      +     * Matching is done in ascending order, when multiple {@code format}s match
      +     * the text equivalently in strength, the first matching {@code limit} is
      +     * returned. If there is no match, {@code Double.NaN} is returned.
      +     * <p>
      +     * For example,
      +     * {@snippet lang=java :
      +     * var fmt = new ChoiceFormat("0#foo|1#bar|2#baz");
      +     * fmt.parse("baz", new ParsePosition(0)); // returns 2.0
      +     * fmt.parse("quux", new ParsePosition(0)); // returns NaN
      +     * }
      +     *
            * @param text the source text.
            * @param status an input-output parameter.  On input, the
            * status.index field indicates the first character of the
      @@ -567,7 +580,8 @@
            * in the source text.  On exit, if an error did occur,
            * status.index is unchanged and status.errorIndex is set to the
            * first index of the character that caused the parse to fail.
      -     * @return A Number representing the value of the number parsed.
      +     * @return A Number which represents the {@code limit} corresponding to the
      +     * {@code format} parsed, or {@code Double.NaN} if the parse fails.

            jlu Justin Lu
            jlu Justin Lu
            Naoto Sato
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: