I’m working on some code for a User Interface. In this code there is a generic interface like the following:
public interface DataValue<T>
{
public String getName();
public T getValue();
public String getDescription();
}
Now, this interface is implemented by many Enumerations across the code and it is used in a hover event controller for values in the interface that can be represented by the DataValue interface. I realized that there were some warnings in this controller so I unsuccessfully tried to find a way to remove them. The controller class looks like the following:
public class HintController implements ItemHoverHandler
{
private final Class<? extends Enum> theEnumClass;
public HintController(Class<? extends Enum> enumClass)
{
theEnumClass = enumClass;
}
@Override
@SuppressWarnings("unchecked")
public void onItemHover(ItemHoverEvent event)
{
FormItem formItem = event.getItem();
Enum<?> enumValue =
Enum.valueOf(theEnumClass, (String)formItem.getValue());
String prompt = (enumValue instanceof DataValue)
? getPromptForDataValue((DataValue)enumValue)
: null;
formItem.setPrompt(prompt);
}
private String getPromptForDataValue(DataValue<?> dataValue)
{
return "<b>" + dataValue.getName() + "</b><br /><br />"
+ dataValue.getDescription();
}
}
Of course this implementation has some rawType warnings which I am not able to fix. If I parametrize the field with ? wildcard it will show a bounds exception. My closes “attempt” was when I tried to change the type of the field and construction parameter tom something like this:
public class HintController implements ItemHoverHandler
{
private final Enum<? extends DataValue<?>> theEnumClass;
public HintController(Enum<? extends DataValue<?>>enumClass)
{
theEnumClass = enumClass;
}
@Override
public void onItemHover(ItemHoverEvent event)
{
FormItem formItem = event.getItem();
Enum<? extends DataValue<?>>enumValue =
Enum.valueOf(theEnumClass.getDeclaringClass(),formItem.getValue().toString());
String prompt = (enumValue instanceof DataValue)
? getPromptForDataValue((DataValue<?>)enumValue)
: null;
formItem.setPrompt(prompt);
}
private String getPromptForDataValue(DataValue<?> dataValue)
{
return "<b>" + dataValue.getName() + "</b><br /><br />"
+ dataValue.getDescription();
}
}
But of course, it failed because I couldn’t find a way to actually send a reference of that type, because they are enumerations and I can’t instantiate them. I think there should be a proper way to do this would be grateful if someone can help. Thanks
You could make the warnings go away by putting a real type parameter on the
HintController:Whether that is plausible given the impact on other classes which use it, i cannot say. I would imagine it would be – anywhere that creates one already has some idea about the type of the
enumClassconstructor parameter, which it could simply propagate to theHintController. Places that use it in other ways can always call it aHintController<?>.