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
Modifier and TypeFieldDescriptionprivate final boolean
private final boolean
-
Constructor Summary
ConstructorDescriptionMultiFormatter
(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
Modifier and TypeMethodDescriptionFormat a value by converting it to a string.getFormatter
(Object value) 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.toExcelValue
(Object value) Convert and format the given value to an Excel value.
-
Field Details
-
defaultFormatter
-
checkSuperclass
private final boolean checkSuperclass -
formatters
-
decideOnFirst
private final boolean decideOnFirst -
formatter
-
-
Constructor Details
-
MultiFormatter
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 Details
-
format
Description copied from interface:Formatter
Format a value by converting it to a string. -
parseString
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
-
getFormatter
-
registerFormatter
Register a formatter for the specified class.- Parameters:
clazz
- The class of objects to format with the formatterformatter
- The formatter
-
toExcelValue
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>
-