public final class Formatter extends Object
Objects to their String representation.
This class contains a set of format functions that can be registered using
registerFormatFunction(Class, Function). A format function is defined by a Class (i.e.
the class to format), and a Function that takes an instance of it as input and returns a String.
Objects can be formatted using format(Object), that retrieves the format function
associated to the Object and applies it.
Xatkit embeds a Default Formatter that maps Objects to their toString() method.
Custom platforms can register new format functions to the default formatter, or define a new formatter
using XatkitCore.registerFormatter(String, Formatter).
| Constructor and Description |
|---|
Formatter()
Constructs the
Formatter and initializes its format functions. |
| Modifier and Type | Method and Description |
|---|---|
String |
format(Object obj)
Formats the provided
obj. |
<T> void |
registerFormatFunction(Class<T> clazz,
Function<T,String> function)
Registers the provided
function to the given clazz. |
public <T> void registerFormatFunction(Class<T> clazz, Function<T,String> function)
function to the given clazz.
If the Formatter already contains a format function for the provided clazz it is
erased and a warning is printed.
public String format(Object obj)
obj.
This method searches for a registered format function associated to obj's Class. If
their is no function for this Class the method will look for functions associated to its super-classes
and interfaces, recursively. The function associated to the Class with the closest distance to the
obj's one is returned, in order to support polymorphic formatting.
Note: if there is no function associated to the obj's Class hierarchy this method will
return the result of applying the default Object format function.
obj - the Object to formatCopyright © 2020 SOM Research Lab. All rights reserved.