Although I use here Hamcrest library, it is probably not an Hamcrest issue but pure Java...
I have the following method:
public static void isGreaterThan(int valueToValidate, int minExpectedVal) {
MatcherAssert.assertThat(valueToValidate, greaterThan(minExpectedVal));
}
I'd like to generalize it to something like:
public static <T> void isGreaterThan(T valueToValidate, T minExpectedVal) {
MatcherAssert.assertThat(valueToValidate, greaterThan(minExpectedVal));
}
or
public static void isGreaterThan(Number valueToValidate, Number minExpectedVal) {
MatcherAssert.assertThat(valueToValidate, greaterThan(minExpectedVal));
}
Hamcrest greaterThan signature is:
<T extends Comparable<T>> Matcher<T> greaterThan(T value)
and assertThat signature:
<T> void assertThat(T actual, Matcher<? super T> matcher)
However, on both cases, I get an error on minExpectedVal saying that it cannot be applied as T.
How can I overcome this?
java.lang.Numberrepresents. It is not a generic number type, as the name would suggest. The name is misleading, as it only represents an object that can be converted to any of the primitive numeric types. That conversion is not lossless and that is the reason why it does not implementComparable. And that is why it really cannot be used to represent a number as one would expect.