mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-09-27 13:33:58 +00:00
Added support for containsAll function
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user