From 155cec1085f9bd5383241e4c93fb40117081aaba Mon Sep 17 00:00:00 2001 From: jamessawle Date: Mon, 4 May 2015 22:19:18 +0100 Subject: [PATCH] LANG-701 - Added new joinWith method to support varargs --- .../org/apache/commons/lang3/StringUtils.java | 41 ++++++++++++++++++- .../apache/commons/lang3/StringUtilsTest.java | 18 ++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 279c472b9..5ebaddecf 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -17,7 +17,6 @@ package org.apache.commons.lang3; import java.io.UnsupportedEncodingException; - import java.nio.charset.Charset; import java.text.Normalizer; import java.util.ArrayList; @@ -4179,6 +4178,46 @@ public static String join(final Iterable iterable, final String separator) { return join(iterable.iterator(), separator); } + /** + *

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

+ * + *

No delimiter is added before or after the list. + * {@code null} elements and separator are treated as empty Strings ("").

+ * + *
+     * StringUtils.joinWith(",", {"a", "b"})        = "a,b"
+     * StringUtils.joinWith(",", {"a", "b",""})     = "a,b,"
+     * StringUtils.joinWith(",", {"a", null, "b"})  = "a,,b"
+     * StringUtils.joinWith(null, {"a", "b"})       = "ab"
+     * 
+ * + * @param separator the separator character to use, null treated as "" + * @param objects the varargs providing the values to join together. {@code null} elements are treated as "" + * @return the joined String. + * @throws java.lang.IllegalArgumentException if a null varargs is provided + */ + public static String joinWith(final String separator, Object... objects) { + if (objects == null) { + throw new IllegalArgumentException("Object varargs must not be null"); + } + + String sanitizedSeparator = defaultString(separator, StringUtils.EMPTY); + + StringBuilder result = new StringBuilder(); + + Iterator iterator = Arrays.asList(objects).iterator(); + while (iterator.hasNext()) { + result.append(ObjectUtils.toString(iterator.next())); + + if (iterator.hasNext()) { + result.append(sanitizedSeparator); + } + } + + return result.toString(); + } + // Delete //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index f11ec703d..44200bd92 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -460,6 +460,24 @@ public void testJoin_IterableString() { assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST), SEPARATOR)); } + @Test + public void testJoinWith() { + assertEquals("", StringUtils.joinWith(",", new Object[0])); // empty array + assertEquals("", StringUtils.joinWith(",", NULL_ARRAY_LIST)); + assertEquals("null", StringUtils.joinWith(",", NULL_TO_STRING_LIST)); //toString method prints 'null' + + assertEquals("a,b,c", StringUtils.joinWith(",", new String[]{"a", "b", "c"})); + assertEquals(",a,", StringUtils.joinWith(",", new String[]{null, "a", ""})); + + assertEquals("ab", StringUtils.joinWith(null, "a", "b")); + } + + @Test(expected = IllegalArgumentException.class) + public void testJoinWithThrowsException() { + StringUtils.joinWith(",", null); + } + + @Test public void testSplit_String() { assertNull(StringUtils.split(null));