PDA

View Full Version : Component.getModel() should use wildcard return value



Grandiosa
1 Jul 2008, 5:19 AM
Shouldn't this method on Component


public ModelData getModel() {
return model;
}
instead be


public ? extends ModelData getModel() {
return model;
}
I have a typed Store<MyModel> and in my tablelistener I want to get a record from the store, like this:


Record record = store.getRecord(item.getModel()); // item is selected TableItem
But this fails since the the getRecord expects a MyModel instance, so a cast is needed:


Record record = store.getRecord((MyModel)item.getModel());

zaccret
3 Jul 2008, 4:26 AM
You cannot do :

public ? extends ModelData getModel() {
return model;
}This is incorrect. It could be instead :

public T getModel() {
return model;
}and add the <T extends ModelData> parameter on Component class. Darell, what would you think of that ? Not too restrictive ?

Grandiosa
3 Jul 2008, 4:40 AM
Oops, one should never write code directly in the wiki-editor... I had this construction in mind


public List<? extends ModelData> getModel() {
...
}
which works.
But components have one model only so i second your suggestion to parametrize the Component class.

zaccret
16 Jul 2008, 4:56 AM
By the way, I think we (users) should avoid to use the Component.get/setModel methods. If you use a table binder, try binder.getSelection() instead.