diff --git a/jdk/src/share/classes/java/lang/AbstractStringBuilder.java b/jdk/src/share/classes/java/lang/AbstractStringBuilder.java index 94585f86e50..b87ebd73d9a 100644 --- a/jdk/src/share/classes/java/lang/AbstractStringBuilder.java +++ b/jdk/src/share/classes/java/lang/AbstractStringBuilder.java @@ -70,6 +70,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { * @return the length of the sequence of characters currently * represented by this object */ + @Override public int length() { return count; } @@ -200,6 +201,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { * @throws IndexOutOfBoundsException if {@code index} is * negative or greater than or equal to {@code length()}. */ + @Override public char charAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); @@ -431,14 +433,29 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { return this; } + /** + * @since 1.8 + */ + AbstractStringBuilder append(AbstractStringBuilder asb) { + if (asb == null) + return append("null"); + int len = asb.length(); + ensureCapacityInternal(count + len); + asb.getChars(0, len, value, count); + count += len; + return this; + } + // Documentation in subclasses because of synchro difference + @Override public AbstractStringBuilder append(CharSequence s) { if (s == null) s = "null"; if (s instanceof String) return this.append((String)s); - if (s instanceof StringBuffer) - return this.append((StringBuffer)s); + if (s instanceof AbstractStringBuilder) + return this.append((AbstractStringBuilder)s); + return this.append(s, 0, s.length()); } @@ -471,6 +488,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { * {@code start} is greater than {@code end} or * {@code end} is greater than {@code s.length()} */ + @Override public AbstractStringBuilder append(CharSequence s, int start, int end) { if (s == null) s = "null"; @@ -585,6 +603,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { * @param c a {@code char}. * @return a reference to this object. */ + @Override public AbstractStringBuilder append(char c) { ensureCapacityInternal(count + 1); value[count++] = c; @@ -847,6 +866,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { * or if start is greater than end * @spec JSR-51 */ + @Override public CharSequence subSequence(int start, int end) { return substring(start, end); } @@ -1397,6 +1417,7 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { * * @return a string representation of this sequence of characters. */ + @Override public abstract String toString(); /** diff --git a/jdk/src/share/classes/java/lang/StringBuffer.java b/jdk/src/share/classes/java/lang/StringBuffer.java index 4dee96e6795..9fabad48d68 100644 --- a/jdk/src/share/classes/java/lang/StringBuffer.java +++ b/jdk/src/share/classes/java/lang/StringBuffer.java @@ -149,15 +149,18 @@ package java.lang; append(seq); } + @Override public synchronized int length() { return count; } + @Override public synchronized int capacity() { return value.length; } + @Override public synchronized void ensureCapacity(int minimumCapacity) { if (minimumCapacity > value.length) { expandCapacity(minimumCapacity); @@ -167,6 +170,7 @@ package java.lang; /** * @since 1.5 */ + @Override public synchronized void trimToSize() { super.trimToSize(); } @@ -175,6 +179,7 @@ package java.lang; * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ + @Override public synchronized void setLength(int newLength) { super.setLength(newLength); } @@ -183,6 +188,7 @@ package java.lang; * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ + @Override public synchronized char charAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); @@ -192,6 +198,7 @@ package java.lang; /** * @since 1.5 */ + @Override public synchronized int codePointAt(int index) { return super.codePointAt(index); } @@ -199,6 +206,7 @@ package java.lang; /** * @since 1.5 */ + @Override public synchronized int codePointBefore(int index) { return super.codePointBefore(index); } @@ -206,6 +214,7 @@ package java.lang; /** * @since 1.5 */ + @Override public synchronized int codePointCount(int beginIndex, int endIndex) { return super.codePointCount(beginIndex, endIndex); } @@ -213,6 +222,7 @@ package java.lang; /** * @since 1.5 */ + @Override public synchronized int offsetByCodePoints(int index, int codePointOffset) { return super.offsetByCodePoints(index, codePointOffset); } @@ -221,6 +231,7 @@ package java.lang; * @throws NullPointerException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ + @Override public synchronized void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { @@ -231,17 +242,20 @@ package java.lang; * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ + @Override public synchronized void setCharAt(int index, char ch) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); value[index] = ch; } + @Override public synchronized StringBuffer append(Object obj) { super.append(String.valueOf(obj)); return this; } + @Override public synchronized StringBuffer append(String str) { super.append(str); return this; @@ -276,6 +290,14 @@ package java.lang; return this; } + /** + * @since 1.8 + */ + @Override + synchronized StringBuffer append(AbstractStringBuilder asb) { + super.append(asb); + return this; + } /** * Appends the specified {@code CharSequence} to this @@ -298,27 +320,26 @@ package java.lang; * @return a reference to this object. * @since 1.5 */ + @Override public StringBuffer append(CharSequence s) { - // Note, synchronization achieved via other invocations - if (s == null) - s = "null"; - if (s instanceof String) - return this.append((String)s); - if (s instanceof StringBuffer) - return this.append((StringBuffer)s); - return this.append(s, 0, s.length()); + // Note, synchronization achieved via invocations of other StringBuffer methods after + // narrowing of s to specific type + super.append(s); + return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ + @Override public synchronized StringBuffer append(CharSequence s, int start, int end) { super.append(s, start, end); return this; } + @Override public synchronized StringBuffer append(char[] str) { super.append(str); return this; @@ -327,21 +348,25 @@ package java.lang; /** * @throws IndexOutOfBoundsException {@inheritDoc} */ + @Override public synchronized StringBuffer append(char[] str, int offset, int len) { super.append(str, offset, len); return this; } + @Override public synchronized StringBuffer append(boolean b) { super.append(b); return this; } + @Override public synchronized StringBuffer append(char c) { super.append(c); return this; } + @Override public synchronized StringBuffer append(int i) { super.append(i); return this; @@ -350,21 +375,25 @@ package java.lang; /** * @since 1.5 */ + @Override public synchronized StringBuffer appendCodePoint(int codePoint) { super.appendCodePoint(codePoint); return this; } + @Override public synchronized StringBuffer append(long lng) { super.append(lng); return this; } + @Override public synchronized StringBuffer append(float f) { super.append(f); return this; } + @Override public synchronized StringBuffer append(double d) { super.append(d); return this; @@ -374,6 +403,7 @@ package java.lang; * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ + @Override public synchronized StringBuffer delete(int start, int end) { super.delete(start, end); return this; @@ -383,6 +413,7 @@ package java.lang; * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ + @Override public synchronized StringBuffer deleteCharAt(int index) { super.deleteCharAt(index); return this; @@ -392,6 +423,7 @@ package java.lang; * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ + @Override public synchronized StringBuffer replace(int start, int end, String str) { super.replace(start, end, str); return this; @@ -401,6 +433,7 @@ package java.lang; * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ + @Override public synchronized String substring(int start) { return substring(start, count); } @@ -409,6 +442,7 @@ package java.lang; * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.4 */ + @Override public synchronized CharSequence subSequence(int start, int end) { return super.substring(start, end); } @@ -417,6 +451,7 @@ package java.lang; * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ + @Override public synchronized String substring(int start, int end) { return super.substring(start, end); } @@ -425,6 +460,7 @@ package java.lang; * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ + @Override public synchronized StringBuffer insert(int index, char[] str, int offset, int len) { @@ -435,6 +471,7 @@ package java.lang; /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ + @Override public synchronized StringBuffer insert(int offset, Object obj) { super.insert(offset, String.valueOf(obj)); return this; @@ -443,6 +480,7 @@ package java.lang; /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ + @Override public synchronized StringBuffer insert(int offset, String str) { super.insert(offset, str); return this; @@ -451,6 +489,7 @@ package java.lang; /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ + @Override public synchronized StringBuffer insert(int offset, char[] str) { super.insert(offset, str); return this; @@ -460,21 +499,21 @@ package java.lang; * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ + @Override public StringBuffer insert(int dstOffset, CharSequence s) { - // Note, synchronization achieved via other invocations - if (s == null) - s = "null"; - if (s instanceof String) - return this.insert(dstOffset, (String)s); - return this.insert(dstOffset, s, 0, s.length()); + // Note, synchronization achieved via invocations of other StringBuffer methods + // after narrowing of s to specific type + super.insert(dstOffset, s); + return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ + @Override public synchronized StringBuffer insert(int dstOffset, CharSequence s, - int start, int end) + int start, int end) { super.insert(dstOffset, s, start, end); return this; @@ -483,13 +522,18 @@ package java.lang; /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ - public StringBuffer insert(int offset, boolean b) { - return insert(offset, String.valueOf(b)); + @Override + public StringBuffer insert(int offset, boolean b) { + // Note, synchronization achieved via invocation of StringBuffer insert(int, String) + // after conversion of b to String by super class method + super.insert(offset, b); + return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ + @Override public synchronized StringBuffer insert(int offset, char c) { super.insert(offset, c); return this; @@ -498,54 +542,73 @@ package java.lang; /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ + @Override public StringBuffer insert(int offset, int i) { - return insert(offset, String.valueOf(i)); + // Note, synchronization achieved via invocation of StringBuffer insert(int, String) + // after conversion of i to String by super class method + super.insert(offset, i); + return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ + @Override public StringBuffer insert(int offset, long l) { - return insert(offset, String.valueOf(l)); + // Note, synchronization achieved via invocation of StringBuffer insert(int, String) + // after conversion of l to String by super class method + super.insert(offset, l); + return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ + @Override public StringBuffer insert(int offset, float f) { - return insert(offset, String.valueOf(f)); + // Note, synchronization achieved via invocation of StringBuffer insert(int, String) + // after conversion of f to String by super class method + super.insert(offset, f); + return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ + @Override public StringBuffer insert(int offset, double d) { - return insert(offset, String.valueOf(d)); + // Note, synchronization achieved via invocation of StringBuffer insert(int, String) + // after conversion of d to String by super class method + super.insert(offset, d); + return this; } /** * @throws NullPointerException {@inheritDoc} * @since 1.4 */ + @Override public int indexOf(String str) { - return indexOf(str, 0); + // Note, synchronization achieved via invocations of other StringBuffer methods + return super.indexOf(str); } /** * @throws NullPointerException {@inheritDoc} * @since 1.4 */ + @Override public synchronized int indexOf(String str, int fromIndex) { - return String.indexOf(value, 0, count, - str.toCharArray(), 0, str.length(), fromIndex); + return super.indexOf(str, fromIndex); } /** * @throws NullPointerException {@inheritDoc} * @since 1.4 */ + @Override public int lastIndexOf(String str) { - // Note, synchronization achieved via other invocations + // Note, synchronization achieved via invocations of other StringBuffer methods return lastIndexOf(str, count); } @@ -553,19 +616,21 @@ package java.lang; * @throws NullPointerException {@inheritDoc} * @since 1.4 */ + @Override public synchronized int lastIndexOf(String str, int fromIndex) { - return String.lastIndexOf(value, 0, count, - str.toCharArray(), 0, str.length(), fromIndex); + return super.lastIndexOf(str, fromIndex); } /** * @since JDK1.0.2 */ + @Override public synchronized StringBuffer reverse() { super.reverse(); return this; } + @Override public synchronized String toString() { return new String(value, 0, count); } diff --git a/jdk/src/share/classes/java/lang/StringBuilder.java b/jdk/src/share/classes/java/lang/StringBuilder.java index eea5f459a90..313eae77c3b 100644 --- a/jdk/src/share/classes/java/lang/StringBuilder.java +++ b/jdk/src/share/classes/java/lang/StringBuilder.java @@ -124,28 +124,17 @@ public final class StringBuilder append(seq); } + @Override public StringBuilder append(Object obj) { return append(String.valueOf(obj)); } + @Override public StringBuilder append(String str) { super.append(str); return this; } - // Appends the specified string builder to this sequence. - private StringBuilder append(StringBuilder sb) { - if (sb == null) - return append("null"); - int len = sb.length(); - int newcount = count + len; - if (newcount > value.length) - expandCapacity(newcount); - sb.getChars(0, len, value, count); - count = newcount; - return this; - } - /** * Appends the specified StringBuffer to this sequence. *
@@ -170,28 +159,22 @@ public final class StringBuilder
return this;
}
- /**
- */
+ @Override
public StringBuilder append(CharSequence s) {
- if (s == null)
- s = "null";
- if (s instanceof String)
- return this.append((String)s);
- if (s instanceof StringBuffer)
- return this.append((StringBuffer)s);
- if (s instanceof StringBuilder)
- return this.append((StringBuilder)s);
- return this.append(s, 0, s.length());
+ super.append(s);
+ return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder append(CharSequence s, int start, int end) {
super.append(s, start, end);
return this;
}
+ @Override
public StringBuilder append(char[] str) {
super.append(str);
return this;
@@ -200,36 +183,43 @@ public final class StringBuilder
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder append(char[] str, int offset, int len) {
super.append(str, offset, len);
return this;
}
+ @Override
public StringBuilder append(boolean b) {
super.append(b);
return this;
}
+ @Override
public StringBuilder append(char c) {
super.append(c);
return this;
}
+ @Override
public StringBuilder append(int i) {
super.append(i);
return this;
}
+ @Override
public StringBuilder append(long lng) {
super.append(lng);
return this;
}
+ @Override
public StringBuilder append(float f) {
super.append(f);
return this;
}
+ @Override
public StringBuilder append(double d) {
super.append(d);
return this;
@@ -238,6 +228,7 @@ public final class StringBuilder
/**
* @since 1.5
*/
+ @Override
public StringBuilder appendCodePoint(int codePoint) {
super.appendCodePoint(codePoint);
return this;
@@ -246,6 +237,7 @@ public final class StringBuilder
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder delete(int start, int end) {
super.delete(start, end);
return this;
@@ -254,6 +246,7 @@ public final class StringBuilder
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder deleteCharAt(int index) {
super.deleteCharAt(index);
return this;
@@ -262,6 +255,7 @@ public final class StringBuilder
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder replace(int start, int end, String str) {
super.replace(start, end, str);
return this;
@@ -270,6 +264,7 @@ public final class StringBuilder
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int index, char[] str, int offset,
int len)
{
@@ -280,13 +275,16 @@ public final class StringBuilder
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int offset, Object obj) {
- return insert(offset, String.valueOf(obj));
+ super.insert(offset, obj);
+ return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int offset, String str) {
super.insert(offset, str);
return this;
@@ -295,6 +293,7 @@ public final class StringBuilder
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int offset, char[] str) {
super.insert(offset, str);
return this;
@@ -303,17 +302,16 @@ public final class StringBuilder
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int dstOffset, CharSequence s) {
- if (s == null)
- s = "null";
- if (s instanceof String)
- return this.insert(dstOffset, (String)s);
- return this.insert(dstOffset, s, 0, s.length());
+ super.insert(dstOffset, s);
+ return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int dstOffset, CharSequence s,
int start, int end)
{
@@ -324,6 +322,7 @@ public final class StringBuilder
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int offset, boolean b) {
super.insert(offset, b);
return this;
@@ -332,6 +331,7 @@ public final class StringBuilder
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int offset, char c) {
super.insert(offset, c);
return this;
@@ -340,66 +340,78 @@ public final class StringBuilder
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int offset, int i) {
- return insert(offset, String.valueOf(i));
+ super.insert(offset, i);
+ return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int offset, long l) {
- return insert(offset, String.valueOf(l));
+ super.insert(offset, l);
+ return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int offset, float f) {
- return insert(offset, String.valueOf(f));
+ super.insert(offset, f);
+ return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
+ @Override
public StringBuilder insert(int offset, double d) {
- return insert(offset, String.valueOf(d));
+ super.insert(offset, d);
+ return this;
}
/**
* @throws NullPointerException {@inheritDoc}
*/
+ @Override
public int indexOf(String str) {
- return indexOf(str, 0);
+ return super.indexOf(str);
}
/**
* @throws NullPointerException {@inheritDoc}
*/
+ @Override
public int indexOf(String str, int fromIndex) {
- return String.indexOf(value, 0, count,
- str.toCharArray(), 0, str.length(), fromIndex);
+ return super.indexOf(str, fromIndex);
}
/**
* @throws NullPointerException {@inheritDoc}
*/
+ @Override
public int lastIndexOf(String str) {
- return lastIndexOf(str, count);
+ return super.lastIndexOf(str);
}
/**
* @throws NullPointerException {@inheritDoc}
*/
+ @Override
public int lastIndexOf(String str, int fromIndex) {
- return String.lastIndexOf(value, 0, count,
- str.toCharArray(), 0, str.length(), fromIndex);
+ return super.lastIndexOf(str, fromIndex);
}
+ @Override
public StringBuilder reverse() {
super.reverse();
return this;
}
+ @Override
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
diff --git a/jdk/test/java/lang/StringBuffer/AppendStringBuilder.java b/jdk/test/java/lang/StringBuffer/AppendStringBuilder.java
new file mode 100644
index 00000000000..a27f6a12a28
--- /dev/null
+++ b/jdk/test/java/lang/StringBuffer/AppendStringBuilder.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/* @test
+ * @bug 6206780
+ * @summary Test StringBuffer.append(StringBuilder);
+ */
+
+import java.util.Random;
+
+public class AppendStringBuilder {
+ private static Random generator = new Random();
+
+ public static void main(String[] args) throws Exception {
+ for (int i=0; i<1000; i++) {
+ StringBuilder sb1 = generateTestBuilder(10, 100);
+ StringBuilder sb2 = generateTestBuilder(10, 100);
+ StringBuilder sb3 = generateTestBuilder(10, 100);
+ String s1 = sb1.toString();
+ String s2 = sb2.toString();
+ String s3 = sb3.toString();
+
+ String concatResult = new String(s1+s2+s3);
+
+ StringBuffer test = new StringBuffer();
+ test.append(sb1);
+ test.append(sb2);
+ test.append(sb3);
+
+ if (!test.toString().equals(concatResult))
+ throw new RuntimeException("StringBuffer.append failure");
+ }
+ }
+
+ private static int getRandomIndex(int constraint1, int constraint2) {
+ int range = constraint2 - constraint1;
+ int x = generator.nextInt(range);
+ return constraint1 + x;
+ }
+
+ private static StringBuilder generateTestBuilder(int min, int max) {
+ StringBuilder aNewStringBuilder = new StringBuilder(120);
+ int aNewLength = getRandomIndex(min, max);
+ for(int y=0; y