You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2005/08/14 23:56:03 UTC
svn commit: r232656 - in /jakarta/commons/proper/lang/trunk/src:
java/org/apache/commons/lang/text/StrBuilder.java
test/org/apache/commons/lang/text/StrBuilderTest.java
Author: scolebourne
Date: Sun Aug 14 14:55:58 2005
New Revision: 232656
URL: http://svn.apache.org/viewcvs?rev=232656&view=rev
Log:
Increase the number of methods that can chain
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java?rev=232656&r1=232655&r2=232656&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java Sun Aug 14 14:55:58 2005
@@ -125,12 +125,14 @@
* Sets the text to be appended when null is added.
*
* @param str the null text, null means no append
+ * @return this, to enable chaining
*/
- public void setNullText(String str) {
+ public StrBuilder setNullText(String str) {
if (str != null && str.length() == 0) {
str = null;
}
nullText = str;
+ return this;
}
//-----------------------------------------------------------------------
@@ -148,9 +150,10 @@
* or adding filler of unicode zero.
*
* @param length the length to set to, must be zero or positive
+ * @return this, to enable chaining
* @throws IndexOutOfBoundsException if the length is negative
*/
- public void setLength(int length) {
+ public StrBuilder setLength(int length) {
if (length < 0) {
throw new StringIndexOutOfBoundsException(length);
}
@@ -165,6 +168,7 @@
buffer[i] = '\0';
}
}
+ return this;
}
//-----------------------------------------------------------------------
@@ -181,24 +185,29 @@
* Checks the capacity and ensures that it is at least the size specified.
*
* @param capacity the capacity to ensure
+ * @return this, to enable chaining
*/
- public void ensureCapacity(int capacity) {
+ public StrBuilder ensureCapacity(int capacity) {
if (capacity > buffer.length) {
char[] old = buffer;
buffer = new char[capacity];
System.arraycopy(old, 0, buffer, 0, size);
}
+ return this;
}
/**
* Minimizes the capacity to the actual length of the string.
+ *
+ * @return this, to enable chaining
*/
- public void minimizeCapacity() {
+ public StrBuilder minimizeCapacity() {
if (buffer.length > length()) {
char[] old = buffer;
buffer = new char[length()];
System.arraycopy(old, 0, buffer, 0, size);
}
+ return this;
}
//-----------------------------------------------------------------------
@@ -219,6 +228,7 @@
* <p>
* This method is the same as checking {@link #length()} and is provided to match the
* API of Collections.
+ *
* @return <code>true</code> if the size is <code>0</code>.
*/
public boolean isEmpty() {
@@ -231,11 +241,14 @@
* This method does not reduce the size of the internal character buffer.
* To do that, call <code>clear()</code> followed by {@link #minimizeCapacity()}.
* <p>
- * This method is the same as {@link #setLength(int)} and is provided to match the
- * API of Collections.
+ * This method is the same as {@link #setLength(int)} called with zero
+ * and is provided to match the API of Collections.
+ *
+ * @return this, to enable chaining
*/
- public void clear() {
+ public StrBuilder clear() {
size = 0;
+ return this;
}
//-----------------------------------------------------------------------
@@ -262,13 +275,15 @@
* @see #deleteCharAt(int)
* @param index the index to set
* @param ch the new character
+ * @return this, to enable chaining
* @throws IndexOutOfBoundsException if the index is invalid
*/
- public void setCharAt(int index, char ch) {
+ public StrBuilder setCharAt(int index, char ch) {
if (index < 0 || index >= length()) {
throw new StringIndexOutOfBoundsException(index);
}
buffer[index] = ch;
+ return this;
}
/**
Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java?rev=232656&r1=232655&r2=232656&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java (original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java Sun Aug 14 14:55:58 2005
@@ -109,6 +109,18 @@
}
//-----------------------------------------------------------------------
+ public void testChaining() {
+ StrBuilder sb = new StrBuilder();
+ assertSame(sb, sb.setNullText(null));
+ assertSame(sb, sb.setLength(1));
+ assertSame(sb, sb.setCharAt(0, 'a'));
+ assertSame(sb, sb.ensureCapacity(0));
+ assertSame(sb, sb.minimizeCapacity());
+ assertSame(sb, sb.clear());
+ assertSame(sb, sb.reverse());
+ }
+
+ //-----------------------------------------------------------------------
public void testCapacityAndLength() {
StrBuilder sb = new StrBuilder();
assertEquals(32, sb.capacity());
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org