From eac9add08144b127826a5f35c3d4270845744c19 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Mon, 8 Aug 2005 22:03:31 +0000 Subject: [PATCH] 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 --- .../apache/commons/lang/text/StrBuilder.java | 68 +++++++- .../commons/lang/text/StrBuilderTest.java | 148 ++++++++++++++++++ 2 files changed, 210 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/commons/lang/text/StrBuilder.java b/src/java/org/apache/commons/lang/text/StrBuilder.java index a0c8fe581..a689c19cb 100644 --- a/src/java/org/apache/commons/lang/text/StrBuilder.java +++ b/src/java/org/apache/commons/lang/text/StrBuilder.java @@ -342,7 +342,7 @@ public class StrBuilder implements Cloneable { //----------------------------------------------------------------------- /** - * Appends the text representing null to the string builder. + * Appends the text representing null 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. diff --git a/src/test/org/apache/commons/lang/text/StrBuilderTest.java b/src/test/org/apache/commons/lang/text/StrBuilderTest.java index df4c33f1c..b4545e790 100644 --- a/src/test/org/apache/commons/lang/text/StrBuilderTest.java +++ b/src/test/org/apache/commons/lang/text/StrBuilderTest.java @@ -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();