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 Details

    • defaultFormatter

      private final Formatter<Object> defaultFormatter
    • checkSuperclass

      private final boolean checkSuperclass
    • formatters

      private final Map<Class<?>,Formatter<Object>> formatters
    • decideOnFirst

      private final boolean decideOnFirst
    • formatter

      private Formatter<Object> formatter
  • Constructor Details

    • 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 Details

    • 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
    • getFormatter

      private Formatter<Object> getFormatter(Object value)
    • 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
    • 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 interface ExcelFormatter<Object,Object>