From 97b8365cafc3a344a22d3980b8ed885f5c6d8357 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 9 Jan 2007 19:58:05 +0000 Subject: Merged gcj-eclipse branch to trunk. From-SVN: r120621 --- libjava/classpath/java/lang/StringBuffer.java | 120 +++++++++++++------------- 1 file changed, 61 insertions(+), 59 deletions(-) (limited to 'libjava/classpath/java/lang/StringBuffer.java') diff --git a/libjava/classpath/java/lang/StringBuffer.java b/libjava/classpath/java/lang/StringBuffer.java index caffd6e7050..3aa84a21e12 100644 --- a/libjava/classpath/java/lang/StringBuffer.java +++ b/libjava/classpath/java/lang/StringBuffer.java @@ -72,8 +72,12 @@ import java.io.Serializable; * @since 1.0 * @status updated to 1.4 */ -public final class StringBuffer implements Serializable, CharSequence +public final class StringBuffer + implements Serializable, CharSequence, Appendable { + // Implementation note: if you change this class, you usually will + // want to change StringBuilder as well. + /** * Compatible with JDK 1.0+. */ @@ -148,21 +152,22 @@ public final class StringBuffer implements Serializable, CharSequence } /** - * Create a new StringBuffer with the characters from the + * Create a new StringBuffer with the characters in the * specified CharSequence. Initial capacity will be the - * size of the CharSequence plus 16. + * length of the sequence plus 16; if the sequence reports a length + * less than or equal to 0, then the initial capacity will be 16. * - * @param sequence the String to convert + * @param seq the initializing CharSequence * @throws NullPointerException if str is null - * * @since 1.5 */ - public StringBuffer(CharSequence sequence) + public StringBuffer(CharSequence seq) { - count = Math.max(0, sequence.length()); + int len = seq.length(); + count = len <= 0 ? 0 : len; value = new char[count + DEFAULT_CAPACITY]; - for (int i = 0; i < count; ++i) - value[i] = sequence.charAt(i); + for (int i = 0; i < len; ++i) + value[i] = seq.charAt(i); } /** @@ -390,46 +395,6 @@ public final class StringBuffer implements Serializable, CharSequence return this; } - /** - * Append the CharSequence value of the argument to this - * StringBuffer. - * - * @param sequence the CharSequence to append - * @return this StringBuffer - * @see #append(Object) - * @since 1.5 - */ - public synchronized StringBuffer append(CharSequence sequence) - { - if (sequence == null) - sequence = "null"; - return append(sequence, 0, sequence.length()); - } - - /** - * Append the specified subsequence of the CharSequence - * argument to this StringBuffer. - * - * @param sequence the CharSequence to append - * @param start the starting index - * @param end one past the ending index - * @return this StringBuffer - * @see #append(Object) - * @since 1.5 - */ - public synchronized StringBuffer append(CharSequence sequence, - int start, int end) - { - if (sequence == null) - sequence = "null"; - if (start < 0 || end < 0 || start > end || end > sequence.length()) - throw new IndexOutOfBoundsException(); - ensureCapacity_unsynchronized(this.count + end - start); - for (int i = start; i < end; ++i) - value[count++] = sequence.charAt(i); - return this; - } - /** * Append the char array to this StringBuffer. * This is similar (but more efficient) than @@ -469,6 +434,25 @@ public final class StringBuffer implements Serializable, CharSequence return this; } + /** + * Append the code point to this StringBuffer. + * This is like #append(char), but will append two characters + * if a supplementary code point is given. + * + * @param code the code point to append + * @return this StringBuffer + * @see Character#toChars(int, char[], int) + * @since 1.5 + */ + public synchronized StringBuffer appendCodePoint(int code) + { + int len = Character.charCount(code); + ensureCapacity_unsynchronized(count + len); + Character.toChars(code, value, count); + count += len; + return this; + } + /** * Append the String value of the argument to this * StringBuffer. Uses String.valueOf() to convert @@ -497,21 +481,39 @@ public final class StringBuffer implements Serializable, CharSequence } /** - * Append the code point to this StringBuffer. - * This is like #append(char), but will append two characters - * if a supplementary code point is given. + * Append the characters in the CharSequence to this + * buffer. * - * @param code the code point to append + * @param seq the CharSequence providing the characters * @return this StringBuffer - * @see Character#toChars(int, char[], int) * @since 1.5 */ - public synchronized StringBuffer appendCodePoint(int code) + public synchronized StringBuffer append(CharSequence seq) { - int len = Character.charCount(code); - ensureCapacity_unsynchronized(count + len); - Character.toChars(code, value, count); - count += len; + return append(seq, 0, seq.length()); + } + + /** + * Append some characters from the CharSequence to this + * buffer. If the argument is null, the four characters "null" are + * appended. + * + * @param seq the CharSequence providing the characters + * @param start the starting index + * @param end one past the final index + * @return this StringBuffer + * @since 1.5 + */ + public synchronized StringBuffer append(CharSequence seq, int start, int end) + { + if (seq == null) + return append("null"); + if (end - start > 0) + { + ensureCapacity_unsynchronized(count + end - start); + for (; start < end; ++start) + value[count++] = seq.charAt(start); + } return this; } -- cgit v1.2.3