I have the following class.
class MyClass<T>
It uses the following constructor.
MyClass(Comparator<T> comparator, Collection<? extends T> data)
And it has a field which is set in the constructor like so:
this.data = Collections.unmodifiableCollection(data);
In the special case where T implements Comparable, I don’t want to require that a comparator be passed in, since I can just use the natural ordering. So I thought I should be able to use this constructor:
public <T extends Comparable<T>> MyClass(Collection<T> data)
But there is apparently a type mismatch: cannot convert from Collection<T> to Collection<? extends T> in the assignment statement above. I’ve tried all sorts of things: adding more generic parameters, and so on, but none work. I seem unable to specify a bound that says: if you have a type T that implements Comparable, do the straightforward thing.
Any ideas?
Thanks.
Unfortunately I don’t think this kind of “if Comparable do this else do that” logic is possible with the Java type system.
You could split the Comparable and non-Comparable cases into separate classes and hide them behind an interface, something like this:
But this might result in a lot of duplicated code. Another option is to leave MyClass requiring a Comparator in its constructor, and build that comparator in the factory: