Add appendNewLine()
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@234016 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
04e1537d3c
commit
a751d82424
|
@ -22,6 +22,7 @@ import java.util.Collection;
|
|||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
|
||||
/**
|
||||
* Builds a string from consituant parts providing a more flexible and powerful API
|
||||
|
@ -71,6 +72,8 @@ public class StrBuilder implements Cloneable {
|
|||
protected char[] buffer;
|
||||
/** Current size of the buffer. */
|
||||
protected int size;
|
||||
/** The new line. */
|
||||
private String newLine;
|
||||
/** The null text. */
|
||||
private String nullText;
|
||||
|
||||
|
@ -111,6 +114,27 @@ public class StrBuilder implements Cloneable {
|
|||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Gets the text to be appended when a new line is added.
|
||||
*
|
||||
* @return the new line text, null means use system default
|
||||
*/
|
||||
public String getNewLineText() {
|
||||
return newLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the text to be appended when a new line is added.
|
||||
*
|
||||
* @param newLine the new line text, null means use system default
|
||||
* @return this, to enable chaining
|
||||
*/
|
||||
public StrBuilder setNewLineText(String newLine) {
|
||||
this.newLine = newLine;
|
||||
return this;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Gets the text to be appended when null is added.
|
||||
|
@ -124,14 +148,14 @@ public class StrBuilder implements Cloneable {
|
|||
/**
|
||||
* Sets the text to be appended when null is added.
|
||||
*
|
||||
* @param str the null text, null means no append
|
||||
* @param nullText the null text, null means no append
|
||||
* @return this, to enable chaining
|
||||
*/
|
||||
public StrBuilder setNullText(String str) {
|
||||
if (str != null && str.length() == 0) {
|
||||
str = null;
|
||||
public StrBuilder setNullText(String nullText) {
|
||||
if (nullText != null && nullText.length() == 0) {
|
||||
nullText = null;
|
||||
}
|
||||
nullText = str;
|
||||
this.nullText = nullText;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -378,6 +402,23 @@ public class StrBuilder implements Cloneable {
|
|||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Appends the new line string to this string builder.
|
||||
* <p>
|
||||
* The new line string can be altered using {@link #setNewLineText(String)}.
|
||||
* This might be used to force the output to always use Unix line endings
|
||||
* even when on Windows.
|
||||
*
|
||||
* @return this, to enable chaining
|
||||
*/
|
||||
public StrBuilder appendNewLine() {
|
||||
if (newLine == null) {
|
||||
append(SystemUtils.LINE_SEPARATOR);
|
||||
return this;
|
||||
}
|
||||
return append(newLine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the text representing <code>null</code> to this string builder.
|
||||
*
|
||||
|
|
|
@ -21,6 +21,8 @@ import java.util.Collection;
|
|||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
@ -69,6 +71,17 @@ public class StrBuilderAppendInsertTest extends TestCase {
|
|||
super(name);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testAppendNewLine() {
|
||||
StrBuilder sb = new StrBuilder("---");
|
||||
sb.appendNewLine().append("+++");
|
||||
assertEquals("---" + SystemUtils.LINE_SEPARATOR + "+++", sb.toString());
|
||||
|
||||
sb = new StrBuilder("---");
|
||||
sb.setNewLineText("#").appendNewLine().setNewLineText(null).appendNewLine();
|
||||
assertEquals("---#" + SystemUtils.LINE_SEPARATOR, sb.toString());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testAppendWithNullText() {
|
||||
StrBuilder sb = new StrBuilder();
|
||||
|
|
|
@ -111,6 +111,7 @@ public class StrBuilderTest extends TestCase {
|
|||
//-----------------------------------------------------------------------
|
||||
public void testChaining() {
|
||||
StrBuilder sb = new StrBuilder();
|
||||
assertSame(sb, sb.setNewLineText(null));
|
||||
assertSame(sb, sb.setNullText(null));
|
||||
assertSame(sb, sb.setLength(1));
|
||||
assertSame(sb, sb.setCharAt(0, 'a'));
|
||||
|
@ -120,6 +121,39 @@ public class StrBuilderTest extends TestCase {
|
|||
assertSame(sb, sb.reverse());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testGetSetNewLineText() {
|
||||
StrBuilder sb = new StrBuilder();
|
||||
assertEquals(null, sb.getNewLineText());
|
||||
|
||||
sb.setNewLineText("#");
|
||||
assertEquals("#", sb.getNewLineText());
|
||||
|
||||
sb.setNewLineText("");
|
||||
assertEquals("", sb.getNewLineText());
|
||||
|
||||
sb.setNewLineText((String) null);
|
||||
assertEquals(null, sb.getNewLineText());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testGetSetNullText() {
|
||||
StrBuilder sb = new StrBuilder();
|
||||
assertEquals(null, sb.getNullText());
|
||||
|
||||
sb.setNullText("null");
|
||||
assertEquals("null", sb.getNullText());
|
||||
|
||||
sb.setNullText("");
|
||||
assertEquals(null, sb.getNullText());
|
||||
|
||||
sb.setNullText("NULL");
|
||||
assertEquals("NULL", sb.getNullText());
|
||||
|
||||
sb.setNullText((String) null);
|
||||
assertEquals(null, sb.getNullText());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testCapacityAndLength() {
|
||||
StrBuilder sb = new StrBuilder();
|
||||
|
@ -480,24 +514,6 @@ public class StrBuilderTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testNullText() {
|
||||
StrBuilder sb = new StrBuilder();
|
||||
assertEquals(null, sb.getNullText());
|
||||
|
||||
sb.setNullText("null");
|
||||
assertEquals("null", sb.getNullText());
|
||||
|
||||
sb.setNullText("");
|
||||
assertEquals(null, sb.getNullText());
|
||||
|
||||
sb.setNullText("NULL");
|
||||
assertEquals("NULL", sb.getNullText());
|
||||
|
||||
sb.setNullText((String) null);
|
||||
assertEquals(null, sb.getNullText());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testDeleteIntInt() {
|
||||
StrBuilder sb = new StrBuilder("abc");
|
||||
|
|
Loading…
Reference in New Issue