Added support for chars function

This commit is contained in:
Shekhar Gulati
2016-05-04 19:08:21 +05:30
parent dce16e8290
commit 59da6a956f
3 changed files with 47 additions and 4 deletions
+22 -2
View File
@@ -18,7 +18,7 @@ public interface Strman {
* @param appends an array of strings to append
* @return full String
*/
static String append(final String value, final String... appends) throws IllegalArgumentException {
static String append(final String value, final String... appends) {
return appendArray(value, appends);
}
@@ -79,9 +79,29 @@ public interface Strman {
return Arrays.stream(parts).map(subPart -> subPart.substring(subPart.indexOf(start) + start.length())).toArray(String[]::new);
}
static void validate(String value, Supplier<String> supplier) {
static void validate(final String value, final Supplier<String> supplier) {
if (value == null) {
throw new IllegalArgumentException(supplier.get());
}
}
/**
* Returns a String array consisting of the characters in the String.
*
* @param value input
* @return character array
*/
static String[] chars(final String value) {
/**
* The other implementation of this could be using String's split method
* String[] chars = value.split("")
*/
validate(value, () -> "'value' should be not null.");
int length = value.length();
String[] result = new String[length];
for (int i = 0; i < length; i++) {
result[i] = at(value, i).get();
}
return result;
}
}
+6 -1
View File
@@ -6,7 +6,6 @@ import java.util.Optional;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
import static org.hamcrest.collection.IsArrayWithSize.emptyArray;
import static org.junit.Assert.assertThat;
import static strman.Strman.*;
@@ -58,4 +57,10 @@ public class StrmanTest {
assertThat(between("[abc][def]", "{", "}"), arrayContaining("[abc][def]"));
assertThat(between("", "{", "}"), arrayContaining(""));
}
@Test
public void charsShouldReturnAllCharactersInString() throws Exception {
final String title = "title";
assertThat(chars(title), equalTo(new String[]{"t", "i", "t", "l", "e"}));
}
}