Added support for first and head methods

This commit is contained in:
Shekhar Gulati
2016-05-17 19:04:41 +05:30
parent 1931c223a2
commit 176525cc0e
2 changed files with 46 additions and 6 deletions
+24 -3
View File
@@ -373,8 +373,8 @@ public abstract class Strman {
/**
* Ensures that the value ends with suffix. If it doesn't, it's appended. This operation is case sensitive.
*
* @param value The input String
* @param suffix The substr to be ensured to be right
* @param value The input String
* @param suffix The substr to be ensured to be right
* @return The string which is guarenteed to start with substr
*/
public static String ensureRight(final String value, final String suffix) {
@@ -394,6 +394,28 @@ public abstract class Strman {
return endsWith(value, suffix, caseSensitive) ? value : append(value, suffix);
}
/**
* Returns the first n chars of String
*
* @param value The input String
* @param n Number of chars to return
* @return The first n chars
*/
public static String first(final String value, final int n) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return value.substring(0, n);
}
/**
* Return the first char of String
*
* @param value The input String
* @return The first char
*/
public static String head(final String value) {
return first(value, 1);
}
public static String leftPad(final String value, final String pad, final int length) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
if (value.length() >= length) {
@@ -402,7 +424,6 @@ public abstract class Strman {
return append(Stream.generate(() -> pad).limit(length - value.length()).collect(joining()), value);
}
private static long countSubstr(String value, String subStr, boolean allowOverlapping, long count) {
int position = value.indexOf(subStr);
if (position == -1) {
+22 -3
View File
@@ -4,7 +4,6 @@ import org.junit.Test;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.equalTo;
@@ -305,7 +304,27 @@ public class StrmanTest {
@Test
public void ensureRight_shouldEnsureStringEndsWithBar() throws Exception {
assertThat(Stream.of("foo", "foobar", "fooBAR").map(el -> ensureRight(el, "bar", false)).collect(toList()), hasItems("foobar", "foobar", "fooBAR"));
assertThat(Stream.of("foo", "foobar", "fooBAR").map(el -> ensureRight(el, "bar")).collect(toList()), hasItems("foobar", "foobar", "fooBARbar"));
final String[] fixture = {
"foo", "foobar", "fooBAR"
};
assertThat(Arrays.stream(fixture).map(el -> ensureRight(el, "bar", false)).collect(toList()), hasItems("foobar", "foobar", "fooBAR"));
assertThat(Arrays.stream(fixture).map(el -> ensureRight(el, "bar")).collect(toList()), hasItems("foobar", "foobar", "fooBARbar"));
}
@Test
public void first_shouldReturnFirstThreeCharsOfString() throws Exception {
final String[] fixture = {
"foo", "foobar"
};
Arrays.stream(fixture).forEach(el -> assertThat(first(el, 3), equalTo("foo")));
}
@Test
public void head_shouldReturnFirstCharOfString() throws Exception {
final String[] fixture = {
"foo", "foobar"
};
Arrays.stream(fixture).forEach(el -> assertThat(head(el), equalTo("f")));
}
}