Menu

#172 ReadableInstant extends Comparable and not Comparable<ReadableInstant>, but the implementation behaves as the latter

v1.0_(example)
open
nobody
7
2014-09-05
2013-08-30
TWiStErRob
No

Let me start with code (I used Date as a comparison, but anything comparable would do, like String, Integer, ...):

:::java
java.util.Date normalDate = new java.util.Date();
org.joda.time.DateTime jodaDate = new new org.joda.time.DateTime();
org.junit.Assert.assertThat(normalDate, org.hamcrest.Matchers.equalsTo(normalDate));
org.junit.Assert.assertThat(jodaDate, org.hamcrest.Matchers.equalsTo(jodaDate));
org.junit.Assert.assertThat(normalDate, org.hamcrest.Matchers.lessThan(normalDate));
org.junit.Assert.assertThat(jodaDate, org.hamcrest.Matchers.lessThan(jodaDate));

at #4 (jodaDate/lessThan) I get weird warning message in Eclipse:

Type safety: The expression of type Matcher needs unchecked conversion to conform to Matcher<? super DateTime>

My guess it is because ReadableInstant extends Comparable and not Comparable<readableinstant>.
int compareTo(Object readableInstant); will throw a ClassCastException if it's not instanceof ReadableInstant which makes it pointless to implement the raw Comparable interface.
That check can be made by the compiler at compile time. In my opinion it won't break any working code because if someone ever calls it with anything other than ReadableInstant instance, they would get an exception anyway, which no-one wants I guess (maybe your unit tests...).</readableinstant>

Would it be possible to change the interface extension to the generic one?
It will give a warning for anyone who tries to use it against any generic interface which expects Comparable<t>, which is pretty annoying if they want "warning-less" code. SuppressWarnings is rather pointless, hopefully I'll only have to use it at a few places. (For example equalsTo in Hamcrest does not give a warning)</t>

Discussion

  • TWiStErRob

    TWiStErRob - 2013-08-31
     

Log in to post a comment.

MongoDB Logo MongoDB