J
Jason Cavett
I'm having some issues with generics and polymorphism. I thought this
was possible in Java - maybe someone can clear up what I'm doing
wrong. Basically, when I actually try to use the preference, the code
will not compile and I get the following error. How can I do what I'm
trying to do?
Here is the code that has the error:
PreferencesEnum.DERIVED_PREFERENCE.getPreference().setValue(new
String());
The error is:
The method setValue(capture#2-of ? extends Object) in the type
Preference<capture#2-of ? extends Object> is not applicable for the
arguments (String)
Thanks,
Jason
--- CLASS LISTINGS ---
I have an enum:
PreferencesEnum {
DERIVED_PREFERENCE(new DerivedPreference());
private final Preference<? extends Object> pref;
private PreferencesEnum(Preference<? extends Object> pref) {
this.pref = pref;
}
public Preference<? extends Object> getPreference() {
return pref;
}
}
And I have the generic Preference:
public abstract class Preference<E extends Object> {
// provides access to the preferences per application, per user
protected static Preferences prefs =
Preferences.userNodeForPackage(Main.class);
/**
* Default constructor.
*/
public Preference() {
}
/**
* Perform a refresh when the preferences change.
*/
public abstract void refresh();
/**
* Set the value of the preference.
*
* @param value
* the value to set
*/
public abstract void setValue(E value);
/**
* Get the value of the preference.
*
* @return the associated preference value
*/
public abstract E getValue();
}
And here's a derived preference:
public class DerivedPreference extends Preference<String> {
private static final String KEY = "derived";
private static final String DEFAULT = "DEFAULT VALUE";
/**
* Default constructor
*/
public DerivedPreference() {
super();
}
@Override
public String getValue() {
return prefs.get(DerivedPreference.KEY,
DerivedPreference.DEFAULT);
}
@Override
public void refresh() {
}
@Override
public void setValue(String value) {
prefs.put(DerivedPreference.KEY, value);
}
}
was possible in Java - maybe someone can clear up what I'm doing
wrong. Basically, when I actually try to use the preference, the code
will not compile and I get the following error. How can I do what I'm
trying to do?
Here is the code that has the error:
PreferencesEnum.DERIVED_PREFERENCE.getPreference().setValue(new
String());
The error is:
The method setValue(capture#2-of ? extends Object) in the type
Preference<capture#2-of ? extends Object> is not applicable for the
arguments (String)
Thanks,
Jason
--- CLASS LISTINGS ---
I have an enum:
PreferencesEnum {
DERIVED_PREFERENCE(new DerivedPreference());
private final Preference<? extends Object> pref;
private PreferencesEnum(Preference<? extends Object> pref) {
this.pref = pref;
}
public Preference<? extends Object> getPreference() {
return pref;
}
}
And I have the generic Preference:
public abstract class Preference<E extends Object> {
// provides access to the preferences per application, per user
protected static Preferences prefs =
Preferences.userNodeForPackage(Main.class);
/**
* Default constructor.
*/
public Preference() {
}
/**
* Perform a refresh when the preferences change.
*/
public abstract void refresh();
/**
* Set the value of the preference.
*
* @param value
* the value to set
*/
public abstract void setValue(E value);
/**
* Get the value of the preference.
*
* @return the associated preference value
*/
public abstract E getValue();
}
And here's a derived preference:
public class DerivedPreference extends Preference<String> {
private static final String KEY = "derived";
private static final String DEFAULT = "DEFAULT VALUE";
/**
* Default constructor
*/
public DerivedPreference() {
super();
}
@Override
public String getValue() {
return prefs.get(DerivedPreference.KEY,
DerivedPreference.DEFAULT);
}
@Override
public void refresh() {
}
@Override
public void setValue(String value) {
prefs.put(DerivedPreference.KEY, value);
}
}