Skip to content

Commit 5859a75

Browse files
committed
Merge branch 'markslater-master'
2 parents fd6acee + 72dc4c1 commit 5859a75

4 files changed

Lines changed: 431 additions & 75 deletions

File tree

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package org.hamcrest.comparator;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Factory;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeMatcher;
7+
8+
import java.util.Comparator;
9+
10+
import static java.lang.Integer.signum;
11+
12+
public final class ComparatorMatcherBuilder<T> {
13+
14+
private final Comparator<T> comparator;
15+
private final boolean includeComparatorInDescription;
16+
17+
/**
18+
* Creates a matcher factory for matchers of {@code Comparable}s.
19+
* <p/>
20+
* For example:
21+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().lessThanOrEqualTo(1))</pre>
22+
*/
23+
public static <T extends Comparable<T>> ComparatorMatcherBuilder<T> usingNaturalOrdering() {
24+
return new ComparatorMatcherBuilder<T>(new Comparator<T>() {
25+
@Override
26+
public int compare(T o1, T o2) {
27+
return o1.compareTo(o2);
28+
}
29+
}, false);
30+
}
31+
32+
/**
33+
* Creates a matcher factory for matchers of {@code Comparators}s of {@code T}.
34+
* <p/>
35+
* For example:
36+
* <pre>assertThat(5, comparedBy(new Comparator<Integer>() {
37+
* public int compare(Integer o1, Integer o2) {
38+
* return -o1.compareTo(o2);
39+
* }
40+
* }).lessThan(4))</pre>
41+
*/
42+
public static <T> ComparatorMatcherBuilder<T> comparedBy(Comparator<T> comparator) {
43+
return new ComparatorMatcherBuilder<T>(comparator, true);
44+
}
45+
46+
private ComparatorMatcherBuilder(Comparator<T> comparator, boolean includeComparatorInDescription) {
47+
this.comparator = comparator;
48+
this.includeComparatorInDescription = includeComparatorInDescription;
49+
}
50+
51+
private static final class ComparatorMatcher<T> extends TypeSafeMatcher<T> {
52+
private static final int LESS_THAN = -1;
53+
private static final int GREATER_THAN = 1;
54+
private static final int EQUAL = 0;
55+
56+
private final Comparator<T> comparator;
57+
private final T expected;
58+
private final int minCompare;
59+
private final int maxCompare;
60+
private final boolean includeComparatorInDescription;
61+
62+
private static final String[] comparisonDescriptions = {
63+
"less than",
64+
"equal to",
65+
"greater than"
66+
};
67+
68+
private ComparatorMatcher(Comparator<T> comparator, T expected, int minCompare, int maxCompare, boolean includeComparatorInDescription) {
69+
this.comparator = comparator;
70+
this.expected = expected;
71+
this.minCompare = minCompare;
72+
this.maxCompare = maxCompare;
73+
this.includeComparatorInDescription = includeComparatorInDescription;
74+
}
75+
76+
@Override
77+
public boolean matchesSafely(T actual) {
78+
try {
79+
int compare = signum(comparator.compare(actual, expected));
80+
return minCompare <= compare && compare <= maxCompare;
81+
} catch (ClassCastException e) {
82+
return false; // type erasure means someone can shonk in a non-T :(
83+
}
84+
}
85+
86+
@Override
87+
public void describeMismatchSafely(T actual, Description mismatchDescription) {
88+
mismatchDescription.appendValue(actual).appendText(" was ")
89+
.appendText(asText(comparator.compare(actual, expected)))
90+
.appendText(" ").appendValue(expected);
91+
if (includeComparatorInDescription) {
92+
mismatchDescription.appendText(" when compared by ").appendValue(comparator);
93+
}
94+
}
95+
96+
@Override
97+
public void describeTo(Description description) {
98+
description.appendText("a value ").appendText(asText(minCompare));
99+
if (minCompare != maxCompare) {
100+
description.appendText(" or ").appendText(asText(maxCompare));
101+
}
102+
description.appendText(" ").appendValue(expected);
103+
if (includeComparatorInDescription) {
104+
description.appendText(" when compared by ").appendValue(comparator);
105+
}
106+
}
107+
108+
private static String asText(int comparison) {
109+
return comparisonDescriptions[signum(comparison) + 1];
110+
}
111+
}
112+
113+
/**
114+
* Creates a matcher of {@code T} object that matches when the examined object is
115+
* equal to the specified value, as reported by the {@code Comparator} used to
116+
* create this builder.
117+
* <p/>
118+
* For example:
119+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().comparesEqualTo(1))</pre>
120+
*
121+
* @param value the value which, when passed to the Comparator supplied to this builder, should return zero
122+
*/
123+
@Factory
124+
public Matcher<T> comparesEqualTo(T value) {
125+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.EQUAL, ComparatorMatcher.EQUAL, includeComparatorInDescription);
126+
}
127+
128+
/**
129+
* Creates a matcher of {@code T} object that matches when the examined object is
130+
* greater than the specified value, as reported by the {@code Comparator} used to
131+
* create this builder.
132+
* <p/>
133+
* For example:
134+
* <pre>assertThat(2, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().greaterThan(1))</pre>
135+
*
136+
* @param value the value which, when passed to the Comparator supplied to this builder, should return greater
137+
* than zero
138+
*/
139+
@Factory
140+
public Matcher<T> greaterThan(T value) {
141+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.GREATER_THAN, ComparatorMatcher.GREATER_THAN, includeComparatorInDescription);
142+
}
143+
144+
/**
145+
* Creates a matcher of {@code T} object that matches when the examined object is
146+
* greater than or equal to the specified value, as reported by the {@code Comparator} used to
147+
* create this builder.
148+
* <p/>
149+
* For example:
150+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().greaterThanOrEqualTo(1))</pre>
151+
*
152+
* @param value the value which, when passed to the Comparator supplied to this builder, should return greater
153+
* than or equal to zero
154+
*/
155+
@Factory
156+
public Matcher<T> greaterThanOrEqualTo(T value) {
157+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.EQUAL, ComparatorMatcher.GREATER_THAN, includeComparatorInDescription);
158+
}
159+
160+
/**
161+
* Creates a matcher of {@code T} object that matches when the examined object is
162+
* less than the specified value, as reported by the {@code Comparator} used to
163+
* create this builder.
164+
* <p/>
165+
* For example:
166+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().lessThan(2))</pre>
167+
*
168+
* @param value the value which, when passed to the Comparator supplied to this builder, should return less
169+
* than zero
170+
*/
171+
@Factory
172+
public Matcher<T> lessThan(T value) {
173+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.LESS_THAN, ComparatorMatcher.LESS_THAN, includeComparatorInDescription);
174+
}
175+
176+
/**
177+
* Creates a matcher of {@code T} object that matches when the examined object is
178+
* less than or equal to the specified value, as reported by the {@code Comparator} used to
179+
* create this builder.
180+
* <p/>
181+
* For example:
182+
* <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().lessThanOrEqualTo(1))</pre>
183+
*
184+
* @param value the value which, when passed to the Comparator supplied to this builder, should return less
185+
* than or equal to zero
186+
*/
187+
@Factory
188+
public Matcher<T> lessThanOrEqualTo(T value) {
189+
return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.LESS_THAN, ComparatorMatcher.EQUAL, includeComparatorInDescription);
190+
}
191+
}

