22 */
33package org .hamcrest .number ;
44
5- import org .hamcrest .Description ;
65import org .hamcrest .Factory ;
76import 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 >usingDefaultComparison (). 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 >usingDefaultComparison (). 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 >usingDefaultComparison (). 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 >usingDefaultComparison (). 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 >usingDefaultComparison (). lessThanOrEqualTo ( value );
14491 }
14592}
0 commit comments