mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-09 03:24:58 +00:00
[LANG-825] Create StrBuilder APIs similar to String.format(String, Object...)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1389042 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
72b9613ddb
commit
01ee7028e6
@ -22,6 +22,7 @@
|
||||
<body>
|
||||
|
||||
<release version="3.2" date="TBA" description="Next release">
|
||||
<action issue="LANG-825" type="add">Create StrBuilder APIs similar to String.format(String, Object...)</action>
|
||||
<action issue="LANG-817" type="fix">Add org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS_8</action>
|
||||
<action issue="LANG-813" type="fix">StringUtils.equalsIgnoreCase doesn't check string reference equality</action>
|
||||
<action issue="LANG-810" type="fix">StringUtils.join() endIndex, bugged for loop</action>
|
||||
|
@ -547,6 +547,19 @@ public StrBuilder append(String str, int startIndex, int length) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls {@link String#format(String, Object...)} and appends the result.
|
||||
*
|
||||
* @param format the format string
|
||||
* @param objs the objects to use in the format string
|
||||
* @return {@code this} to enable chaining
|
||||
* @see String#format(String, Object...)
|
||||
* @since 3.2
|
||||
*/
|
||||
public StrBuilder append(String format, Object... objs) {
|
||||
return append(String.format(format, objs));
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string buffer to this string builder.
|
||||
* Appending null will call {@link #appendNull()}.
|
||||
@ -812,6 +825,19 @@ public StrBuilder appendln(String str, int startIndex, int length) {
|
||||
return append(str, startIndex, length).appendNewLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls {@link String#format(String, Object...)} and appends the result.
|
||||
*
|
||||
* @param format the format string
|
||||
* @param objs the objects to use in the format string
|
||||
* @return {@code this} to enable chaining
|
||||
* @see String#format(String, Object...)
|
||||
* @since 3.2
|
||||
*/
|
||||
public StrBuilder appendln(String format, Object... objs) {
|
||||
return append(format, objs).appendNewLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string buffer followed by a new line to this string builder.
|
||||
* Appending null will call {@link #appendNull()}.
|
||||
|
@ -457,6 +457,28 @@ public void testAppend_PrimitiveNumber() {
|
||||
assertEquals("012.34.5", sb.toString());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testAppendln_FormattedString() {
|
||||
final int[] count = new int[2];
|
||||
StrBuilder sb = new StrBuilder() {
|
||||
@Override
|
||||
public StrBuilder append(String str) {
|
||||
count[0]++;
|
||||
return super.append(str);
|
||||
}
|
||||
@Override
|
||||
public StrBuilder appendNewLine() {
|
||||
count[1]++;
|
||||
return super.appendNewLine();
|
||||
}
|
||||
};
|
||||
sb.appendln("Hello %s", "Alice");
|
||||
assertEquals("Hello Alice" + SEP, sb.toString());
|
||||
assertEquals(2, count[0]); // appendNewLine() calls append(String)
|
||||
assertEquals(1, count[1]);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testAppendln_Object() {
|
||||
@ -855,6 +877,28 @@ public void testAppendFixedWidthPadRight_int() {
|
||||
assertEquals("123-------", sb.toString());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testAppend_FormattedString() {
|
||||
StrBuilder sb;
|
||||
|
||||
sb = new StrBuilder();
|
||||
sb.append("Hi", (Object[]) null);
|
||||
assertEquals("Hi", sb.toString());
|
||||
|
||||
sb = new StrBuilder();
|
||||
sb.append("Hi", "Alice");
|
||||
assertEquals("Hi", sb.toString());
|
||||
|
||||
sb = new StrBuilder();
|
||||
sb.append("Hi %s", "Alice");
|
||||
assertEquals("Hi Alice", sb.toString());
|
||||
|
||||
sb = new StrBuilder();
|
||||
sb.append("Hi %s %,d", "Alice", 5000);
|
||||
assertEquals("Hi Alice 5,000", sb.toString());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testAppendAll_Array() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user