Add join(Object[]) as a replacement for concatenate

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137549 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-08-01 21:02:16 +00:00
parent a2bb66002d
commit 4da8084e45
2 changed files with 38 additions and 6 deletions

View File

@ -144,7 +144,7 @@
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author Phil Steitz
* @since 1.0
* @version $Id: StringUtils.java,v 1.84 2003/08/01 20:45:17 scolebourne Exp $
* @version $Id: StringUtils.java,v 1.85 2003/08/01 21:02:16 scolebourne Exp $
*/
public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -1909,9 +1909,6 @@ public static String[] split(String str, String separatorChars, int max) {
* Null objects or empty strings within the array are represented by
* empty strings.</p>
*
* <p>The difference from join is that concatenate has no delimiter -- i.e., <br>
* <code>StringUtils.concatenate(array) = StringUtils.join(array, null)</code>.</p>
*
* <pre>
* StringUtils.concatenate(null) = null
* StringUtils.concatenate([]) = ""
@ -1922,11 +1919,36 @@ public static String[] split(String str, String separatorChars, int max) {
*
* @param array the array of values to concatenate, may be null
* @return the concatenated String, <code>null</code> if null array input
* @deprecated Use the better named {@link #join(Object[])} instead.
* Method will be removed in Commons Lang 3.0.
*/
public static String concatenate(Object[] array) {
return join(array, null);
}
/**
* <p>Joins the elements of the provided array into a single String
* containing the provided list of elements.</p>
*
* <p>No separator is added to the joined String.
* Null objects or empty strings within the array are represented by
* empty strings.</p>
*
* <pre>
* StringUtils.join(null) = null
* StringUtils.join([]) = ""
* StringUtils.join([null]) = ""
* StringUtils.join(["a", "b", "c"]) = "abc"
* StringUtils.join([null, "", "a"]) = "a"
* </pre>
*
* @param array the array of values to join together, may be null
* @return the joined String, <code>null</code> if null array input
*/
public static String join(Object[] array) {
return join(array, null);
}
/**
* <p>Joins the elements of the provided array into a single String
* containing the provided list of elements.</p>

View File

@ -74,7 +74,7 @@
* @author Holger Krauth
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author Phil Steitz
* @version $Id: StringUtilsTest.java,v 1.41 2003/07/31 20:38:26 scolebourne Exp $
* @version $Id: StringUtilsTest.java,v 1.42 2003/08/01 21:02:16 scolebourne Exp $
*/
public class StringUtilsTest extends TestCase {
@ -205,6 +205,16 @@ public void testCaseFunctions() {
"Hello aPACHE", StringUtils.swapCase("hELLO Apache") );
}
public void testJoin_Objectarray() {
assertEquals(null, StringUtils.join(null));
assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST));
assertEquals("", StringUtils.join(NULL_ARRAY_LIST));
assertEquals("abc", StringUtils.join(new String[] {"a", "b", "c"}));
assertEquals("a", StringUtils.join(new String[] {null, "a", ""}));
assertEquals("foo", StringUtils.join(MIXED_ARRAY_LIST));
assertEquals("foo2", StringUtils.join(MIXED_TYPE_LIST));
}
public void testJoin_ArrayChar() {
assertEquals(null, StringUtils.join((Object[]) null, ','));
assertEquals(TEXT_LIST_CHAR, StringUtils.join(ARRAY_LIST, SEPARATOR_CHAR));
@ -250,7 +260,7 @@ public void testJoin_IteratorString() {
assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR));
}
public void testConcatenate_Array() {
public void testConcatenate_Objectarray() {
assertEquals(null, StringUtils.concatenate(null));
assertEquals("", StringUtils.concatenate(EMPTY_ARRAY_LIST));
assertEquals("", StringUtils.concatenate(NULL_ARRAY_LIST));