Added support for ensureRight

This commit is contained in:
Shekhar Gulati
2016-05-17 18:53:47 +05:30
parent 169404c795
commit 1931c223a2
2 changed files with 14 additions and 9 deletions
+13 -8
View File
@@ -370,23 +370,28 @@ public abstract class Strman {
return value.chars().mapToObj(ch -> leftPad(Integer.toString(ch), "0", 5)).collect(joining());
}
/**
* 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
* @return The string which is guarenteed to start with substr
*/
public static String ensureRight(final String value, final String suffix) {
return ensureRight(value, suffix, true);
}
/**
* Ensures that the value ends with suffix. If it doesn't, it's appended.
*
* @param value The input String
* @param suffix The substr to be ensured to be right
* @param caseSensitive Use case (in-)sensitive matching for determining if value already ends with suffix
* @return
* @return The string which is guarenteed to start with substr
*/
public static String ensureRight(final String value, final String suffix, boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
boolean endsWith;
if (caseSensitive) {
endsWith = value.endsWith(suffix);
} else {
endsWith = value.toLowerCase().endsWith(suffix.toLowerCase());
}
return endsWith ? value : append(value, suffix);
return endsWith(value, suffix, caseSensitive) ? value : append(value, suffix);
}
public static String leftPad(final String value, final String pad, final int length) {
+1 -1
View File
@@ -306,6 +306,6 @@ 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", true)).collect(toList()), hasItems("foobar", "foobar", "fooBARbar"));
assertThat(Stream.of("foo", "foobar", "fooBAR").map(el -> ensureRight(el, "bar")).collect(toList()), hasItems("foobar", "foobar", "fooBARbar"));
}
}