22
33import org .hamcrest .Description ;
44import org .hamcrest .Matcher ;
5- import org .hamcrest .Matchers ;
5+ import org .hamcrest .TypeSafeMatcher ;
66
77import java .util .Arrays ;
88import java .util .regex .Pattern ;
99
1010import static org .hamcrest .core .AnyOf .anyOf ;
1111import static org .hamcrest .core .IsNull .nullValue ;
12- import static org .hamcrest .text .IsEmptyString .IS_EMPTY_STRING ;
1312
1413public 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}
0 commit comments