From 176525cc0ef47feb137fcdfb23223f59f5a53da4 Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Tue, 17 May 2016 19:04:41 +0530 Subject: [PATCH] Added support for `first` and `head` methods --- src/main/java/strman/Strman.java | 27 ++++++++++++++++++++++++--- src/test/java/strman/StrmanTest.java | 25 ++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 8a92aaa..a94485e 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -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) { diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index c810fe4..62ebeb3 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -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"))); } } \ No newline at end of file