Skip to content

Commit d3c0cac

Browse files
committed
Added support for containsAll function
1 parent 313fb3f commit d3c0cac

3 files changed

Lines changed: 67 additions & 6 deletions

File tree

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,27 @@ final String result = collapseWhitespace(title);
8484
Verifies that the needle is contained in value.
8585

8686
```java
87-
boolean result = contains("foo bar", "bazz", false);
87+
boolean result = contains("foo bar", "BAR", true);
8888
// result = false
8989

9090
boolean result = contains("foo bar", "FOO");
9191
// result = true
9292
```
9393

94+
## containsAll(value, needles, caseSensitive)
95+
96+
Verifies that all needles are contained in value.
97+
98+
```java
99+
boolean result = containsAll("foo bar", new String[]{"FOO", "bar"});
100+
// result = true
101+
102+
boolean result = containsAll("foo bar", new String[]{"FOO", "bar"}, true);
103+
// result = false
104+
```
105+
106+
107+
94108

95109
## Other functions that can be added
96110

src/main/java/strman/Strman.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static Optional<String> at(final String value, int index) {
6969
*
7070
* @param value input
7171
* @param start start
72-
* @param end end
72+
* @param end end
7373
* @return Array containing different parts between start and end.
7474
*/
7575

@@ -124,7 +124,7 @@ static void validate(String value, Predicate<String> predicate, final Supplier<S
124124
/**
125125
* Verifies that the needle is contained in the value. The search is case insensitive
126126
*
127-
* @param value to search
127+
* @param value to search
128128
* @param needle to find
129129
* @return true if found else false.
130130
*/
@@ -133,11 +133,10 @@ static boolean contains(final String value, final String needle) {
133133
}
134134

135135
/**
136-
*
137136
* Verifies that the needle is contained in the value.
138137
*
139-
* @param value to search
140-
* @param needle to find
138+
* @param value to search
139+
* @param needle to find
141140
* @param caseSensitive true or false
142141
* @return true if found else false.
143142
*/
@@ -148,4 +147,29 @@ static boolean contains(final String value, final String needle, final boolean c
148147
}
149148
return value.toLowerCase().indexOf(needle.toLowerCase()) > -1;
150149
}
150+
151+
/**
152+
* Verifies that all needles are contained in value. The search is case insensitive
153+
*
154+
* @param value input String to search
155+
* @param needles needles to find
156+
* @return true if all needles are found else false.
157+
*/
158+
static boolean containsAll(String value, String[] needles) {
159+
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
160+
return Arrays.stream(needles).allMatch(needle -> contains(value, needle, false));
161+
}
162+
163+
/**
164+
* Verifies that all needles are contained in value
165+
*
166+
* @param value input String to search
167+
* @param needles needles to find
168+
* @param caseSensitive true or false
169+
* @return true if all needles are found else false.
170+
*/
171+
static boolean containsAll(String value, String[] needles, boolean caseSensitive) {
172+
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
173+
return Arrays.stream(needles).allMatch(needle -> contains(value, needle, caseSensitive));
174+
}
151175
}

src/test/java/strman/StrmanTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,27 @@ public void containsWithCaseSensitiveTrue_shouldReturnTrueWhenStringContainsNeed
105105

106106
Arrays.stream(fixture).forEach(el -> assertFalse(contains(el, "FOO", true)));
107107
}
108+
109+
@Test
110+
public void containsAll_shouldReturnTrueOnlyWhenAllNeedlesAreContainedInValue() throws Exception {
111+
String[] fixture = {
112+
"foo bar",
113+
"bar foo",
114+
"foobar",
115+
};
116+
117+
Arrays.stream(fixture).forEach(el -> assertTrue(containsAll(el, new String[]{"FOO", "bar"})));
118+
}
119+
120+
@Test
121+
public void containsAll_shouldReturnFalseOnlyWhenAllNeedlesAreNotContainedInValue() throws Exception {
122+
String[] fixture = {
123+
"foo bar",
124+
"bar foo",
125+
"foobar",
126+
};
127+
128+
Arrays.stream(fixture).forEach(el -> assertFalse(containsAll(el, new String[]{"FOO", "bar"}, true)));
129+
}
130+
108131
}

0 commit comments

Comments
 (0)