‎hamcrest-library/src/main/java/org/hamcrest/number/OrderingComparison.java‎

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,13 @@
22
*/
33
package org.hamcrest.number;
44

5-
import org.hamcrest.Description;
65
import org.hamcrest.Factory;
76
import org.hamcrest.Matcher;
8-
import org.hamcrest.TypeSafeMatcher;
7+
import org.hamcrest.comparator.ComparatorMatcherBuilder;
98

10-
import static java.lang.Integer.signum;
9+
public class OrderingComparison {
1110

12-
public class OrderingComparison<T extends Comparable<T>> extends TypeSafeMatcher<T> {
13-
private static final int LESS_THAN = -1;
14-
private static final int GREATER_THAN = 1;
15-
private static final int EQUAL = 0;
16-
private final T expected;
17-
private final int minCompare, maxCompare;
18-
19-
private static final String[] comparisonDescriptions = {
20-
"less than",
21-
"equal to",
22-
"greater than"
23-
};
24-
25-
private OrderingComparison(T expected, int minCompare, int maxCompare) {
26-
this.expected = expected;
27-
this.minCompare = minCompare;
28-
this.maxCompare = maxCompare;
29-
}
30-
31-
@Override
32-
public boolean matchesSafely(T actual) {
33-
int compare = signum(actual.compareTo(expected));
34-
return minCompare <= compare && compare <= maxCompare;
35-
}
36-
37-
@Override
38-
public void describeMismatchSafely(T actual, Description mismatchDescription) {
39-
mismatchDescription.appendValue(actual).appendText(" was ")
40-
.appendText(asText(actual.compareTo(expected)))
41-
.appendText(" ").appendValue(expected);
42-
}
43-
44-
@Override
45-
public void describeTo(Description description) {
46-
description.appendText("a value ").appendText(asText(minCompare));
47-
if (minCompare != maxCompare) {
48-
description.appendText(" or ").appendText(asText(maxCompare));
49-
}
50-
description.appendText(" ").appendValue(expected);
51-
}
52-
53-
private static String asText(int comparison) {
54-
return comparisonDescriptions[signum(comparison) + 1];
11+
private OrderingComparison() {
5512
}
5613

5714
/**
@@ -61,14 +18,12 @@ private static String asText(int comparison) {
6118
* <p/>
6219
* For example:
6320
* <pre>assertThat(1, comparesEqualTo(1))</pre>
64-
*
65-
* @param value
66-
* the value which, when passed to the compareTo method of the examined object, should return zero
67-
*
21+
*
22+
* @param value the value which, when passed to the compareTo method of the examined object, should return zero
6823
*/
6924
@Factory
7025
public static <T extends Comparable<T>> Matcher<T> comparesEqualTo(T value) {
71-
return new OrderingComparison<T>(value, EQUAL, EQUAL);
26+
return ComparatorMatcherBuilder.<T>usingNaturalOrdering().comparesEqualTo(value);
7227
}
7328

7429
/**
@@ -78,15 +33,13 @@ public static <T extends Comparable<T>> Matcher<T> comparesEqualTo(T value) {
7833
* <p/>
7934
* For example:
8035
* <pre>assertThat(2, greaterThan(1))</pre>
81-
*
82-
* @param value
83-
* the value which, when passed to the compareTo method of the examined object, should return greater
84-
* than zero
85-
*
36+
*
37+
* @param value the value which, when passed to the compareTo method of the examined object, should return greater
38+
* than zero
8639
*/
8740
@Factory
8841
public static <T extends Comparable<T>> Matcher<T> greaterThan(T value) {
89-
return new OrderingComparison<T>(value, GREATER_THAN, GREATER_THAN);
42+
return ComparatorMatcherBuilder.<T>usingNaturalOrdering().greaterThan(value);
9043
}
9144

9245
/**
@@ -96,15 +49,13 @@ public static <T extends Comparable<T>> Matcher<T> greaterThan(T value) {
9649
* <p/>
9750
* For example:
9851
* <pre>assertThat(1, greaterThanOrEqualTo(1))</pre>
99-
*
100-
* @param value
101-
* the value which, when passed to the compareTo method of the examined object, should return greater
102-
* than or equal to zero
103-
*
52+
*
53+
* @param value the value which, when passed to the compareTo method of the examined object, should return greater
54+
* than or equal to zero
10455
*/
10556
@Factory
10657
public static <T extends Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value) {
107-
return new OrderingComparison<T>(value, EQUAL, GREATER_THAN);
58+
return ComparatorMatcherBuilder.<T>usingNaturalOrdering().greaterThanOrEqualTo(value);
10859
}
10960

11061
/**
@@ -114,15 +65,13 @@ public static <T extends Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value)
11465
* <p/>
11566
* For example:
11667
* <pre>assertThat(1, lessThan(2))</pre>
117-
*
118-
* @param value
119-
* the value which, when passed to the compareTo method of the examined object, should return less
120-
* than zero
121-
*
68+
*
69+
* @param value the value which, when passed to the compareTo method of the examined object, should return less
70+
* than zero
12271
*/
12372
@Factory
12473
public static <T extends Comparable<T>> Matcher<T> lessThan(T value) {
125-
return new OrderingComparison<T>(value, LESS_THAN, LESS_THAN);
74+
return ComparatorMatcherBuilder.<T>usingNaturalOrdering().lessThan(value);
12675
}
12776

12877
/**
@@ -132,14 +81,12 @@ public static <T extends Comparable<T>> Matcher<T> lessThan(T value) {
13281
* <p/>
13382
* For example:
13483
* <pre>assertThat(1, lessThanOrEqualTo(1))</pre>
135-
*
136-
* @param value
137-
* the value which, when passed to the compareTo method of the examined object, should return less
138-
* than or equal to zero
139-
*
84+
*
85+
* @param value the value which, when passed to the compareTo method of the examined object, should return less
86+
* than or equal to zero
14087
*/
14188
@Factory
14289
public static <T extends Comparable<T>> Matcher<T> lessThanOrEqualTo(T value) {
143-
return new OrderingComparison<T>(value, LESS_THAN, EQUAL);
90+
return ComparatorMatcherBuilder.<T>usingNaturalOrdering().lessThanOrEqualTo(value);
14491
}
14592
}

0 commit comments

Comments
 (0)