< prev index next >

src/java.base/share/classes/java/lang/AbstractStringBuilder.java

Print this page

        

@@ -545,10 +545,14 @@
             return this.append((String)s);
         }
         if (s instanceof AbstractStringBuilder) {
             return this.append((AbstractStringBuilder)s);
         }
+        if (s instanceof CharRepetitions) {
+            CharRepetitions cr = (CharRepetitions) s;
+            return this.appendN(cr.getChar(), cr.length());
+        }
         return this.append(s, 0, s.length());
     }
 
     private AbstractStringBuilder appendNull() {
         ensureCapacityInternal(count + 4);

@@ -741,10 +745,57 @@
         }
         return this;
     }
 
     /**
+     * Appends {@code n} copies of the string representation of
+     * the {@code char} argument to this sequence.
+     * <p>
+     * The first argument is appended to the contents of this
+     * sequence specified amount of times.
+     * The length of this sequence increases by {@code n}.
+     * <p>
+     * The overall effect is exactly as if the first argument
+     * were converted to a string by the method
+     * {@link String#valueOf(char)}, and the character in that
+     * string were then {@link #append(String) appended} to this
+     * character sequence {@code n} times.
+     *
+     * @param   c   a {@code char}.
+     * @param   n   a number of copies to append.
+     * @return  a reference to this object.
+     * @throws  IllegalArgumentException  if {@code n < 0}.
+     */
+    @Override
+    public AbstractStringBuilder appendN(char c, int n) {
+        if (n <= 0) {
+            if (n < 0) {
+                throw new IllegalArgumentException(
+                        "Negative number of copies: " + n);
+            }
+        } else {
+            int count = this.count;
+            ensureCapacityInternal(count + n);
+            if (isLatin1() && StringLatin1.canEncode(c)) {
+                byte b = (byte)c;
+                do {
+                    value[count++] = b;
+                } while (--n > 0);
+            } else {
+                if (isLatin1()) {
+                    inflate();
+                }
+                do {
+                    StringUTF16.putCharSB(value, count++, c);
+                } while (--n > 0);
+            }
+            this.count = count;
+        }
+        return this;
+    }
+
+    /**
      * Appends the string representation of the {@code int}
      * argument to this sequence.
      * <p>
      * The overall effect is exactly as if the argument were converted
      * to a string by the method {@link String#valueOf(int)},
< prev index next >