commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r926448 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/StringUtils.java test/java/org/apache/commons/lang3/StringUtilsTest.java
Date Tue, 23 Mar 2010 02:56:18 GMT
Author: bayard
Date: Tue Mar 23 02:56:18 2010
New Revision: 926448

URL: http://svn.apache.org/viewvc?rev=926448&view=rev
Log:
Adding concat(Object...) and concatWith(String, Object...) methods to provide vararg'd versions
of the more prominent join methods. This ties into the String.concat method. LANG-396

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=926448&r1=926447&r2=926448&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Tue
Mar 23 02:56:18 2010
@@ -3076,6 +3076,46 @@ public class StringUtils {
     // Joining
     //-----------------------------------------------------------------------
     /**
+     * <p>Joins the provided elements into a single String. </p>
+     *
+     * <p>No separator is added to the joined String.
+     * Null objects or empty string elements are represented by
+     * empty strings.</p>
+     *
+     * <pre>
+     * StringUtils.concat("a", "b", "c") = "abc"
+     * StringUtils.concat(null, "", "a") = "a"
+     * </pre>
+     *
+     * @param elements  the values to join together
+     * @return the concatenated String
+     * @since 3.0
+     */
+    public static String concat(Object... elements) {
+        return join(elements, null);
+    }
+
+    /**
+     * <p>Joins the provided elements into a single String. </p>
+     *
+     * <p>No separator is added to the joined String.
+     * Null objects or empty string elements are represented by
+     * empty strings.</p>
+     *
+     * <pre>
+     * StringUtils.concatWith(".", "a", "b", "c") = "a.b.c"
+     * StringUtils.concatWith("", null, "", "a") = "a"
+     * </pre>
+     *
+     * @param elements  the values to join together
+     * @return the concatenated String
+     * @since 3.0
+     */
+    public static String concatWith(String separator, Object... elements) {
+        return join(elements, separator);
+    }
+
+    /**
      * <p>Joins the elements of the provided array into a single String
      * containing the provided list of elements.</p>
      *

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java?rev=926448&r1=926447&r2=926448&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Tue Mar 23 02:56:18 2010
@@ -188,6 +188,20 @@ public class StringUtilsTest extends Tes
     }
 
     //-----------------------------------------------------------------------
+    public void testConcat_Objects() {
+        assertEquals("abc", StringUtils.concat("a", "b", "c"));
+        assertEquals("a",   StringUtils.concat(null, "", "a"));
+        assertEquals(null,  StringUtils.concat(null));
+    }
+
+    public void testConcatWith_StringObjects() {
+        assertEquals("a.b.c", StringUtils.concatWith(".", "a", "b", "c"));
+        assertEquals("a...b...c", StringUtils.concatWith("...", "a", "b", "c"));
+        assertEquals("a",     StringUtils.concatWith("", null, "", "a"));
+        assertEquals("a",     StringUtils.concatWith(null, null, "", "a"));
+        assertEquals(null,    StringUtils.concatWith(null, null));
+    }
+
     public void testJoin_Objectarray() {
         assertEquals(null, StringUtils.join(null));
         assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST));



Mime
View raw message