Skip to content

Commit 313fb3f

Browse files
committed
Added support for contains function
1 parent 49e51fe commit 313fb3f

3 files changed

Lines changed: 73 additions & 11 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ final String result = collapseWhitespace(title);
7979
// result = "foo bar";
8080
```
8181

82+
## contains(value, needle, caseSensitive)
83+
84+
Verifies that the needle is contained in value.
85+
86+
```java
87+
boolean result = contains("foo bar", "bazz", false);
88+
// result = false
89+
90+
boolean result = contains("foo bar", "FOO");
91+
// result = true
92+
```
8293

8394

8495
## Other functions that can be added

src/main/java/strman/Strman.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
public interface Strman {
1313

1414
Predicate<String> NULL_STRING_PREDICATE = str -> str == null;
15-
Predicate<String> EMPTY_STRING_PREDICATE = str -> str.length() == 0;
16-
Predicate<String> NULL_AND_EMPTY_STRING_PREDICATE = NULL_STRING_PREDICATE.and(EMPTY_STRING_PREDICATE);
17-
String EMPTY_STRING = " ";
15+
Supplier<String> NULL_STRING_MSG_SUPPLIER = () -> "'value' should be not null.";
1816

1917
/**
2018
* Appends Strings to value
@@ -35,7 +33,7 @@ static String append(final String value, final String... appends) {
3533
* @return full String
3634
*/
3735
static String appendArray(final String value, final String[] appends) {
38-
validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null.");
36+
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
3937
if (appends == null || appends.length == 0) {
4038
return value;
4139
}
@@ -69,14 +67,14 @@ static Optional<String> at(final String value, int index) {
6967
/**
7068
* Returns an array with strings between start and end.
7169
*
72-
* @param value
73-
* @param start
74-
* @param end
70+
* @param value input
71+
* @param start start
72+
* @param end end
7573
* @return Array containing different parts between start and end.
7674
*/
7775

7876
static String[] between(final String value, final String start, final String end) {
79-
validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null.");
77+
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
8078
validate(start, NULL_STRING_PREDICATE, () -> "'start' should be not null.");
8179
validate(end, NULL_STRING_PREDICATE, () -> "'end' should be not null.");
8280

@@ -95,7 +93,7 @@ static String[] chars(final String value) {
9593
* The other implementation of this could be using String's split method
9694
* String[] chars = value.split("")
9795
*/
98-
validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null.");
96+
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
9997
int length = value.length();
10098
String[] result = new String[length];
10199
for (int i = 0; i < length; i++) {
@@ -112,13 +110,42 @@ static String[] chars(final String value) {
112110
* @return collapsed String
113111
*/
114112
static String collapseWhitespace(final String value) {
115-
validate(value, NULL_STRING_PREDICATE, () -> "'value' should be not null.");
113+
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
116114
return value.trim().replaceAll("\\s\\s+", " ");
117115
}
118116

117+
119118
static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
120119
if (predicate.test(value)) {
121120
throw new IllegalArgumentException(supplier.get());
122121
}
123122
}
123+
124+
/**
125+
* Verifies that the needle is contained in the value. The search is case insensitive
126+
*
127+
* @param value to search
128+
* @param needle to find
129+
* @return true if found else false.
130+
*/
131+
static boolean contains(final String value, final String needle) {
132+
return contains(value, needle, false);
133+
}
134+
135+
/**
136+
*
137+
* Verifies that the needle is contained in the value.
138+
*
139+
* @param value to search
140+
* @param needle to find
141+
* @param caseSensitive true or false
142+
* @return true if found else false.
143+
*/
144+
static boolean contains(final String value, final String needle, final boolean caseSensitive) {
145+
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
146+
if (caseSensitive) {
147+
return value.indexOf(needle) > -1;
148+
}
149+
return value.toLowerCase().indexOf(needle.toLowerCase()) > -1;
150+
}
124151
}

src/test/java/strman/StrmanTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import static org.hamcrest.CoreMatchers.equalTo;
99
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
10-
import static org.junit.Assert.assertThat;
10+
import static org.junit.Assert.*;
1111
import static strman.Strman.*;
1212

1313
public class StrmanTest {
@@ -81,4 +81,28 @@ public void collapseWhitespace_shouldReplaceConsecutiveWhitespaceBetweenMultiple
8181
String input = " foo bar bazz hello world ";
8282
assertThat(collapseWhitespace(input), equalTo("foo bar bazz hello world"));
8383
}
84+
85+
@Test
86+
public void containsWithCaseSensitiveFalse_shouldReturnTrueWhenStringContainsNeedle() throws Exception {
87+
String[] fixture = {
88+
"foo bar",
89+
"bar foo",
90+
"foobar",
91+
"foo"
92+
};
93+
94+
Arrays.stream(fixture).forEach(el -> assertTrue(contains(el, "FOO")));
95+
}
96+
97+
@Test
98+
public void containsWithCaseSensitiveTrue_shouldReturnTrueWhenStringContainsNeedle() throws Exception {
99+
String[] fixture = {
100+
"foo bar",
101+
"bar foo",
102+
"foobar",
103+
"foo"
104+
};
105+
106+
Arrays.stream(fixture).forEach(el -> assertFalse(contains(el, "FOO", true)));
107+
}
84108
}

0 commit comments

Comments
 (0)