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

Matcher doesn't provide option to use StringBuilder

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8u20
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      A DESCRIPTION OF THE REQUEST :
      In many applications, StringBuilder is used over StringBuffer as unless synchronization is required, StringBuilder will generally be faster. Matcher, a commonly used class, has many methods related to the usage of a StringBuffer, and does not provide StringBuilder-based alternatives.

      In my opinion, methods should be added to allow the usage of a StringBuilder. Ideally, these shouldn't just create a StringBuffer object with the same contents as the StringBuilder, but instead be properly optimized for usage with StringBuilder.

      Quick Links:

      Matcher: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html
      StringBuffer: http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
      StringBuilder: http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

      JUSTIFICATION :
      While not a massive bug, this issue is something which wouldn't be incredibly difficult to implement and would generally help improve performance in areas where looped String concatenation is required along with the usage of a Matcher(s). This is not a particularly obscure situation.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would like to see methods in java.util.Matcher which take a StringBuffer become compatible with StringBuilders. Note that this is not a request to just add methods which convert a StringBuilder into a new StringBuffer, but one to add properly optimized methods for StringBuilder.
      ACTUAL -
      Currently, there are no methods which allow the usage of StringBuilder.

      ---------- BEGIN SOURCE ----------
      Pattern regex = Pattern.compile("^\\s*((-?[0-9]?\\.?[0-9]+\\s*){3})$");
      // StringBuilder would be optimal
      StringBuffer sb = new StringBuffer();
      for (String line : listOfStrings) {
          Matcher m = regex.matcher(line);
          if (m.find()) {
              m.appendReplacement(sb, "v $1");
              m.appendTail(sb);
              // do something with it (in the actual application this is outputted to a file)
              sb.setLength(0);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      As above, a StringBuffer can be used, but is not optimal.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              sherman Xueming Shen
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: