1
+ /* Copyright (c) 2000-2009 hamcrest.org
2
+ */
1
3
package org .hamcrest .number ;
2
4
3
5
import org .hamcrest .Description ;
6
8
import org .hamcrest .TypeSafeMatcher ;
7
9
8
10
public 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 ;
10
15
private final int minCompare , maxCompare ;
11
16
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 ;
14
19
this .minCompare = minCompare ;
15
20
this .maxCompare = maxCompare ;
16
21
}
17
22
18
23
@ 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 ));
21
26
return minCompare <= compare && compare <= maxCompare ;
22
27
}
23
28
24
29
@ 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 );
28
34
};
29
35
30
36
public void describeTo (Description description ) {
31
37
description .appendText ("a value " ).appendText (comparison (minCompare ));
32
38
if (minCompare != maxCompare ) {
33
39
description .appendText (" or " ).appendText (comparison (maxCompare ));
34
40
}
35
- description .appendText (" " ).appendValue (value );
41
+ description .appendText (" " ).appendValue (expected );
36
42
}
37
43
38
44
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
+ }
48
52
}
49
53
50
54
/**
51
55
* Is value = expected?
52
56
*/
53
57
@ Factory
54
58
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 );
56
60
}
57
61
58
62
/**
59
63
* Is value > expected?
60
64
*/
61
65
@ Factory
62
66
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 );
64
68
}
65
69
66
70
/**
67
71
* Is value >= expected?
68
72
*/
69
73
@ Factory
70
74
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 );
72
76
}
73
77
74
78
/**
75
79
* Is value < expected?
76
80
*/
77
81
@ Factory
78
82
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 );
80
84
}
81
85
82
86
/**
83
87
* Is value <= expected?
84
88
*/
85
89
@ Factory
86
90
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 );
88
92
}
89
93
}
0 commit comments