From 4da8084e451787cafed959016aec814127f0c7cd Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Fri, 1 Aug 2003 21:02:16 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang/StringUtils.java | 30 ++++++++++++++++--- .../apache/commons/lang/StringUtilsTest.java | 14 +++++++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index 08840bce9..497376fc3 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -144,7 +144,7 @@ * @author Gary Gregory * @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.

* - *

The difference from join is that concatenate has no delimiter -- i.e.,
- * StringUtils.concatenate(array) = StringUtils.join(array, null).

- * *
      * 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, null 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);
     }
     
+    /**
+     * 

Joins the elements of the provided array into a single String + * containing the provided list of elements.

+ * + *

No separator is added to the joined String. + * Null objects or empty strings within the array are represented by + * empty strings.

+ * + *
+     * StringUtils.join(null)            = null
+     * StringUtils.join([])              = ""
+     * StringUtils.join([null])          = ""
+     * StringUtils.join(["a", "b", "c"]) = "abc"
+     * StringUtils.join([null, "", "a"]) = "a"
+     * 
+ * + * @param array the array of values to join together, may be null + * @return the joined String, null if null array input + */ + public static String join(Object[] array) { + return join(array, null); + } + /** *

Joins the elements of the provided array into a single String * containing the provided list of elements.

diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index 7bb36e519..d191198f6 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -74,7 +74,7 @@ * @author Holger Krauth * @author Henning P. Schmiedehausen * @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));