public class ToStringTransformer extends Object implements Transformer
A generic Transformer
implementation that transforms arbitrary
objects into formatted strings.
This implementation can be used for transforming data of the most common data types to be presented in form input fields (especially text fields). Based on the type of the passed in object a specific transforming method will be invoked. The following data types are supported:
java.util.Date
objects will be formatted using a
DateFormat
object for the current locale. With the
setDateFormatStyle()
the specific formatting style can be
defined (one of the constants defined by the DateFormat
class
like SHORT
, MEDIUM
, or FULL
.
The class will always use a date instance for doing the formatting. So if
other formats are needed (e.g. only the time portion or both date and time),
a different Transformer
implementation must be used.NumberFormat
object initialized for the current locale. For configuring the format a set
of properties is available matching the options of NumberFormat
(e.g. the grouping flag or the maximum number of fraction digits).NumberFormat
object, but no
fraction digits will be output.toString()
method will be
invoked.
The properties that configure the formatting can be set through the
bean-style set methods. When declaring an input element in a builder script
and assigning a Transformer
to it, it is also possible to
specify properties, which will then override the settings stored in the
object's properties. These properties have the same names as the bean
properties defined by this class, e.g. the groupingUsed
property corresponds to the setGroupingUsed()
method.
An instance of this Transformer
implementation can be shared
between multiple input components. It is possible to create a single instance
(e.g. using the dependency injection framework) and initialize it with
default settings. If single input components require different settings,
specific properties can be set for them overriding the defaults.
Modifier and Type | Field and Description |
---|---|
static String |
PROP_DATE_FORMAT_STYLE
Constant for the date format style property.
|
static String |
PROP_GROUPING_USED
Constant for the grouping used property.
|
static String |
PROP_MAXIMUM_FRACTION_DIGITS
Constant for the maximum fraction digits property.
|
static String |
PROP_MINIMUM_FRACTION_DIGITS
Constant for the minimum fraction digits property.
|
Constructor and Description |
---|
ToStringTransformer()
Creates a new instance of
ToStringTransformer . |
Modifier and Type | Method and Description |
---|---|
int |
getDateFormatStyle()
Returns the style to be used for formatting dates.
|
int |
getMaximumFractionDigits()
Returns the maximum number of fraction digits.
|
int |
getMinimumFractionDigits()
Returns the minimum number of fraction digits.
|
protected boolean |
isDecimalNumber(Object o)
Checks whether the passed in object is a decimal number.
|
boolean |
isGroupingUsed()
Returns a flag whether a grouping character is to be used when formatting
numbers.
|
protected boolean |
isIntegerNumber(Object o)
Checks whether the passed in object is an integer number.
|
void |
setDateFormatStyle(int dateFormatStyle)
Sets the style to be used for formatting dates.
|
void |
setGroupingUsed(boolean groupingUsed)
Sets the grouping used flag.
|
void |
setMaximumFractionDigits(int maximumFractionDigits)
Sets the maximum number of fraction digits.
|
void |
setMinimumFractionDigits(int minimumFractionDigits)
Sets the minimum number of fraction digits.
|
Object |
transform(Object o,
TransformerContext ctx)
Transforms the specified object.
|
protected Object |
transformDate(Date dt,
TransformerContext ctx)
Transforms a date object.
|
protected Object |
transformDecimalNumber(Object number,
TransformerContext ctx)
Transforms a decimal number object.
|
protected Object |
transformIntegerNumber(Object number,
TransformerContext ctx)
Transforms an integer number object.
|
protected Object |
transformNull(TransformerContext ctx)
Transforms a null object.
|
protected Object |
transformObject(Object o,
TransformerContext ctx)
Transforms an arbitrary object.
|
public static final String PROP_DATE_FORMAT_STYLE
public static final String PROP_MAXIMUM_FRACTION_DIGITS
public static final String PROP_MINIMUM_FRACTION_DIGITS
public static final String PROP_GROUPING_USED
public ToStringTransformer()
ToStringTransformer
.public int getDateFormatStyle()
public void setDateFormatStyle(int dateFormatStyle)
java.text.DateFormat
class.dateFormatStyle
- the style to be usedpublic int getMinimumFractionDigits()
public void setMinimumFractionDigits(int minimumFractionDigits)
minimumFractionDigits
- the new minimum number of fraction digitspublic int getMaximumFractionDigits()
public void setMaximumFractionDigits(int maximumFractionDigits)
maximumFractionDigits
- the new maximum number of fraction digitspublic boolean isGroupingUsed()
public void setGroupingUsed(boolean groupingUsed)
groupingUsed
- the grouping flagpublic Object transform(Object o, TransformerContext ctx) throws Exception
transform
in interface Transformer
o
- the object to be transformedctx
- the transformer contextException
- if an error occursprotected Object transformNull(TransformerContext ctx) throws Exception
ctx
- the transformer contextException
- if an error occursprotected Object transformDate(Date dt, TransformerContext ctx) throws Exception
dt
- the date to be formattedctx
- the transformer contextException
- if an error occursprotected Object transformDecimalNumber(Object number, TransformerContext ctx) throws Exception
number
- the number object to be formattedctx
- the transformer contextException
- if an error occursprotected Object transformIntegerNumber(Object number, TransformerContext ctx) throws Exception
number
- the number to be transformedctx
- the transformer contextException
- if an error occursprotected Object transformObject(Object o, TransformerContext ctx) throws Exception
toString()
method.o
- the object to be transformedctx
- the transformer contextException
- if an error occursprotected boolean isDecimalNumber(Object o)
o
- the object to checkprotected boolean isIntegerNumber(Object o)
java.lang.Number
. Because decimal numbers are checked
first, the remaining number objects are integers.o
- the object to checkCopyright © 2016 The JGUIraffe Team. All rights reserved.