Applying my patch to LANG-355, making StrBuilder implement Appendable

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@795604 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-07-19 19:41:50 +00:00
parent 4351e7a1a1
commit 944f642f0b
2 changed files with 37 additions and 1 deletions

View File

@ -71,7 +71,7 @@ import org.apache.commons.lang.SystemUtils;
* @since 2.2
* @version $Id$
*/
public class StrBuilder implements CharSequence {
public class StrBuilder implements CharSequence, Appendable {
/**
* The extra capacity for new builders.
@ -462,6 +462,36 @@ public class StrBuilder implements CharSequence {
return append(obj.toString());
}
/**
* Appends a CharSequence to this string builder.
* Appending null will call {@link #appendNull()}.
*
* @param seq the CharSequence to append
* @return this, to enable chaining
*/
public StrBuilder append(CharSequence seq) {
if (seq == null) {
return appendNull();
}
return append(seq.toString());
}
/**
* Appends part of a CharSequence to this string builder.
* Appending null will call {@link #appendNull()}.
*
* @param seq the CharSequence 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(CharSequence seq, int startIndex, int length) {
if (seq == null) {
return appendNull();
}
return append(seq.toString(), startIndex, length);
}
/**
* Appends a string to this string builder.
* Appending null will call {@link #appendNull()}.

View File

@ -138,6 +138,9 @@ public class StrBuilderAppendInsertTest extends TestCase {
sb.append(new StrBuilder("yes"));
assertEquals("foobazyes", sb.toString());
sb.append((CharSequence) "Seq");
assertEquals("foobazyesSeq", sb.toString());
}
//-----------------------------------------------------------------------
@ -214,6 +217,9 @@ public class StrBuilderAppendInsertTest extends TestCase {
sb.append("abcbardef", 3, 3);
assertEquals("foobar", sb.toString());
sb.append( (CharSequence)"abcbardef", 4, 3);
assertEquals("foobarard", sb.toString());
}
//-----------------------------------------------------------------------