[PATCH] AbstractStringBuilder.append(<StringBuilder>)

Isaac Levy isaac.r.levy at gmail.com
Thu Jun 28 03:57:18 UTC 2018


AbstractStringBuilder already has append(<StringBuffer>). This patch
adds append(<StringBuilder>).

The new method improves parity between the two classes. In addition,
combining StringBuilders is presumably common. append(<CharSequence>)
has a couple insteadof checks, which this new method skips.

-Isaac




diff --git a/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
b/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
index 2ef3e53256..1fe89bb92a 100644
--- a/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
+++ b/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
@@ -543,6 +543,11 @@ abstract class AbstractStringBuilder implements
Appendable, CharSequence {
         return this.append((AbstractStringBuilder)sb);
     }

+    // Documentation in subclasses because of synchro difference
+    public AbstractStringBuilder append(StringBuilder sb) {
+        return this.append((AbstractStringBuilder)sb);
+    }
+
     /**
      * @since 1.8
      */
diff --git a/src/java.base/share/classes/java/lang/StringBuffer.java
b/src/java.base/share/classes/java/lang/StringBuffer.java
index e597a8112e..613ba90c25 100644
--- a/src/java.base/share/classes/java/lang/StringBuffer.java
+++ b/src/java.base/share/classes/java/lang/StringBuffer.java
@@ -313,6 +313,33 @@ import jdk.internal.HotSpotIntrinsicCandidate;
         return this;
     }

+    /**
+     * Appends the specified {@code StringBuilder} to this sequence.
+     * <p>
+     * The characters of the {@code StringBuilder} argument are appended,
+     * in order, to the contents of this {@code StringBuffer}, increasing the
+     * length of this {@code StringBuffer} by the length of the argument.
+     * If {@code sb} is {@code null}, then the four characters
+     * {@code "null"} are appended to this {@code StringBuffer}.
+     * <p>
+     * Let <i>n</i> be the length of the old character sequence, the one
+     * contained in the {@code StringBuffer} just prior to execution of the
+     * {@code append} method. Then the character at index <i>k</i> in
+     * the new character sequence is equal to the character at index <i>k</i>
+     * in the old character sequence, if <i>k</i> is less than <i>n</i>;
+     * otherwise, it is equal to the character at index <i>k-n</i> in the
+     * argument {@code sb}.
+     * <p>
+     *
+     * @param   sb   the {@code StringBuilder} to append.
+     * @return  a reference to this object.
+     */
+    public synchronized StringBuffer append(StringBuilder sb) {
+        toStringCache = null;
+        super.append(sb);
+        return this;
+    }
+
     /**
      * Appends the specified {@code StringBuffer} to this sequence.
      * <p>
diff --git a/src/java.base/share/classes/java/lang/StringBuilder.java
b/src/java.base/share/classes/java/lang/StringBuilder.java
index 40da2083c2..5ddd4fb5f9 100644
--- a/src/java.base/share/classes/java/lang/StringBuilder.java
+++ b/src/java.base/share/classes/java/lang/StringBuilder.java
@@ -199,6 +199,30 @@ public final class StringBuilder
         return this;
     }

+    /**
+     * Appends the specified {@code StringBuilder} to this sequence.
+     * <p>
+     * The characters of the {@code StringBuilder} argument are appended,
+     * in order, to this sequence, increasing the
+     * length of this sequence by the length of the argument.
+     * If {@code sb} is {@code null}, then the four characters
+     * {@code "null"} are appended to this sequence.
+     * <p>
+     * Let <i>n</i> be the length of this character sequence just prior to
+     * execution of the {@code append} method. Then the character at index
+     * <i>k</i> in the new character sequence is equal to the character at
+     * index <i>k</i> in the old character sequence, if <i>k</i> is less than
+     * <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
+     * in the argument {@code sb}.
+     *
+     * @param   sb   the {@code StringBuilder} to append.
+     * @return  a reference to this object.
+     */
+    public StringBuilder append(StringBuilder sb) {
+        super.append(sb);
+        return this;
+    }
+
     @Override
     public StringBuilder append(CharSequence s) {
         super.append(s);


More information about the core-libs-dev mailing list