This post originated from an RSS feed registered with Java Buzz
by Wilfred Springer.
Original Post: Fluent Interface for Wicket
Feed Title: Distributed Reflections of the Third Kind
Feed URL: http://blog.flotsam.nl/feeds/posts/default/-/Java
Feed Description: Anything coming to my mind having to do with Java
Last week I learned Wicket. This week, I am looking at Wicket 1.4rc2. I remember having looked at what would have been Wicket 2.0 in the past. Wicket 1.4 introduces Generics again.
Now, the first time I saw the angle brackets appearing in our source code, I was in shock. Then I started to think... This could actually be good.
Now, looking at a simple example of how all of this work, you probably don't get all that excited. Admittedly, having the IDE and compiler helping you to add the proper type of validators is sort of ok, if you like that kind of thing. (I do!) But then again, the amount of brackets (type parameters) appearing in your code is not really cool. Especially for a framework that is proud of it's limited use of angle brackets. (XML, that is.)
Here's an example:
form.add(new TextField<Date>("whatever", new PropertyModel<Date>(event, "date")) .add(DateValidator.maximum(new Date())));
While I was thinking about this, I started to wonder if a fluent interface would be able to take care of all of this through type inference. And I think it does. I quickly prototyped a fluent FormBuilder, and this is what the above example looks alike now:
And all the brackets are magically gone again... Now don't be fooled by the simplicity here. The compiler and IDE will support you in exactly the same way as in the first code example. You will not be able to add an IValidator type of validator, as the screenshot below illustrates.
Now, I have to say that the current incarnation of this fluent API is less than great. One of the things I should have done already - but haven't done yet - is to be able to drop .forModelOfType(...) in case you pass the property type in the forProperty(...) construct. So something like this: .forProperty("date").ofType(Date.class).of(event). But that's details. In general, I think this could work. What do you think?