diff --git a/src/java/org/apache/commons/lang/text/StrBuilder.java b/src/java/org/apache/commons/lang/text/StrBuilder.java index 79cffa79a..b8894d495 100644 --- a/src/java/org/apache/commons/lang/text/StrBuilder.java +++ b/src/java/org/apache/commons/lang/text/StrBuilder.java @@ -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()}. diff --git a/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java b/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java index bff5889e1..1b9386e7b 100644 --- a/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java +++ b/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java @@ -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()); } //-----------------------------------------------------------------------