Skip to content

Commit c426369

Browse files
author
joeretro
committed
Java5ified IsEqualIgnoringWhiteSpace
1 parent d790a11 commit c426369

3 files changed

Lines changed: 30 additions & 19 deletions

File tree

‎src/library/org/hamcrest/text/IsEqualIgnoringCase.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*/
1212
public class IsEqualIgnoringCase implements Matcher<String> {
1313

14+
// TODO: Replace String with CharSequence to allow for easy interopability between
15+
// String, StringBuffer, StringBuilder, CharBuffer, etc (joe).
16+
1417
private final String string;
1518

1619
public IsEqualIgnoringCase(String string) {

‎src/library/org/hamcrest/text/IsEqualIgnoringWhiteSpace.java‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89
/**
910
* Tests if a string is equal to another string, ignoring any changes in whitespace.
1011
*/
11-
public class IsEqualIgnoringWhiteSpace implements Matcher {
12+
public class IsEqualIgnoringWhiteSpace implements Matcher<String> {
13+
14+
// TODO: Replace String with CharSequence to allow for easy interopability between
15+
// String, StringBuffer, StringBuilder, CharBuffer, etc (joe).
1216

1317
private final String string;
1418

@@ -19,13 +23,11 @@ public IsEqualIgnoringWhiteSpace(String string) {
1923
this.string = string;
2024
}
2125

22-
public boolean match(Object o) {
23-
if (o == null) {
24-
return false;
25-
} else if (!(o instanceof String)) {
26+
public boolean match(String item) {
27+
if (item == null) {
2628
return false;
2729
} else {
28-
return stripSpace(string).equalsIgnoreCase(stripSpace((String) o));
30+
return stripSpace(string).equalsIgnoreCase(stripSpace(item));
2931
}
3032
}
3133

@@ -36,7 +38,7 @@ public void describeTo(Description description) {
3638
}
3739

3840
public String stripSpace(String string) {
39-
StringBuffer result = new StringBuffer();
41+
StringBuilder result = new StringBuilder();
4042
boolean lastWasSpace = true;
4143
for (int i = 0; i < string.length(); i++) {
4244
char c = string.charAt(i);
@@ -53,4 +55,9 @@ public String stripSpace(String string) {
5355
return result.toString().trim();
5456
}
5557

58+
@Factory
59+
public static Matcher<String> eqIgnoringWhiteSpace(String string) {
60+
return new IsEqualIgnoringWhiteSpace(string);
61+
}
62+
5663
}

‎src/unit-test/org/hamcrest/text/IsEqualIgnoringWhiteSpaceTest.java‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,35 @@
22
*/
33
package org.hamcrest.text;
44

5+
import static org.hamcrest.core.IsNot.not;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.eqIgnoringWhiteSpace;
58
import org.hamcrest.AbstractMatcherTest;
69
import org.hamcrest.Matcher;
10+
import org.hamcrest.MatcherAssert;
11+
import org.hamcrest.core.IsNot;
712

813
public class IsEqualIgnoringWhiteSpaceTest extends AbstractMatcherTest {
914

10-
private final Matcher matcher = new IsEqualIgnoringWhiteSpace("Hello World how\n are we? ");
15+
private final Matcher<String> matcher = eqIgnoringWhiteSpace("Hello World how\n are we? ");
1116

1217
public void testPassesIfWordsAreSameButWhitespaceDiffers() {
13-
assertTrue(matcher.match("Hello World how are we?"));
14-
assertTrue(matcher.match(" Hello World how are \n\n\twe?"));
18+
assertThat("Hello World how are we?", matcher);
19+
assertThat(" Hello World how are \n\n\twe?", matcher);
1520
}
1621

1722
public void testFailsIfTextOtherThanWhitespaceDiffers() {
18-
assertFalse(matcher.match("Hello PLANET how are we?"));
19-
assertFalse(matcher.match("Hello World how are we"));
23+
assertThat("Hello PLANET how are we?", not(matcher));
24+
assertThat("Hello World how are we", not(matcher));
2025
}
2126

2227
public void testFailsIfWhitespaceIsAddedOrRemovedInMidWord() {
23-
assertFalse(matcher.match("HelloWorld how are we?"));
24-
assertFalse(matcher.match("Hello Wo rld how are we?"));
25-
}
26-
27-
public void testFailsIfNotMatchingAgainstString() {
28-
assertFalse(matcher.match(new Object()));
28+
assertThat("HelloWorld how are we?", not(matcher));
29+
assertThat("Hello Wo rld how are we?", not(matcher));
2930
}
3031

3132
public void testFailsIfMatchingAgainstNull() {
32-
assertFalse(matcher.match(null));
33+
assertThat(null, not(matcher));
3334
}
3435

3536
public void testRequiresNonNullStringToBeConstructed() {

0 commit comments

Comments
 (0)