Resolved #54 and Resolved #59

This commit is contained in:
Shekhar Gulati
2016-07-27 23:19:41 +05:30
parent 3594042ab5
commit f282dddca3
2 changed files with 51 additions and 1 deletions
+34 -1
View File
@@ -1117,7 +1117,7 @@ public abstract class Strman {
* @param separator The separator to use
* @return Concatenated String
*/
public static String join(final String[] strings, final String separator) {
public static String join(final String[] strings, final String separator) throws IllegalArgumentException {
if (strings == null) {
throw new IllegalArgumentException("Input array 'strings' can't be null");
}
@@ -1131,6 +1131,39 @@ public abstract class Strman {
return joiner.toString();
}
/**
* Converts the first character of string to upper case and the remaining to lower case.
*
* @param input The string to capitalize
* @return The capitalized string
*/
public static String capitalize(final String input) throws IllegalArgumentException {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
if (input.length() == 0) {
return "";
}
return head(input).toUpperCase() + tail(input).toLowerCase();
}
/**
* Converts the first character of string to lower case.
*
* @param input The string to convert
* @return The converted string
* @throws IllegalArgumentException
*/
public static String lowerFirst(final String input) throws IllegalArgumentException {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
if (input.length() == 0) {
return "";
}
return head(input).toLowerCase() + tail(input);
}
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
if (predicate.test(value)) {
throw new IllegalArgumentException(supplier.get());
+17
View File
@@ -912,4 +912,21 @@ public class StrmanTest {
String[] emptyArray = {};
assertThat(join(emptyArray, ","), is(equalTo("")));
}
@Test
public void capitalize_shouldCapitalizeFirstCharacterOfString() throws Exception {
String[] strings = {
"FRED",
"fRED",
"fred"
};
Arrays.stream(strings).forEach(el -> assertThat(String.format("%s should be Fred", el), capitalize(el), equalTo("Fred")));
}
@Test
public void lowerFirst_shouldLowercasedFirstCharacterOfString() throws Exception {
assertThat(lowerFirst("FRED"),is(equalTo("fRED")));
assertThat(lowerFirst("fred"),is(equalTo("fred")));
assertThat(lowerFirst("Fred"),is(equalTo("fred")));
}
}