Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jdt-core-dev] Type-safe compareTo--how to use?

Does anyone know how to use typesafe compareTo? I'm moving all my code
to generics, and I'm getting a warning in a class where I've written a
custom comparator as follows.

    public static final Comparator COMPARABLE_COMPARATOR = new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) o1).compareTo(o2);
        }
    };

The warning is "Unsafe type operation: Should not invoke the method
compareTo(T) of raw type Comparable. References to generic type
Comparable<T> should be parameterized". I have no idea what to do with
this. I've tried Comparable<Object>, but then I get a warning that it
won't be able to typecheck my code.

On a similar note, I have another strange warning invlolving this
class: I have an accessor for that Comparator called getComparator
which is as follows:

    protected Comparator getComparator(int column) {
        Class columnType = tableModel.getColumnClass(column);
        Comparator comparator = columnComparators.get(columnType);
        if (comparator != null) {
            return comparator;
        }
        if (Comparable.class.isAssignableFrom(columnType)) {
            return COMPARABLE_COMPARATOR;
        }
        return LEXICAL_COMPARATOR;
    }

And gives the warning "Unsafe type operation: The method
isAssignableFrom(Class<?>) in the type Class<Comparable> should not be
applied for the arguments (Class). References to generic types should
be parameterized". I have no idea what to do here either.


Back to the top