From b24a5ebbf2049094bbf53f1dd3091f0419850c6a Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Fri, 6 May 2016 15:40:47 +0530 Subject: [PATCH] Added support for `containsAny` function --- README.md | 8 ++++++++ src/main/java/strman/Strman.java | 17 +++++++++++++---- src/test/java/strman/StrmanTest.java | 20 +++++++++++++++++++- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fc59d7d..cd63411 100644 --- a/README.md +++ b/README.md @@ -103,8 +103,16 @@ Verifies that all needles are contained in value. // result = false ``` +## containsAny(value, needles, caseSensitive) +Verifies that one or more of needles are contained in value. +```java +String title = "foo bar"; +String needles = ["foo", "bar", "test"]; +String result = containsAny(title, needles, true); +// 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 eb7d924..43ddd69 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -151,11 +151,11 @@ public interface Strman { /** * Verifies that all needles are contained in value. The search is case insensitive * - * @param value input String to search - * @param needles needles to find + * @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) { + static boolean containsAll(final String value, final String[] needles) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return Arrays.stream(needles).allMatch(needle -> contains(value, needle, false)); } @@ -168,8 +168,17 @@ public interface Strman { * @param caseSensitive true or false * @return true if all needles are found else false. */ - static boolean containsAll(String value, String[] needles, boolean caseSensitive) { + static boolean containsAll(final String value, final String[] needles, final boolean caseSensitive) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return Arrays.stream(needles).allMatch(needle -> contains(value, needle, caseSensitive)); } + + static boolean containsAny(final String value, final String[] needles) { + return containsAny(value, needles, false); + } + + static boolean containsAny(final String value, final String[] needles, final boolean caseSensitive) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + return Arrays.stream(needles).anyMatch(needle -> contains(value, needle, caseSensitive)); + } } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index ce99669..74ceb39 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -124,8 +124,26 @@ public class StrmanTest { "bar foo", "foobar", }; - Arrays.stream(fixture).forEach(el -> assertFalse(containsAll(el, new String[]{"FOO", "bar"}, true))); } + @Test + public void containsAny_shouldReturnTrueWhenAnyOfSearchNeedleExistInInputValue() throws Exception { + String[] fixture = { + "foo bar", + "bar foo", + "foobar", + }; + Arrays.stream(fixture).forEach(el -> assertTrue(containsAny(el, new String[]{"foo", "bar", "test"}))); + } + + @Test + public void containsAny_shouldReturnFalseWhenNoneOfSearchNeedleExistInInputValue() throws Exception { + String[] fixture = { + "foo bar", + "bar foo", + "foobar", + }; + Arrays.stream(fixture).forEach(el -> assertFalse(containsAny(el, new String[]{"FOO", "BAR", "Test"}, true))); + } } \ No newline at end of file