I'm currently working with the following types:
// A Field whose values are of type V.
interface Field<V> {
public V getValue();
}
// A Field whose values are of type V and that has a LabelProvider for those values.
interface LabelField<V> extends Field<V> {
public LabelProvider<V> getLabelProvider();
}
These come from a library and I cannot change them. This is probably not relevant, but a LabelProvider is basically a class that converts values of type V to Strings.
Now I want to create a class that provides an interface similar to this:
// A Wrapper class for Fields of values V
interface IWrapper<V, F extends Field<V>> {
public F getField();
public V getValue();
public String getLabel();
}
The class will wrap a Field, and let its users retrieve the value from the field, but also provide other functionalities, including retrieving the label. Here's what I have:
public class Wrapper<V, F extends Field<V>> {
private F field;
private LabelProvider<V> labelProvider; // Converts from V to String
// Since we have no guarantee that F has a LabelProvider, one is explicitely supplied.
public Wrapper(F field, LabelProvider<V> labelProvider) {
this.field = field;
this.labelProvider = labelProvider;
}
private String getLabel() {
return labelProvider.getLabel(field.getValue());
}
public F getField() {
return field;
}
public V getValue() {
return field.getValue();
}
}
No problem until this point, although I'm not sure whether I need these two generic types. Ideally I would have prefered to just have Wrapper> and be able to access both F and V. I think that this is not possible, and that the above is the correct way to do this ?
Now here's the real issue.
I want to have a more specific constructor for the LabelField type, which would look like this:
public Wrapper(LabelField<V> labelField) {
// Use the field's own label provider
this(labelField, labelField.getLabelProvider());
}
In this case the labelprovider can be extracted from the field. But the compiler doesn't like this and I get the following error:
The constructor Wrapper<V,F>(LabelField<V>, LabelProvider<V>) is undefined
Even though this method, which uses the same types, compiles :
public static <X> void test(LabelField<X> lf) {
LabelProvider<X> lp = lf.getLabelProvider();
new Wrapper<X, LabelField<X>>(lf, lp);
}
Why doesn't the second constructor compile ?
Here's a SSCE : http://pastebin.com/UqLJhJA7