Converting objects from A to B and back - there ought to be a library
Posted by timboudreau on May 1, 2009 at 2:33 PM EDT
One pattern that is an incredibly frequent recurring theme is converting an Object of type A into an object of type B for something that understands B to consume. Tons of libraries have something like this embedded in them - beans binding, pretty much anything that validates strings. If there were ever something that deserves a simple common library, it's this.
I was just looking at Fabrizio Guiduci's Better Beans Binding project, which took me to the original beans binding project. Embedded in it is a mini-framework for converting objects back and forth between types - an application provides a List, a JList wants a ListModel.
Last week as part of my new Simple Validation project I wrote into it a mini framework for object conversion and a converter registry:
public abstract class Converter<From,To> {
public abstract <To> convert (From from);
public static <From,To> Converter<From,To> find (Class<From> from, Class<To> to);
}
Java Beans property editors are de-facto Object -> String -> Object convertors - I've long thought that one of the bigger messes in the beans spec (there are so many!) is the fact that type conversion is a completely orthagonal concern that should have been factored out.
And on it goes - I'm sure there are many, many more examples. All of the subclasses of java.text.Format in the JDK count. Pretty much anything that communicates via HTTP and uses an object-oriented language is one.
Perhaps something like this already exists and I don't know about it.
But what I would really like to see is a simple framework that solves the problem, that the rest of these projects could use - it's really an area where there is endless spinning of wheels. The characteristics something like that would be:
- Simple - if the framework exposes more API classes than you can count on your fingers, it's overcomplicated
- Converters are injected - i.e. you ask for a Converter for Date -> String -> Date; you do not ever refer to the actual implementation type of the converter unless you are writing it yourself. Getting a stable of converters for the preexisting types you need should be as simple as putting the right JAR on the classpath. Only if there are really multiple right ways to approach the problem should you need to instantiate a converter yourself.
- Does not assume bi-directionality (the Beans Binding version does this, but it needs that for some cases) - i.e. you have
Converter<From,To> { To convert (From from); }andBidiConverter<From,To> extends Converter<From,To< { From reverseConvert (To to); }or maybe skip the bi-directionality assumption altogether. - Comes with an optional set of useful converters for common things such as JDK classes
- Conversion errors are unchecked exceptions with meaningful error messages, and few but useful-if-present exception subtypes
Related Topics >>
Blog Links >>
- Login or register to post comments
- Printer-friendly version
- timboudreau's blog
- 1088 reads






Comments
by timboudreau - 2009-05-05 23:17
Thanks for the links! I'll take a look at both of these...by rondeth - 2009-05-04 08:35
FWIW, we tried Dozer and found some features lacking--and the source was a relative mess at the time. We had much better luck with Morph: http://morph.sourceforge.net/by amusi - 2009-05-02 03:57
Maybe Dozer would help: http://dozer.sourceforge.net/ It's a framework for copying information from a to b, configurable with xml files, using convention over configuration. It really can do a lot of things, I've been using it for very simple things though. regards, amusiby ljnelson - 2009-05-01 12:45
I always wanted this to be implemented properly: http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Beans.html#getInstanceOf(java.lang.Object,%20java.lang.Class) Best, Laird