 |
Gooey Formatter
Posted by evanx on December 14, 2006 at 02:18 AM | Comments (4)
We implement a singleton for formatting objects into strings, where
custom formatters ie. for specific types, are registered with the singleton.
Click here to read "Gooey Formatter, an objective expose" ;)
A part of "Gooey Beans, a trilogy in 42 parts"
Code Snippet
Our singleton is implemented as follows.
public class QDefaultFormatter implements QFormatter {
public Map<Class, QFormatter> formatterMap = new HashMap();
...
public void register(Class type, QFormatter formatter) {
formatterMap.put(type, formatter);
}
public void register(QLabelMapFormatter formatter) {
QResourceMap resourceMap = new QResourceMap(formatter.getType());
resourceMap.configureEnumLabelMap(formatter.getType(),
formatter.getLabelMap());
formatterMap.put(formatter.getType(), formatter);
}
...
public String format(Object object) {
if (object == null) return "";
...
QFormatter formatter = formatterMap.get(object.getClass());
if (formatter != null) return formatter.format(object);
return object.toString();
}
...
}
where formatterMap is our registry map eg. for QLabelMapFormatter instances, which
map enum values to labels from our resource bundle.
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Hi, Evan; with your other excellent and deep work on JavaBeans, I have to assume you know about the PropertyEditor and PropertyEditorManager classes. Is there a reason that they would not fit your needs? The only one I can think of is that they require a two-part invocation:editor.setObject(object);
final String s = editor.getAsText();Care to elaborate?
Posted by: ljnelson on December 14, 2006 at 09:58 AM
-
ljnelson, thanks for that info, i didn't know about PropertyEditor so i will check it out :)
Posted by: evanx on December 15, 2006 at 12:15 AM
-
Yep; PropertyEditors have all kinds of support in all kinds of products. They've emerged as a faintly ugly but powerful standard way of taking arbitrary objects and representing them as Strings, and vice versa.
While you're there, have a look at the Customizer interface as well.
Lastly, there is the more special purpose java.text.Format class, which IBM (well, Taligent; remember them?) wrote originally (and it shows) and intended for internationalization. But there is nothing that prevents you from using this class as well. You can of course make a Format that is backed by a PropertyEditor or vice versa.
Posted by: ljnelson on December 15, 2006 at 05:39 AM
-
i must confess that QDefaultFormatter is the messiest class i have. I'm getting it neater lately by splitting it up, eg. loggerFormatter, collectionFormatter, exceptionFormatter. That's why i wrote about it now and why i was putting it off before - because only recently i cleaned it up enough to write about it.
Because before i'd tend to dump everything related to formatting, in the formatter singleton, whether for the developer eg. logging, exceptions and collections (eg. of arguments eg. for logging in general, and logging exceptions), or for the end-user, and it so spins out of control quite quickly.
I will try to start using PropertyEditor for user displayable stuff, since that seems the standard support for this.
For exceptions and logging, i have loggerFormatter, which does extend java.util.logging.Formatter, but actually i mostly use an extended QLogRecord which has much more stuff than it's superclass java.util.logging.LogRecord - eg. it might contain an exception reference, and collection of arguments related to the exception - eg. for formatting into GooeyConsole which is an HTML JTextPane.
Posted by: evanx on December 15, 2006 at 09:31 AM
|