Skip to content

Commit 0e93dbe

Browse files
committed
Tidying up string matchers. Removed IsEmptyString
1 parent 7577d9b commit 0e93dbe

File tree

4 files changed

+27
-101
lines changed

4 files changed

+27
-101
lines changed

hamcrest-library/src/main/java/org/hamcrest/Matchers.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ public static org.hamcrest.Matcher<java.lang.String> equalToIgnoringWhiteSpace(j
12461246
* <pre>assertThat(((String)null), is(emptyOrNullString()))</pre>
12471247
*/
12481248
public static org.hamcrest.Matcher<java.lang.String> emptyOrNullString() {
1249-
return org.hamcrest.text.IsEmptyString.emptyOrNullString();
1249+
return MatchStrings.emptyOrNullString();
12501250
}
12511251

12521252
/**
@@ -1255,7 +1255,7 @@ public static org.hamcrest.Matcher<java.lang.String> emptyOrNullString() {
12551255
* <pre>assertThat("", is(emptyString()))</pre>
12561256
*/
12571257
public static org.hamcrest.Matcher<java.lang.String> emptyString() {
1258-
return org.hamcrest.text.IsEmptyString.emptyString();
1258+
return MatchStrings.emptyString();
12591259
}
12601260

12611261
/**
@@ -1268,7 +1268,7 @@ public static org.hamcrest.Matcher<java.lang.String> emptyString() {
12681268
*/
12691269
@SuppressWarnings("deprecation")
12701270
public static org.hamcrest.Matcher<java.lang.String> isEmptyOrNullString() {
1271-
return org.hamcrest.text.IsEmptyString.isEmptyOrNullString();
1271+
return MatchStrings.emptyOrNullString();
12721272
}
12731273

12741274
/**
@@ -1280,7 +1280,7 @@ public static org.hamcrest.Matcher<java.lang.String> isEmptyOrNullString() {
12801280
*/
12811281
@SuppressWarnings("deprecation")
12821282
public static org.hamcrest.Matcher<java.lang.String> isEmptyString() {
1283-
return org.hamcrest.text.IsEmptyString.isEmptyString();
1283+
return MatchStrings.emptyString();
12841284
}
12851285

12861286
/**

hamcrest-library/src/main/java/org/hamcrest/text/IsEmptyString.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

hamcrest-library/src/main/java/org/hamcrest/text/MatchStrings.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
5-
import org.hamcrest.Matchers;
5+
import org.hamcrest.TypeSafeMatcher;
66

77
import java.util.Arrays;
88
import java.util.regex.Pattern;
99

1010
import static org.hamcrest.core.AnyOf.anyOf;
1111
import static org.hamcrest.core.IsNull.nullValue;
12-
import static org.hamcrest.text.IsEmptyString.IS_EMPTY_STRING;
1312

1413
public class MatchStrings {
14+
public static final Matcher<String> IS_EMPTY_STRING = new TypeSafeMatcher<String>() {
15+
@Override public void describeTo(Description description) { description.appendText("an empty string"); }
16+
@Override protected boolean matchesSafely(String item) { return item.isEmpty(); }
17+
};
18+
public static final Matcher<String> IS_NULL_OR_EMPTY_STRING = anyOf(IS_EMPTY_STRING, nullValue());
19+
20+
1521
private static final Pattern REGEX_WHITESPACE = Pattern.compile("\\s*");
1622
public static final Matcher<String> IS_BLANK_STRING = new MatchesPattern(REGEX_WHITESPACE) {
1723
@Override
@@ -29,7 +35,7 @@ public void describeTo(Description description) {
2935
*
3036
* @param expectedString the expected value of matched strings
3137
*/
32-
public static Matcher<String> equalToIgnoringCase(java.lang.String expectedString) {
38+
public static Matcher<String> equalToIgnoringCase(String expectedString) {
3339
return new IsEqualIgnoringCase(expectedString);
3440
}
3541

@@ -46,7 +52,7 @@ public static Matcher<String> equalToIgnoringCase(java.lang.String expectedStrin
4652
*
4753
* @param expectedString the expected value of matched strings
4854
*/
49-
public static Matcher<java.lang.String> equalToIgnoringWhiteSpace(java.lang.String expectedString) {
55+
public static Matcher<String> equalToIgnoringWhiteSpace(String expectedString) {
5056
return new IsEqualIgnoringWhiteSpace(expectedString);
5157
}
5258

@@ -56,16 +62,14 @@ public static Matcher<java.lang.String> equalToIgnoringWhiteSpace(java.lang.Stri
5662
* For example:
5763
* <pre>assertThat(((String)null), is(emptyOrNullString()))</pre>
5864
*/
59-
public static Matcher<java.lang.String> emptyOrNullString() {
60-
return Matchers.anyOf(nullValue(), IS_EMPTY_STRING);
61-
}
65+
public static Matcher<String> emptyOrNullString() { return IS_NULL_OR_EMPTY_STRING; }
6266

6367
/**
6468
* Creates a matcher of {@link String} that matches when the examined string has zero length.
6569
* For example:
6670
* <pre>assertThat("", is(emptyString()))</pre>
6771
*/
68-
public static Matcher<java.lang.String> emptyString() {
72+
public static Matcher<String> emptyString() {
6973
return IS_EMPTY_STRING;
7074
}
7175

@@ -75,33 +79,31 @@ public static Matcher<java.lang.String> emptyString() {
7579
* For example:
7680
* <pre>assertThat(((String)null), is(blankOrNullString()))</pre>
7781
*/
78-
public static Matcher<java.lang.String> blankOrNullString() {
79-
return IS_BLANK_OR_NULL_STRING;
80-
}
82+
public static Matcher<String> blankOrNullString() { return IS_BLANK_OR_NULL_STRING; }
8183

8284
/**
8385
* Creates a matcher of {@link String} that matches when the examined string contains
8486
* zero or more whitespace characters and nothing else.
8587
* For example:
8688
* <pre>assertThat(" ", is(blankString()))</pre>
8789
*/
88-
public static Matcher<java.lang.String> blankString() {
90+
public static Matcher<String> blankString() {
8991
return IS_BLANK_STRING;
9092
}
9193

9294
/**
93-
* Creates a matcher of {@link java.lang.String} that matches when the examined string
94-
* exactly matches the given {@link java.util.regex.Pattern}.
95+
* Creates a matcher of {@link String} that matches when the examined string
96+
* exactly matches the given {@link Pattern}.
9597
*/
96-
public static Matcher<java.lang.String> matchesPattern(java.util.regex.Pattern pattern) {
98+
public static Matcher<String> matchesPattern(Pattern pattern) {
9799
return new MatchesPattern(pattern);
98100
}
99101

100102
/**
101-
* Creates a matcher of {@link java.lang.String} that matches when the examined string
102-
* exactly matches the given regular expression, treated as a {@link java.util.regex.Pattern}.
103+
* Creates a matcher of {@link String} that matches when the examined string
104+
* exactly matches the given regular expression, treated as a {@link Pattern}.
103105
*/
104-
public static Matcher<java.lang.String> matchesPattern(java.lang.String regex) {
106+
public static Matcher<String> matchesPattern(String regex) {
105107
return new MatchesPattern(Pattern.compile(regex));
106108
}
107109

@@ -114,7 +116,7 @@ public static Matcher<java.lang.String> matchesPattern(java.lang.String regex) {
114116
*
115117
* @param substrings the substrings that must be contained within matching strings
116118
*/
117-
public static Matcher<java.lang.String> stringContainsInOrder(java.lang.Iterable<java.lang.String> substrings) {
119+
public static Matcher<String> stringContainsInOrder(Iterable<String> substrings) {
118120
return new StringContainsInOrder(substrings);
119121
}
120122

@@ -127,7 +129,7 @@ public static Matcher<java.lang.String> stringContainsInOrder(java.lang.Iterable
127129
*
128130
* @param substrings the substrings that must be contained within matching strings
129131
*/
130-
public static Matcher<java.lang.String> stringContainsInOrder(java.lang.String... substrings) {
132+
public static Matcher<String> stringContainsInOrder(String... substrings) {
131133
return new StringContainsInOrder(Arrays.asList(substrings));
132134
}
133135
}

hamcrest-library/src/test/java/org/hamcrest/text/IsEmptyStringTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class IsEmptyStringTest {
4444
@Test public void
4545
describesItself() {
4646
assertDescription("an empty string", emptyString());
47-
assertDescription("(null or an empty string)", emptyOrNullString());
47+
assertDescription("(an empty string or null)", emptyOrNullString());
4848
}
4949

5050
@Test public void

0 commit comments

Comments
 (0)