This commit is contained in:
Shekhar Gulati
2016-07-25 15:29:43 +05:30
parent 6bbe0a9167
commit 3594042ab5
2 changed files with 52 additions and 3 deletions
+22
View File
@@ -1109,6 +1109,28 @@ public abstract class Strman {
return value.chars().mapToObj(ch -> leftPad(Integer.toString(ch, radix), "0", digits)).collect(joining());
}
/**
* Join concatenates all the elements of the strings array into a single String. The separator string is placed between elements in the resulting string.
*
* @param strings The input array to concatenate
* @param separator The separator to use
* @return Concatenated String
*/
public static String join(final String[] strings, final String separator) {
if (strings == null) {
throw new IllegalArgumentException("Input array 'strings' can't be null");
}
if (separator == null) {
throw new IllegalArgumentException("separator can't be null");
}
StringJoiner joiner = new StringJoiner(separator);
for (String el : strings) {
joiner.add(el);
}
return joiner.toString();
}
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
if (predicate.test(value)) {
throw new IllegalArgumentException(supplier.get());
+30 -3
View File
@@ -411,9 +411,9 @@ public class StrmanTest {
@Test
public void inequal_shouldTestInequalityOfStrings() throws Exception {
assertThat(inequal("a", "b"), equalTo(true));
assertThat(inequal("a", "a"), equalTo(false));
assertThat(inequal("0", "1"), equalTo(true));
assertThat(unequal("a", "b"), equalTo(true));
assertThat(unequal("a", "a"), equalTo(false));
assertThat(unequal("0", "1"), equalTo(true));
}
@Test
@@ -885,4 +885,31 @@ public class StrmanTest {
assertThat(String.format("%s should be foo_bar", el), toSnakeCase(el), is(equalTo("foo_bar"))));
}
@Test
public void join_shouldJoinArrayOfStringIntoASingleString() throws Exception {
String[] strings = {
"hello",
"world",
"123"
};
assertThat(join(strings, ";"), is(equalTo("hello;world;123")));
}
@Test(expected = IllegalArgumentException.class)
public void join_shouldThrowIllegalArgumentExceptionWhenSeparatorIsNull() throws Exception {
String[] strings = {
"hello",
"world",
"123"
};
join(strings, null);
}
@Test
public void join_shouldReturnEmptyStringWhenInputArrayIsEmpty() throws Exception {
String[] emptyArray = {};
assertThat(join(emptyArray, ","), is(equalTo("")));
}
}