diff --git a/README.md b/README.md index 51f3b3c..ec92e50 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,17 @@ final String result = collapseWhitespace(title); // result = "foo bar"; ``` +## contains(value, needle, caseSensitive) + +Verifies that the needle is contained in value. + +```java +boolean result = contains("foo bar", "bazz", false); +// result = false + +boolean result = contains("foo bar", "FOO"); +// result = true +``` ## Other functions that can be added diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index e06ec45..6982126 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -12,9 +12,7 @@ import java.util.function.Supplier; public interface Strman { Predicate NULL_STRING_PREDICATE = str -> str == null; - Predicate EMPTY_STRING_PREDICATE = str -> str.length() == 0; - Predicate NULL_AND_EMPTY_STRING_PREDICATE = NULL_STRING_PREDICATE.and(EMPTY_STRING_PREDICATE); - String EMPTY_STRING = " "; + Supplier NULL_STRING_MSG_SUPPLIER = () -> "'value' should be not null."; /** * Appends Strings to value @@ -35,7 +33,7 @@ public interface Strman { * @return full String */ static String appendArray(final String value, final String[] appends) { - validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null."); + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); if (appends == null || appends.length == 0) { return value; } @@ -69,14 +67,14 @@ public interface Strman { /** * Returns an array with strings between start and end. * - * @param value - * @param start - * @param end + * @param value input + * @param start start + * @param end end * @return Array containing different parts between start and end. */ static String[] between(final String value, final String start, final String end) { - validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null."); + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); validate(start, NULL_STRING_PREDICATE, () -> "'start' should be not null."); validate(end, NULL_STRING_PREDICATE, () -> "'end' should be not null."); @@ -95,7 +93,7 @@ public interface Strman { * The other implementation of this could be using String's split method * String[] chars = value.split("") */ - validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null."); + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); int length = value.length(); String[] result = new String[length]; for (int i = 0; i < length; i++) { @@ -112,13 +110,42 @@ public interface Strman { * @return collapsed String */ static String collapseWhitespace(final String value) { - validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null."); + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return value.trim().replaceAll("\\s\\s+", " "); } + static void validate(String value, Predicate predicate, final Supplier supplier) { if (predicate.test(value)) { throw new IllegalArgumentException(supplier.get()); } } + + /** + * Verifies that the needle is contained in the value. The search is case insensitive + * + * @param value to search + * @param needle to find + * @return true if found else false. + */ + static boolean contains(final String value, final String needle) { + return contains(value, needle, false); + } + + /** + * + * Verifies that the needle is contained in the value. + * + * @param value to search + * @param needle to find + * @param caseSensitive true or false + * @return true if found else false. + */ + static boolean contains(final String value, final String needle, final boolean caseSensitive) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + if (caseSensitive) { + return value.indexOf(needle) > -1; + } + return value.toLowerCase().indexOf(needle.toLowerCase()) > -1; + } } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index d775374..2c69fa8 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -7,7 +7,7 @@ import java.util.Optional; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; import static strman.Strman.*; public class StrmanTest { @@ -81,4 +81,28 @@ public class StrmanTest { String input = " foo bar bazz hello world "; assertThat(collapseWhitespace(input), equalTo("foo bar bazz hello world")); } + + @Test + public void containsWithCaseSensitiveFalse_shouldReturnTrueWhenStringContainsNeedle() throws Exception { + String[] fixture = { + "foo bar", + "bar foo", + "foobar", + "foo" + }; + + Arrays.stream(fixture).forEach(el -> assertTrue(contains(el, "FOO"))); + } + + @Test + public void containsWithCaseSensitiveTrue_shouldReturnTrueWhenStringContainsNeedle() throws Exception { + String[] fixture = { + "foo bar", + "bar foo", + "foobar", + "foo" + }; + + Arrays.stream(fixture).forEach(el -> assertFalse(contains(el, "FOO", true))); + } } \ No newline at end of file