« Return to Thread: generics in Wicket

Re: generics in Wicket

by Ryan Holmes :: Rate this Message:

Reply to Author | View in Thread


On Mar 19, 2007, at 12:58 AM, Eelco Hillenius wrote:
>
> Do you have an idea that works without having to generify Component?
>

Not sure if you would put this in the "idea that works" category, but  
I was thinking of something like this:

public class Component {
        private IModel model;
       
        public <T> Component(final String id, final IModel<T> model) {
                this.model = model;
        }
       
        public <T> IModel<T> getModel() {
                return this.model;
        }
       
        public <T> void setModel(IModel<T> model) {
                this.model = model;
        }
}

This accommodates generic models but doesn't enforce type consistency  
between getModel and setModel. In practice, the types would usually  
be inferred:

IModel<Integer> model = component.getModel();

so it's easy to use and does eliminate casting, but get/set model  
inconsistency would only be caught at runtime (unless you use the  
full generic method syntax with a type parameter)

I don't know -- it doesn't really buy you much. This doesn't work:

Integer i = component.getModel().getObject();

I also played around with the idea of a generic "ModelHolder" object  
inside of Component so at least getModel() and setModel() would use  
the same type parameter, but again the get/set model methods on  
Component itself can't pick that type up.

<sigh> Oh well, maybe it's just taking me longer to come to the same  
conclusion you did.

-Ryan

 « Return to Thread: generics in Wicket