From d3c0cac9101feec349df7249a7580d47f8cd1145 Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Wed, 4 May 2016 20:20:01 +0530 Subject: [PATCH] Added support for `containsAll` function --- README.md | 16 ++++++++++++- src/main/java/strman/Strman.java | 34 ++++++++++++++++++++++++---- src/test/java/strman/StrmanTest.java | 23 +++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ec92e50..fc59d7d 100644 --- a/README.md +++ b/README.md @@ -84,13 +84,27 @@ final String result = collapseWhitespace(title); Verifies that the needle is contained in value. ```java -boolean result = contains("foo bar", "bazz", false); +boolean result = contains("foo bar", "BAR", true); // result = false boolean result = contains("foo bar", "FOO"); // result = true ``` +## containsAll(value, needles, caseSensitive) + +Verifies that all needles are contained in value. + +```java + boolean result = containsAll("foo bar", new String[]{"FOO", "bar"}); + // result = true + + boolean result = containsAll("foo bar", new String[]{"FOO", "bar"}, true); + // result = false +``` + + + ## Other functions that can be added diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 6982126..eb7d924 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -69,7 +69,7 @@ public interface Strman { * * @param value input * @param start start - * @param end end + * @param end end * @return Array containing different parts between start and end. */ @@ -124,7 +124,7 @@ public interface Strman { /** * Verifies that the needle is contained in the value. The search is case insensitive * - * @param value to search + * @param value to search * @param needle to find * @return true if found else false. */ @@ -133,11 +133,10 @@ public interface Strman { } /** - * * Verifies that the needle is contained in the value. * - * @param value to search - * @param needle to find + * @param value to search + * @param needle to find * @param caseSensitive true or false * @return true if found else false. */ @@ -148,4 +147,29 @@ public interface Strman { } return value.toLowerCase().indexOf(needle.toLowerCase()) > -1; } + + /** + * Verifies that all needles are contained in value. The search is case insensitive + * + * @param value input String to search + * @param needles needles to find + * @return true if all needles are found else false. + */ + static boolean containsAll(String value, String[] needles) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + return Arrays.stream(needles).allMatch(needle -> contains(value, needle, false)); + } + + /** + * Verifies that all needles are contained in value + * + * @param value input String to search + * @param needles needles to find + * @param caseSensitive true or false + * @return true if all needles are found else false. + */ + static boolean containsAll(String value, String[] needles, boolean caseSensitive) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + return Arrays.stream(needles).allMatch(needle -> contains(value, needle, caseSensitive)); + } } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 2c69fa8..ce99669 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -105,4 +105,27 @@ public class StrmanTest { Arrays.stream(fixture).forEach(el -> assertFalse(contains(el, "FOO", true))); } + + @Test + public void containsAll_shouldReturnTrueOnlyWhenAllNeedlesAreContainedInValue() throws Exception { + String[] fixture = { + "foo bar", + "bar foo", + "foobar", + }; + + Arrays.stream(fixture).forEach(el -> assertTrue(containsAll(el, new String[]{"FOO", "bar"}))); + } + + @Test + public void containsAll_shouldReturnFalseOnlyWhenAllNeedlesAreNotContainedInValue() throws Exception { + String[] fixture = { + "foo bar", + "bar foo", + "foobar", + }; + + Arrays.stream(fixture).forEach(el -> assertFalse(containsAll(el, new String[]{"FOO", "bar"}, true))); + } + } \ No newline at end of file