2.17.2: 2011-06-17

net.sf.basedb.util.formatter
Class MultiFormatter

java.lang.Object
  extended by net.sf.basedb.util.formatter.MultiFormatter
All Implemented Interfaces:
Formatter<Object>

public class MultiFormatter
extends Object
implements Formatter<Object>

A generic formatter that can be used to format objects of mixed classes. To use it you register a formatter for each class you expect to use. A default formatter is used for objects with unregistered classes.

Null values return the empty string.

Version:
2.0
Author:
nicklas
Last modified
$Date: 2010-04-20 10:02:16 +0200 (Tue, 20 Apr 2010) $

Field Summary
private  boolean checkSuperclass
           
private  boolean decideOnFirst
           
private  Formatter<Object> defaultFormatter
           
private  Formatter formatter
           
private  Map<Class<?>,Formatter<?>> formatters
           
 
Constructor Summary
MultiFormatter(Formatter<Object> defaultFormatter, boolean checkSuperclass)
          Create a new multi formatter.
MultiFormatter(Formatter<Object> defaultFormatter, boolean checkSuperclass, boolean decideOnFirst)
          Create a new multi formatter.
 
Method Summary
 String format(Object value)
          Format a value by converting it to a string.
 Number parseString(String value)
          Parse a string and return a value of the correct type.
<T> void
registerFormatter(Class<? extends T> clazz, Formatter<T> formatter)
          Register a formatter for the specified class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultFormatter

private final Formatter<Object> defaultFormatter

checkSuperclass

private final boolean checkSuperclass

formatters

private final Map<Class<?>,Formatter<?>> formatters

decideOnFirst

private final boolean decideOnFirst

formatter

private Formatter formatter
Constructor Detail

MultiFormatter

public MultiFormatter(Formatter<Object> defaultFormatter,
                      boolean checkSuperclass)
Create a new multi formatter.

Parameters:
defaultFormatter - The formatter to use if the object's class isn't found among the registered formatters
checkSuperclass - If the superclass and interfaces of the object should be checked in case there is no exact match

MultiFormatter

public MultiFormatter(Formatter<Object> defaultFormatter,
                      boolean checkSuperclass,
                      boolean decideOnFirst)
Create a new multi formatter.

Parameters:
defaultFormatter - The formatter to use if the object's class isn't found among the registered formatters
checkSuperclass - If the superclass and interfaces of the object should be checked in case there is no exact match
decideOnFirst - If TRUE, the first non-null value decides which parent formatter to use. All subsequent calls to format(Object) uses the same formatetter. This is useful when you know that the formatter will only use the same class of objects, but not which class.
Since:
2.7.2
Method Detail

format

public String format(Object value)
Description copied from interface: Formatter
Format a value by converting it to a string.

Specified by:
format in interface Formatter<Object>
Parameters:
value - The value to format
Returns:
A formatted string

parseString

public Number parseString(String value)
Description copied from interface: Formatter
Parse a string and return a value of the correct type. A null input string should (unless otherwise documented by a specific formatter implementation) be allowed and may return null or any other "default" value.

Specified by:
parseString in interface Formatter<Object>
Parameters:
value - The string to parse
Returns:
An object

registerFormatter

public <T> void registerFormatter(Class<? extends T> clazz,
                                  Formatter<T> formatter)
Register a formatter for the specified class.

Parameters:
clazz - The class of objects to format with the formatter
formatter - The formatter

2.17.2: 2011-06-17