Added support for containsAll function

This commit is contained in:
Shekhar Gulati
2016-05-04 20:20:01 +05:30
parent 313fb3f989
commit d3c0cac910
3 changed files with 67 additions and 6 deletions
+15 -1
View File
@@ -84,13 +84,27 @@ final String result = collapseWhitespace(title);
Verifies that the needle is contained in value. Verifies that the needle is contained in value.
```java ```java
boolean result = contains("foo bar", "bazz", false); boolean result = contains("foo bar", "BAR", true);
// result = false // result = false
boolean result = contains("foo bar", "FOO"); boolean result = contains("foo bar", "FOO");
// result = true // 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 ## Other functions that can be added
+29 -5
View File
@@ -69,7 +69,7 @@ public interface Strman {
* *
* @param value input * @param value input
* @param start start * @param start start
* @param end end * @param end end
* @return Array containing different parts between start and 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 * 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 * @param needle to find
* @return true if found else false. * @return true if found else false.
*/ */
@@ -133,11 +133,10 @@ public interface Strman {
} }
/** /**
*
* Verifies that the needle is contained in the value. * Verifies that the needle is contained in the value.
* *
* @param value to search * @param value to search
* @param needle to find * @param needle to find
* @param caseSensitive true or false * @param caseSensitive true or false
* @return true if found else false. * @return true if found else false.
*/ */
@@ -148,4 +147,29 @@ public interface Strman {
} }
return value.toLowerCase().indexOf(needle.toLowerCase()) > -1; 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));
}
} }
+23
View File
@@ -105,4 +105,27 @@ public class StrmanTest {
Arrays.stream(fixture).forEach(el -> assertFalse(contains(el, "FOO", true))); 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)));
}
} }