StrBuilder implements Builder; provide toStringBuilder as toStringBuffer
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1407534 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fd5af17932
commit
1ea6e4471c
|
@ -25,6 +25,7 @@ import java.util.List;
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.apache.commons.lang3.SystemUtils;
|
import org.apache.commons.lang3.SystemUtils;
|
||||||
|
import org.apache.commons.lang3.builder.Builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a string from constituent parts providing a more flexible and powerful API
|
* Builds a string from constituent parts providing a more flexible and powerful API
|
||||||
|
@ -71,7 +72,7 @@ import org.apache.commons.lang3.SystemUtils;
|
||||||
* @since 2.2
|
* @since 2.2
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class StrBuilder implements CharSequence, Appendable, Serializable {
|
public class StrBuilder implements CharSequence, Appendable, Serializable, Builder<String> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The extra capacity for new builders.
|
* The extra capacity for new builders.
|
||||||
|
@ -2661,6 +2662,27 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
|
||||||
return new StringBuffer(size).append(buffer, 0, size);
|
return new StringBuffer(size).append(buffer, 0, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a StringBuilder version of the string builder, creating a
|
||||||
|
* new instance each time the method is called.
|
||||||
|
*
|
||||||
|
* @return the builder as a StringBuilder
|
||||||
|
* @since Apache Commons Lang 3.2
|
||||||
|
*/
|
||||||
|
public StringBuilder toStringBuilder() {
|
||||||
|
return new StringBuilder(size).append(buffer, 0, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implement the {@link Builder} interface.
|
||||||
|
* @return the builder as a String
|
||||||
|
* @since Apache Commons Lang 3.2
|
||||||
|
* @see #toString()
|
||||||
|
*/
|
||||||
|
public String build() {
|
||||||
|
return toString();
|
||||||
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Validates parameters defining a range of the builder.
|
* Validates parameters defining a range of the builder.
|
||||||
|
|
|
@ -916,6 +916,10 @@ public class StrBuilderAppendInsertTest {
|
||||||
sb.clear();
|
sb.clear();
|
||||||
sb.appendAll(new Object[]{"foo", "bar", "baz"});
|
sb.appendAll(new Object[]{"foo", "bar", "baz"});
|
||||||
assertEquals("foobarbaz", sb.toString());
|
assertEquals("foobarbaz", sb.toString());
|
||||||
|
|
||||||
|
sb.clear();
|
||||||
|
sb.appendAll("foo", "bar", "baz");
|
||||||
|
assertEquals("foobarbaz", sb.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
|
|
@ -1800,6 +1800,16 @@ public class StrBuilderTest {
|
||||||
assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString());
|
assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
@Test
|
||||||
|
public void testToStringBuilder() {
|
||||||
|
StrBuilder sb = new StrBuilder();
|
||||||
|
assertEquals(new StringBuilder().toString(), sb.toStringBuilder().toString());
|
||||||
|
|
||||||
|
sb.append("junit");
|
||||||
|
assertEquals(new StringBuilder("junit").toString(), sb.toStringBuilder().toString());
|
||||||
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
@Test
|
@Test
|
||||||
public void testLang294() {
|
public void testLang294() {
|
||||||
|
@ -1839,4 +1849,10 @@ public class StrBuilderTest {
|
||||||
assertEquals( "Failed to invoke appendFixedWidthPadLeft correctly", "**********", sb.toString());
|
assertEquals( "Failed to invoke appendFixedWidthPadLeft correctly", "**********", sb.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAsBuilder() {
|
||||||
|
StrBuilder sb = new StrBuilder().appendAll("Lorem", " ", "ipsum", " ", "dolor");
|
||||||
|
assertEquals(sb.toString(), sb.build());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue