You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/07/19 21:30:40 UTC
svn commit: r795600 - in /commons/proper/lang/trunk/src:
java/org/apache/commons/lang/text/StrBuilder.java
test/org/apache/commons/lang/text/StrBuilderTest.java
Author: bayard
Date: Sun Jul 19 19:30:40 2009
New Revision: 795600
URL: http://svn.apache.org/viewvc?rev=795600&view=rev
Log:
Applying Arnaud Brunet's patch from LANG-355 to make StrBuilder implement CharSequence
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java?rev=795600&r1=795599&r2=795600&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java Sun Jul 19 19:30:40 2009
@@ -71,7 +71,7 @@
* @since 2.2
* @version $Id$
*/
-public class StrBuilder {
+public class StrBuilder implements CharSequence {
/**
* The extra capacity for new builders.
@@ -1918,6 +1918,22 @@
//-----------------------------------------------------------------------
/**
+ * {@inheritDoc}
+ */
+ public CharSequence subSequence(int startIndex, int endIndex) {
+ if (startIndex < 0) {
+ throw new StringIndexOutOfBoundsException(startIndex);
+ }
+ if (endIndex > size) {
+ throw new StringIndexOutOfBoundsException(endIndex);
+ }
+ if (startIndex > endIndex) {
+ throw new StringIndexOutOfBoundsException(endIndex - startIndex);
+ }
+ return substring(startIndex, endIndex);
+ }
+
+ /**
* Extracts a portion of this string builder as a string.
*
* @param start the start index, inclusive, must be valid
Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java?rev=795600&r1=795599&r2=795600&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java Sun Jul 19 19:30:40 2009
@@ -1099,6 +1099,39 @@
}
//-----------------------------------------------------------------------
+ public void testSubSequenceIntInt() {
+ StrBuilder sb = new StrBuilder ("hello goodbye");
+ // Start index is negative
+ try {
+ sb.subSequence(-1, 5);
+ fail();
+ } catch (IndexOutOfBoundsException e) {}
+
+ // End index is negative
+ try {
+ sb.subSequence(2, -1);
+ fail();
+ } catch (IndexOutOfBoundsException e) {}
+
+ // End index greater than length()
+ try {
+ sb.subSequence(2, sb.length() + 1);
+ fail();
+ } catch (IndexOutOfBoundsException e) {}
+
+ // Start index greater then end index
+ try {
+ sb.subSequence(3, 2);
+ fail();
+ } catch (IndexOutOfBoundsException e) {}
+
+ // Normal cases
+ assertEquals ("hello", sb.subSequence(0, 5));
+ assertEquals ("hello goodbye".subSequence(0, 6), sb.subSequence(0, 6));
+ assertEquals ("goodbye", sb.subSequence(6, 13));
+ assertEquals ("hello goodbye".subSequence(6,13), sb.subSequence(6, 13));
+ }
+
public void testSubstringInt() {
StrBuilder sb = new StrBuilder ("hello goodbye");
assertEquals ("goodbye", sb.substring(6));