LANG-701 - Added new joinWith method to support varargs
This commit is contained in:
parent
1cb5573ada
commit
155cec1085
|
@ -17,7 +17,6 @@
|
||||||
package org.apache.commons.lang3;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.text.Normalizer;
|
import java.text.Normalizer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -4179,6 +4178,46 @@ public static String join(final Iterable<?> iterable, final String separator) {
|
||||||
return join(iterable.iterator(), separator);
|
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
|
// Delete
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -460,6 +460,24 @@ public void testJoin_IterableString() {
|
||||||
assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST), SEPARATOR));
|
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
|
@Test
|
||||||
public void testSplit_String() {
|
public void testSplit_String() {
|
||||||
assertNull(StringUtils.split(null));
|
assertNull(StringUtils.split(null));
|
||||||
|
|
Loading…
Reference in New Issue