|
1 | 1 | package org.hamcrest.number; |
2 | 2 |
|
3 | | -import static org.hamcrest.core.AnyOf.anyOf; |
4 | | -import static org.hamcrest.core.IsEqual.equalTo; |
5 | | -import static org.hamcrest.core.IsNot.not; |
6 | | -import static org.hamcrest.core.DescribedAs.describedAs; |
7 | | - |
| 3 | +import org.hamcrest.Description; |
8 | 4 | import org.hamcrest.Factory; |
9 | 5 | import org.hamcrest.Matcher; |
| 6 | +import org.hamcrest.TypeSafeMatcher; |
10 | 7 |
|
11 | 8 | @SuppressWarnings("unchecked") |
12 | 9 | public class OrderingComparisons { |
| 10 | + public static class OrderingComparison<T extends Comparable> extends TypeSafeMatcher<T> { |
| 11 | + private final T value; |
| 12 | + private final int minCompare, maxCompare; |
| 13 | + |
| 14 | + public OrderingComparison(T value, int minCompare, int maxCompare) { |
| 15 | + this.value = value; |
| 16 | + this.minCompare = minCompare; |
| 17 | + this.maxCompare = maxCompare; |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public boolean matchesSafely(T other) { |
| 22 | + int compare = Integer.signum(value.compareTo(other)); |
| 23 | + return minCompare <= compare && compare <= maxCompare; |
| 24 | + } |
| 25 | + |
| 26 | + public void describeTo(Description description) { |
| 27 | + description.appendText("a value ") |
| 28 | + .appendText(comparison(minCompare)); |
| 29 | + if (minCompare != maxCompare) { |
| 30 | + description.appendText(" or ") |
| 31 | + .appendText(comparison(maxCompare)); |
| 32 | + } |
| 33 | + description.appendText(" ") |
| 34 | + .appendValue(value); |
| 35 | + } |
| 36 | + |
| 37 | + private String comparison(int compare) { |
| 38 | + if (compare > 0) { |
| 39 | + return "less than"; |
| 40 | + } |
| 41 | + else if (compare == 0) { |
| 42 | + return "equal to "; |
| 43 | + } |
| 44 | + else { |
| 45 | + return "greater than"; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
13 | 50 | @Factory |
14 | 51 | public static <T extends Comparable<T>> Matcher<T> greaterThan(T value) { |
15 | | - return new IsGreaterThan<T>(value); |
| 52 | + return new OrderingComparison<T>(value, -1, -1); |
16 | 53 | } |
17 | 54 |
|
18 | 55 | @Factory |
19 | 56 | public static <T extends Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value) { |
20 | | - return describedAs("a value greater than or equal to %0", |
21 | | - anyOf(greaterThan(value), equalTo(value)), |
22 | | - value); |
| 57 | + return new OrderingComparison<T>(value, -1, 0); |
23 | 58 | } |
24 | 59 |
|
25 | 60 | @Factory |
26 | 61 | public static <T extends Comparable<T>> Matcher<T> lessThan(T value) { |
27 | | - return describedAs("a value less than %0", |
28 | | - not(greaterThanOrEqualTo(value)), |
29 | | - value); |
| 62 | + return new OrderingComparison<T>(value, 1, 1); |
30 | 63 | } |
31 | 64 |
|
32 | 65 | @Factory |
33 | 66 | public static <T extends Comparable<T>> Matcher<T> lessThanOrEqualTo(T value) { |
34 | | - return describedAs("a value less than or equal to %0", |
35 | | - not(greaterThan(value)), |
36 | | - value); |
| 67 | + return new OrderingComparison<T>(value, 0, 1); |
37 | 68 | } |
38 | 69 | } |
0 commit comments