Merge branch 'LANG-701'

LANG-701: StringUtils join with var args. Thanks to James Sawle.
This commit is contained in:
Benedikt Ritter 2015-05-05 20:32:39 +02:00
commit 7fae5b0b17
3 changed files with 59 additions and 1 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-701" type="add" dev="britter" due-to="James Sawle">StringUtils join with var args</action>
<action issue="LANG-1130" type="fix" dev="britter">Fix critical issues reported by SonarQube</action>
<action issue="LANG-1131" type="fix" dev="britter">StrBuilder.equals(StrBuilder) doesn't check for null inputs</action>
<action issue="LANG-1105" type="add" dev="britter" due-to="Hendrik Saly">Add ThreadUtils - A utility class which provides helper methods related to java.lang.Thread</action>

View File

@ -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 class StringUtils {
return join(iterable.iterator(), separator);
}
/**
* <p>Joins the elements of the provided varargs into a
* single String containing the provided elements.</p>
*
* <p>No delimiter is added before or after the list.
* {@code null} elements and separator are treated as empty Strings ("").</p>
*
* <pre>
* 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"
* </pre>
*
* @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<Object> iterator = Arrays.asList(objects).iterator();
while (iterator.hasNext()) {
result.append(ObjectUtils.toString(iterator.next()));
if (iterator.hasNext()) {
result.append(sanitizedSeparator);
}
}
return result.toString();
}
// Delete
//-----------------------------------------------------------------------
/**

View File

@ -460,6 +460,24 @@ public class StringUtilsTest {
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));