1+ /* Copyright (c) 2000-2009 hamcrest.org
2+ */
13package org .hamcrest .number ;
24
35import org .hamcrest .Description ;
68import org .hamcrest .TypeSafeMatcher ;
79
810public class OrderingComparison <T extends Comparable <T >> extends TypeSafeMatcher <T > {
9- private final T value ;
11+ private static final int LESS_THAN = -1 ;
12+ private static final int GREATER_THAN = 1 ;
13+ private static final int EQUAL = 0 ;
14+ private final T expected ;
1015 private final int minCompare , maxCompare ;
1116
12- private OrderingComparison (T value , int minCompare , int maxCompare ) {
13- this .value = value ;
17+ private OrderingComparison (T expected , int minCompare , int maxCompare ) {
18+ this .expected = expected ;
1419 this .minCompare = minCompare ;
1520 this .maxCompare = maxCompare ;
1621 }
1722
1823 @ Override
19- public boolean matchesSafely (T other ) {
20- int compare = Integer .signum (value .compareTo (other ));
24+ public boolean matchesSafely (T actual ) {
25+ int compare = Integer .signum (actual .compareTo (expected ));
2126 return minCompare <= compare && compare <= maxCompare ;
2227 }
2328
2429 @ Override
25- public void describeMismatchSafely (T item , Description mismatchDescription ) {
26- mismatchDescription .appendValue (value ) .appendText (" was " )
27- .appendText (comparison (value .compareTo (item ))).appendText (" " ).appendValue (item );
30+ public void describeMismatchSafely (T actual , Description mismatchDescription ) {
31+ mismatchDescription .appendValue (expected ) .appendText (" was " )
32+ .appendText (comparison (actual .compareTo (expected )))
33+ .appendText (" " ).appendValue (actual );
2834 };
2935
3036 public void describeTo (Description description ) {
3137 description .appendText ("a value " ).appendText (comparison (minCompare ));
3238 if (minCompare != maxCompare ) {
3339 description .appendText (" or " ).appendText (comparison (maxCompare ));
3440 }
35- description .appendText (" " ).appendValue (value );
41+ description .appendText (" " ).appendValue (expected );
3642 }
3743
3844 private String comparison (int compare ) {
39- if (compare > 0 ) {
40- return "less than" ;
41- }
42- else if (compare == 0 ) {
43- return "equal to " ;
44- }
45- else {
46- return "greater than" ;
47- }
45+ if (compare == EQUAL ) {
46+ return "equal to" ;
47+ } else if (compare > EQUAL ) {
48+ return "greater than" ;
49+ } else {
50+ return "less than" ;
51+ }
4852 }
4953
5054 /**
5155 * Is value = expected?
5256 */
5357 @ Factory
5458 public static <T extends Comparable <T >> Matcher <? super T > comparesEqualTo (T value ) {
55- return new OrderingComparison <T >(value , 0 , 0 );
59+ return new OrderingComparison <T >(value , EQUAL , EQUAL );
5660 }
5761
5862 /**
5963 * Is value > expected?
6064 */
6165 @ Factory
6266 public static <T extends Comparable <T >> Matcher <? super T > greaterThan (T value ) {
63- return new OrderingComparison <T >(value , - 1 , - 1 );
67+ return new OrderingComparison <T >(value , GREATER_THAN , GREATER_THAN );
6468 }
6569
6670 /**
6771 * Is value >= expected?
6872 */
6973 @ Factory
7074 public static <T extends Comparable <T >> Matcher <? super T > greaterThanOrEqualTo (T value ) {
71- return new OrderingComparison <T >(value , - 1 , 0 );
75+ return new OrderingComparison <T >(value , EQUAL , GREATER_THAN );
7276 }
7377
7478 /**
7579 * Is value < expected?
7680 */
7781 @ Factory
7882 public static <T extends Comparable <T >> Matcher <? super T > lessThan (T value ) {
79- return new OrderingComparison <T >(value , 1 , 1 );
83+ return new OrderingComparison <T >(value , LESS_THAN , LESS_THAN );
8084 }
8185
8286 /**
8387 * Is value <= expected?
8488 */
8589 @ Factory
8690 public static <T extends Comparable <T >> Matcher <? super T > lessThanOrEqualTo (T value ) {
87- return new OrderingComparison <T >(value , 0 , 1 );
91+ return new OrderingComparison <T >(value , LESS_THAN , EQUAL );
8892 }
8993}
0 commit comments