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

[Fmt] format() method in java.text.Format requires new FieldPosition object each time

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P5
    • Resolution: Won't Fix
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      A DESCRIPTION OF THE REQUEST :
      This bug report proposes an optimisation to java.text.Format that would benefit all sub-classes of Format *without changing their ABI, including Serialisation*.

      When you call Format.format(), frequently you don't care about the values returned in FieldPosition. Some format() methods even create a new instance of FieldPosition each time, which is what most other callers also do.

      However DateFormat, SimpleDateFormat and NumberFormat use an optimisation in a few cases; they use a package-private class DontCareFieldPosition and specifically it's INSTANCE member each time which makes use of a dummy FieldDelegate clas.

      I propose to add a new format(Object, StringBuffer) method to Format that would make use of DontCareFieldPosition.INSTANCE and expose this capability to all sub-classes of Format *without changing the visibility of DontCareFieldPosition or FieldDelegate*.

      The method is extremely simple:

          /**
           * Formats an object and appends the resulting text to a given string
           * buffer.
           *
           * @param obj The object to format
           * @param toAppendTo where the new date-time text is to be appended.
           * @return the formatted date-time string.
           * @exception NullPointerException if the given date is null
           */
          public StringBuffer format(Object obj, StringBuffer toAppendTo)
          {
              return format(obj, toAppendTo, DontCareFieldPosition.INSTANCE);
          }

      I suggest that this optimisation could also be applied to the Format.format(Object) method as follows:


          /**
           * Formats an object to produce a string. This is equivalent to
           * <blockquote>
           * {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj,
           * new StringBuffer(), new FieldPosition(0)).toString();</code>
           * </blockquote>
           *
           * @param obj The object to format
           * @return Formatted string.
           * @exception IllegalArgumentException if the Format cannot format the given
           * object
           */
          public final String format (Object obj) {
              return format(obj, new StringBuffer()).toString();
          }


      JUSTIFICATION :
      The JDK makes partial internal use of an optimisation that prevents creating new objects unnecessarily. This minor change would make further use of this optimisation within the JDK and make it available to Java application developers.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                nishjain Nishit Jain (Inactive)
                Reporter:
                ndcosta Nelson Dcosta
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: