Skip to content

Commit fbaa7c8

Browse files
committed
Change generic signature of OrderingComparison to accept Comparable<? super T>
1 parent 40465aa commit fbaa7c8

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

hamcrest-library/src/main/java/org/hamcrest/number/OrderingComparison.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import static java.lang.Integer.signum;
1111

12-
public class OrderingComparison<T extends Comparable<T>> extends TypeSafeMatcher<T> {
12+
public class OrderingComparison<T extends Comparable<? super T>> extends TypeSafeMatcher<T> {
1313
private static final int LESS_THAN = -1;
1414
private static final int GREATER_THAN = 1;
1515
private static final int EQUAL = 0;
@@ -67,7 +67,7 @@ private static String asText(int comparison) {
6767
*
6868
*/
6969
@Factory
70-
public static <T extends Comparable<T>> Matcher<T> comparesEqualTo(T value) {
70+
public static <T extends Comparable<? super T>> Matcher<T> comparesEqualTo(T value) {
7171
return new OrderingComparison<T>(value, EQUAL, EQUAL);
7272
}
7373

@@ -85,7 +85,7 @@ public static <T extends Comparable<T>> Matcher<T> comparesEqualTo(T value) {
8585
*
8686
*/
8787
@Factory
88-
public static <T extends Comparable<T>> Matcher<T> greaterThan(T value) {
88+
public static <T extends Comparable<? super T>> Matcher<T> greaterThan(T value) {
8989
return new OrderingComparison<T>(value, GREATER_THAN, GREATER_THAN);
9090
}
9191

@@ -103,7 +103,7 @@ public static <T extends Comparable<T>> Matcher<T> greaterThan(T value) {
103103
*
104104
*/
105105
@Factory
106-
public static <T extends Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value) {
106+
public static <T extends Comparable<? super T>> Matcher<T> greaterThanOrEqualTo(T value) {
107107
return new OrderingComparison<T>(value, EQUAL, GREATER_THAN);
108108
}
109109

@@ -121,7 +121,7 @@ public static <T extends Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value)
121121
*
122122
*/
123123
@Factory
124-
public static <T extends Comparable<T>> Matcher<T> lessThan(T value) {
124+
public static <T extends Comparable<? super T>> Matcher<T> lessThan(T value) {
125125
return new OrderingComparison<T>(value, LESS_THAN, LESS_THAN);
126126
}
127127

@@ -139,7 +139,7 @@ public static <T extends Comparable<T>> Matcher<T> lessThan(T value) {
139139
*
140140
*/
141141
@Factory
142-
public static <T extends Comparable<T>> Matcher<T> lessThanOrEqualTo(T value) {
142+
public static <T extends Comparable<? super T>> Matcher<T> lessThanOrEqualTo(T value) {
143143
return new OrderingComparison<T>(value, LESS_THAN, EQUAL);
144144
}
145145
}

hamcrest-library/src/test/java/org/hamcrest/number/OrderingComparisonTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ public void testComparesBigDecimalsWithDifferentScalesCorrectlyForIssue20() {
6868
public void testComparesCustomTypesWhoseCompareToReturnsValuesGreaterThatOne() {
6969
assertThat(new CustomInt(5), lessThan(new CustomInt(10)));
7070
}
71+
72+
public void testComparesCustomTypesWhichExtendComparableClasses() {
73+
assertThat(new CustomExtendedInt(5), lessThan(new CustomExtendedInt(10)));
74+
}
7175

72-
private static final class CustomInt implements Comparable<CustomInt> {
76+
private static class CustomInt implements Comparable<CustomInt> {
7377
private final int value;
7478
public CustomInt(int value) {
7579
this.value = value;
@@ -79,4 +83,10 @@ public int compareTo(CustomInt other) {
7983
return value - other.value;
8084
}
8185
}
86+
87+
private static class CustomExtendedInt extends CustomInt {
88+
public CustomExtendedInt(int value) {
89+
super(value);
90+
}
91+
}
8292
}

0 commit comments

Comments
 (0)