|
1 | 1 | package org.hamcrest.number; |
2 | 2 |
|
3 | | -import org.hamcrest.Description; |
4 | | -import org.hamcrest.Factory; |
5 | 3 | import org.hamcrest.Matcher; |
6 | | -import org.hamcrest.TypeSafeMatcher; |
7 | 4 |
|
8 | | -@SuppressWarnings("unchecked") |
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 | 5 |
|
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 | | - |
50 | | - @Factory |
| 6 | +/** |
| 7 | + * Use {@link OrderingComparison} instead. |
| 8 | + */ |
| 9 | +@Deprecated |
| 10 | +public class OrderingComparisons { |
51 | 11 | public static <T extends Comparable<T>> Matcher<T> greaterThan(T value) { |
52 | | - return new OrderingComparison<T>(value, -1, -1); |
| 12 | + return OrderingComparison.greaterThan(value); |
53 | 13 | } |
54 | | - |
55 | | - @Factory |
| 14 | + |
56 | 15 | public static <T extends Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value) { |
57 | | - return new OrderingComparison<T>(value, -1, 0); |
| 16 | + return OrderingComparison.greaterThanOrEqualTo(value); |
58 | 17 | } |
59 | | - |
60 | | - @Factory |
| 18 | + |
61 | 19 | public static <T extends Comparable<T>> Matcher<T> lessThan(T value) { |
62 | | - return new OrderingComparison<T>(value, 1, 1); |
| 20 | + return OrderingComparison.lessThan(value); |
63 | 21 | } |
64 | 22 |
|
65 | | - @Factory |
66 | 23 | public static <T extends Comparable<T>> Matcher<T> lessThanOrEqualTo(T value) { |
67 | | - return new OrderingComparison<T>(value, 0, 1); |
| 24 | + return OrderingComparison.lessThanOrEqualTo(value); |
68 | 25 | } |
69 | 26 | } |
0 commit comments