Sunday, July 23, 2006

Java 7 improvements

Since now seems the time to request new Java 7 improvements (see for example Bag O' Tricks) here are my requests:
  • Type aliases I really hate having to type code like
    private Map<String, List<StringFormatter<DomainThing>>> map = new HashMap<String, List<StringFormatter<DomainThing>>>();
    It is so non-DRY that my fingers ache every time I see code like this.

    Here is an alternative:

    private class DomainFormatterList :     List<StringFormatter<DomainThing>>;
    private Map<String, DomainFormatterList> map =
        new HashMap<String, DomainFormatterList>();
    Still some repetition, but a lot better already. Any ideas for improvement?
  • A non-static String.format method.
    How often did I start with a literal String only to find out I had to move the cursor back to the left and put "String.format(" in front of it? It would have been easier if the format method were an instance method. Compare:
    String.format("Message for %d %s items", 5, "red");
    "Message for %d %s items".format(5, "red");
Please share you thoughts.

Saturday, July 22, 2006

Expert Spring MVC and Web Flow - Book review

I just finished another book: Expert Spring MVC and Web Flow. This time I really enjoyed reading the book. The tutorial and the example applications distributed with Spring are excellent, but to get a good feel for the complete Spring MVC this book is a gem.

Here are some of its strong points:
- The book has clear subjects per chapter (an obvious requirement, but not always met).
- Your time is not wasted; the language is concise without being to cryptic. Really, the books reads as if it has been reviewed and corrected by at least 30 knowledgeable persons.
- You're not coerced in how you must use Spring MVC, but you're told how to make the appropriate choices from the options Spring MVC provides.
- The book not only shows you how stuff works, but also why, learning you valuable design principles that stretch way beyond Spring MVC.
- The appendix on DWR integration is short but very sweet.

Some minor nitpicking:
- The books regularly refers to Spring 1.3, though by the time of writing it was already known that version 1.2 would be followed by 2.0 (the chapters on Spring Web Flow have this correct).
- There is no information on the new form tag libraries.
- There is no mention of Spring Portlet MVC, except for a short reference in the chapters on Spring Web Flow. (Note that you'll find switching from Spring MVC to Spring Portlet MVC quite easy.)

If you are considering learning another web application framework: buy this book.

Tuesday, July 11, 2006

Writing CSV from Java

I recently tried to find a open source Java CSV writer released under a commercially friendly license. To my surprise I could not find one.

CSVs are still the easy solution when you want to export to any spreadsheet program. The only big problem is getting it right for Excel as Microsoft has its own opinion about CSV.

Anyway, since I did not find any, I wrote my own. It has the following features:
- streaming output (big files are no problem)
- uses Java5 printf formatting for flexible column formats
- settable separation character, locale and encoding
- verifies numer of columns

I also wrote a View implementation for use in a Spring MVC environment.

Here are some possible improvements:
- more automatic support for different Excel versions (dependent on locale)
- introduce a CsvWriterFactory to enable easier Spring wiring

I don't think my employer would minds if this went opensource. For example with an Apache license. Anybody interested?

Update 2007-10-23: I think our troubles are finally over: meet Super CSV!