Skip to main content

Gooey Formatter

Posted by evanx on December 14, 2006 at 2:18 AM PST

We implement a singleton for formatting objects into strings, where
custom formatters ie. for specific types, are registered with the singleton.

href="http://aptframework.dev.java.net/gooey/formatter.html">
align="left" hspace="8"/>

style="text-decoration: none;">



Code Snippet

Our singleton is implemented as follows.

public class QDefaultFormatter implements QFormatter {
    public Map 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.


style="text-decoration: none;">
style="text-decoration: none;">
style='text-decoration: none;'>
Related Topics >>