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

(str) Appendable.appendN(char, int) method to append multiple copies of char

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: tbd_major
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Understanding:
      Fix Understood

      Description

      There are several places where in JDK where the same character is appended multiple times (usually it is padding).

      For each individuall append a few manipulations are performed, which may be done only once.

      Such method may be added as a default method into Appendable interface:
      default Appendable appendN(char c, int n) {
          if (n < 0) throw new IndexOutOfBoundsException();
          for (int i = 0; i < n; ++i)
              append(c);
          return this;
      }

      AbstractStringBuilder may then provide an optimized overridden implementation.


      Examples of where such a method could be used in JDK:
      macosx/classes/sun/util/locale/provider/HostLocaleProviderAdapterImpl.java- private static void appendN(char c, int n, StringBuilder sb) {
      macosx/classes/sun/util/locale/provider/HostLocaleProviderAdapterImpl.java- for (int i = 0; i < n; i++) {
      macosx/classes/sun/util/locale/provider/HostLocaleProviderAdapterImpl.java: sb.append(c);
      macosx/classes/sun/util/locale/provider/HostLocaleProviderAdapterImpl.java- }
      macosx/classes/sun/util/locale/provider/HostLocaleProviderAdapterImpl.java- }

      share/classes/java/util/Formatter.java- for (int i = 0; i < sp; i++) {
      share/classes/java/util/Formatter.java: a.append(' ');
      share/classes/java/util/Formatter.java- }

      share/classes/java/util/Formatter.java- private void trailingZeros(StringBuilder sb, int nzeros) {
      share/classes/java/util/Formatter.java- for (int i = 0; i < nzeros; i++) {
      share/classes/java/util/Formatter.java: sb.append('0');
      share/classes/java/util/Formatter.java- }
      share/classes/java/util/Formatter.java- }

      share/classes/java/util/regex/PatternSyntaxException.java: for (int i = 0; i < index; i++) sb.append(' ');

      share/classes/java/math/BigInteger.java- int numLeadingZeros = digitsPerLong[radix]-digitGroup[i].length();
      share/classes/java/math/BigInteger.java- if (numLeadingZeros != 0) {
      share/classes/java/math/BigInteger.java: buf.append(zeros[numLeadingZeros]);
      share/classes/java/math/BigInteger.java- }

      share/classes/java/math/BigInteger.java- for (int i=s.length(); i < digits; i++) {
      share/classes/java/math/BigInteger.java: sb.append('0');
      share/classes/java/math/BigInteger.java- }

      share/classes/java/math/BigDecimal.java- for (int i = 0; i < trailingZeros; i++) {
      share/classes/java/math/BigDecimal.java: buf.append('0');
      share/classes/java/math/BigDecimal.java- }

      share/classes/java/math/BigDecimal.java- for (int i=0; i<-insertionPoint; i++) {
      share/classes/java/math/BigDecimal.java: buf.append('0');
      share/classes/java/math/BigDecimal.java- }

      share/classes/java/math/BigDecimal.java- for (; pad>0; pad--) {
      share/classes/java/math/BigDecimal.java: buf.append('0');
      share/classes/java/math/BigDecimal.java- }

      share/classes/java/text/DecimalFormat.java- for (int i=digitList.decimalAt; i<minExponentDigits; ++i) {
      share/classes/java/text/DecimalFormat.java: result.append(zero);
      share/classes/java/text/DecimalFormat.java- }

      share/classes/java/text/DecimalFormat.java- for (i=0; i<minExponentDigits; ++i)
      share/classes/java/text/DecimalFormat.java: result.append(localized ? symbols.getZeroDigit() :
      share/classes/java/text/DecimalFormat.java- PATTERN_ZERO_DIGIT);

      share/classes/java/text/DigitList.java- for (int i = count; i < decimalAt; ++i) {
      share/classes/java/text/DigitList.java: temp.append('0');
      share/classes/java/text/DigitList.java- }

      share/classes/java/lang/invoke/LambdaForm.java- for (int i = buf.length() - leadingZero; i < 3; i++)
      share/classes/java/lang/invoke/LambdaForm.java- buf.insert(leadingZero, '0');

      share/classes/java/time/format/DateTimeFormatterBuilder.java- for (int i = 0; i < minWidth - str.length(); i++) {
      share/classes/java/time/format/DateTimeFormatterBuilder.java: buf.append(decimalStyle.getZeroDigit());
      share/classes/java/time/format/DateTimeFormatterBuilder.java- }

      share/classes/java/time/format/DateTimeFormatterBuilder.java- for (int i = 0; i < minWidth; i++) {
      share/classes/java/time/format/DateTimeFormatterBuilder.java: buf.append(decimalStyle.getZeroDigit());
      share/classes/java/time/format/DateTimeFormatterBuilder.java- }

      share/classes/sun/util/calendar/CalendarUtils.java- for (int i = 1; i < width && d < n; i++) {
      share/classes/sun/util/calendar/CalendarUtils.java: sb.append('0');
      share/classes/sun/util/calendar/CalendarUtils.java- n /= 10;
      share/classes/sun/util/calendar/CalendarUtils.java- }

      share/classes/sun/util/calendar/CalendarUtils.java- for (int i = 1; i < width && d < n; i++) {
      share/classes/sun/util/calendar/CalendarUtils.java: sb.append('0');
      share/classes/sun/util/calendar/CalendarUtils.java- n /= 10;
      share/classes/sun/util/calendar/CalendarUtils.java- }

      share/classes/sun/security/x509/AVA.java- for (int i = 0; i < spaceCount; i++) {
      share/classes/sun/security/x509/AVA.java: temp.append(' ');
      share/classes/sun/security/x509/AVA.java- }



      Two more places, where we append copies of a string, not a char:

      share/classes/java/lang/Class.java- for (int i = 0; i < arrayDepth; i++)
      share/classes/java/lang/Class.java: sb.append("[]");

      share/classes/java/lang/Class.java- for (int i = 0; i < dimensions; i++) {
      share/classes/java/lang/Class.java: sb.append("[]");
      share/classes/java/lang/Class.java- }


        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                igerasim Ivan Gerasimov
                Reporter:
                igerasim Ivan Gerasimov
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: