Add append(StringBuffer,int,int) and append(StrBuilder,int,int)

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@230911 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-08-08 22:03:31 +00:00
parent 3aa15e0c47
commit eac9add081
2 changed files with 210 additions and 6 deletions

View File

@ -342,7 +342,7 @@ public class StrBuilder implements Cloneable {
//-----------------------------------------------------------------------
/**
* Appends the text representing <code>null</code> to the string builder.
* Appends the text representing <code>null</code> to this string builder.
*
* @return this, to enable chaining
*/
@ -354,7 +354,7 @@ public class StrBuilder implements Cloneable {
}
/**
* Appends an object to the string builder.
* Appends an object to this string builder.
* Appending null will call {@link #appendNull()}.
*
* @param obj the object to append
@ -368,7 +368,7 @@ public class StrBuilder implements Cloneable {
}
/**
* Appends a string to the string builder.
* Appends a string to this string builder.
* Appending null will call {@link #appendNull()}.
*
* @param str the string to append
@ -389,7 +389,7 @@ public class StrBuilder implements Cloneable {
}
/**
* Appends a string to the string builder.
* Appends part of a string to this string builder.
* Appending null will call {@link #appendNull()}.
*
* @param str the string to append
@ -417,7 +417,7 @@ public class StrBuilder implements Cloneable {
}
/**
* Appends a string buffer to the string builder.
* Appends a string buffer to this string builder.
* Appending null will call {@link #appendNull()}.
*
* @param str the string buffer to append
@ -438,7 +438,35 @@ public class StrBuilder implements Cloneable {
}
/**
* Appends another string builder to the string builder.
* Appends part of a string buffer to this string builder.
* Appending null will call {@link #appendNull()}.
*
* @param str the string to append
* @param startIndex the start index, inclusive, must be valid
* @param length the length to append, must be valid
* @return this, to enable chaining
*/
public StrBuilder append(StringBuffer str, int startIndex, int length) {
if (str == null) {
return appendNull();
}
if (startIndex < 0 || startIndex > str.length()) {
throw new StringIndexOutOfBoundsException("startIndex must be valid");
}
if (length < 0 || (startIndex + length) > str.length()) {
throw new StringIndexOutOfBoundsException("length must be valid");
}
if (length > 0) {
int len = length();
ensureCapacity(len + length);
str.getChars(startIndex, startIndex + length, buffer, len);
size += length;
}
return this;
}
/**
* Appends another string builder to this string builder.
* Appending null will call {@link #appendNull()}.
*
* @param str the string builder to append
@ -458,6 +486,34 @@ public class StrBuilder implements Cloneable {
return this;
}
/**
* Appends part of a string builder to this string builder.
* Appending null will call {@link #appendNull()}.
*
* @param str the string to append
* @param startIndex the start index, inclusive, must be valid
* @param length the length to append, must be valid
* @return this, to enable chaining
*/
public StrBuilder append(StrBuilder str, int startIndex, int length) {
if (str == null) {
return appendNull();
}
if (startIndex < 0 || startIndex > str.length()) {
throw new StringIndexOutOfBoundsException("startIndex must be valid");
}
if (length < 0 || (startIndex + length) > str.length()) {
throw new StringIndexOutOfBoundsException("length must be valid");
}
if (length > 0) {
int len = length();
ensureCapacity(len + length);
str.getChars(startIndex, startIndex + length, buffer, len);
size += length;
}
return this;
}
/**
* Appends a char array to the string builder.
* Appending null has no effect.

View File

@ -635,6 +635,154 @@ public class StrBuilderTest extends TestCase {
assertEquals("foobar", sb.toString());
}
public void testAppend_StringBuffer() {
StrBuilder sb = new StrBuilder();
sb.append(new StringBuffer("foo"));
assertEquals("foo", sb.toString());
sb.append((StringBuffer) null);
assertEquals("foo", sb.toString());
sb.append(new StringBuffer(""));
assertEquals("foo", sb.toString());
sb.append(new StringBuffer("bar"));
assertEquals("foobar", sb.toString());
}
public void testAppend_StringBuffer_int_int() {
StrBuilder sb = new StrBuilder();
sb.append(new StringBuffer("foo"), 0, 3);
assertEquals("foo", sb.toString());
sb.append((StringBuffer) null, 0, 1);
assertEquals("foo", sb.toString());
try {
sb.append(new StringBuffer("bar"), -1, 1);
fail("append(char[], -1,) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StringBuffer("bar"), 3, 1);
fail("append(char[], 3,) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StringBuffer("bar"), 1, -1);
fail("append(char[],, -1) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StringBuffer("bar"), 1, 3);
fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StringBuffer("bar"), -1, 3);
fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StringBuffer("bar"), 4, 0);
fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
sb.append(new StringBuffer("bar"), 3, 0);
assertEquals("foo", sb.toString());
sb.append(new StringBuffer("abcbardef"), 3, 3);
assertEquals("foobar", sb.toString());
}
public void testAppend_StrBuilder() {
StrBuilder sb = new StrBuilder();
sb.append(new StrBuilder("foo"));
assertEquals("foo", sb.toString());
sb.append((StrBuilder) null);
assertEquals("foo", sb.toString());
sb.append(new StrBuilder(""));
assertEquals("foo", sb.toString());
sb.append(new StrBuilder("bar"));
assertEquals("foobar", sb.toString());
}
public void testAppend_StrBuilder_int_int() {
StrBuilder sb = new StrBuilder();
sb.append(new StrBuilder("foo"), 0, 3);
assertEquals("foo", sb.toString());
sb.append((StrBuilder) null, 0, 1);
assertEquals("foo", sb.toString());
try {
sb.append(new StrBuilder("bar"), -1, 1);
fail("append(char[], -1,) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StrBuilder("bar"), 3, 1);
fail("append(char[], 3,) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StrBuilder("bar"), 1, -1);
fail("append(char[],, -1) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StrBuilder("bar"), 1, 3);
fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StrBuilder("bar"), -1, 3);
fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
sb.append(new StrBuilder("bar"), 4, 0);
fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// expected
}
sb.append(new StrBuilder("bar"), 3, 0);
assertEquals("foo", sb.toString());
sb.append(new StrBuilder("abcbardef"), 3, 3);
assertEquals("foobar", sb.toString());
}
public void testAppend_CharArray() {
StrBuilder sb = new StrBuilder();