Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Joda instants extend the raw type Comparable?

Tags:

java

jodatime

Joda's AbstractInstant interface extends the raw type Comparable, instead of Comparable<AbstractInstant>, which seems to violate Java best practices. In particular, it means that I cannot use DateTime to parameterize a class like this:

class Foo<T extends Comparable<? super T>> {
    public int ct(T a, T b) {
        return a.compareTo(b);
    }
}

It was my understanding this kind of class was perfectly valid (it certainly works with Double, etc.). To get it to work with DateTime, though, I have litter my own code with the raw type and suppressed warnings:

@SuppressWarnings("unchecked")
class Foo<T extends Comparable> {
    public int ct(T a, T b) {
        return a.compareTo(b);
    }
}

There is a related question that suggests a workaround (wrapping the DateTime in another class for the purposes of comparison), but I don't see why that should be necessary. My question then is:

  1. Does anyone know why Joda is extending a raw type, or
  2. Is this a bug I should report to the library maintainers?
like image 394
Alice Purcell Avatar asked Dec 13 '10 12:12

Alice Purcell


1 Answers

JodaTime is designed to work on Java 1.4, and so does not use any Java 5 features, including generics.

So yes, you need to add that boilerplate warning suppression stuff in some cases.

like image 167
skaffman Avatar answered Nov 08 '22 19:11

skaffman