mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-20 14:34:04 +00:00
Added support for containsAny function
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user