Package net.sf.basedb.util.formatter
Class MultiFormatter
- java.lang.Object
-
- net.sf.basedb.util.formatter.MultiFormatter
-
- All Implemented Interfaces:
ExcelFormatter<Object,Object>
,Formatter<Object>
public class MultiFormatter extends Object implements Formatter<Object>, ExcelFormatter<Object,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: 2019-03-14 07:52:48 +0100 (tors, 14 mars 2019) $
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
checkSuperclass
private boolean
decideOnFirst
private Formatter<Object>
defaultFormatter
private Formatter<Object>
formatter
private Map<Class<?>,Formatter<Object>>
formatters
-
Constructor Summary
Constructors Constructor Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
format(Object value)
Format a value by converting it to a string.private Formatter<Object>
getFormatter(Object value)
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.ExcelValue<Object>
toExcelValue(Object value)
Convert and format the given value to an Excel value.
-
-
-
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 formatterscheckSuperclass
- 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 formatterscheckSuperclass
- If the superclass and interfaces of the object should be checked in case there is no exact matchdecideOnFirst
- If TRUE, the first non-null value decides which parent formatter to use. All subsequent calls toformat(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.
-
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 interfaceFormatter<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 formatterformatter
- The formatter
-
toExcelValue
public ExcelValue<Object> toExcelValue(Object value)
Description copied from interface:ExcelFormatter
Convert and format the given value to an Excel value. If the value is null the implementation may return null to create an empty cell or it may return another non-null value.- Specified by:
toExcelValue
in interfaceExcelFormatter<Object,Object>
-
-