T
- the type handled by this transformerpublic abstract class NumberTransformerBase<T extends Number> extends Object implements Transformer, Validator
An abstract base class for transformers and validators for numbers.
This base class already provides the major part of functionality for
validating numeric input and transforming strings to number objects. Concrete
sub classes are responsible for the creation of a
java.text.NumberFormat
object that is used for parsing the
user input. The class also supports certain semantic checks, especially
whether an entered number lies in a specified interval.
This class makes use of Java generics to be independent on a concrete number type. The returned (transformed) object will be of this type, and also the specified minimum or maximum values must use this type.
The following properties are supported by this class:
Property | Description | Default |
---|---|---|
minimum | Here the minimum value can be defined. Entered numbers are checked to be greater or equal than this number. If this property is undefined, no minimum checks will be performed. | undefined |
maximum | Here the maximum value can be defined. Entered numbers are checked to be less or equal than this number. If this property is undefined, no maximum checks will be performed. | undefined |
Validation of user input can fail for multiple reasons. The following table lists the possible error messages:
Message key | Description | Parameters |
---|---|---|
|
The passed in string cannot be parsed to a number object. | {0} = the input string |
|
The entered number is too small. This error message is returned if the
number is less than the specified minimum number and no maximum number was
specified. (If both a minimum and a maximum number are specified, the error
code
is used.) |
{0} = the minimum number |
|
The entered number is too big. This error message is returned if the
number is greater than the specified maximum number and no minimum number was
specified. (If both a minimum and a maximum number are specified, the error
code
is used.) |
{0} = the maximum number |
|
The entered number is not in the interval spanned by the minimum and the
maximum value. If both a minimum and a maximum are specified and the entered
number does not meet these constraints, this error message is produced rather
than one of
or
. |
{0} = the minimum value, {1} = the maximum value |
The class implements both the
and
Transformer
Validator
interfaces. It is safe to use an instance
concurrently as transformer and validator for the same or multiple input
fields.
Modifier and Type | Field and Description |
---|---|
protected static String |
PROP_MAXIMUM
Constant for the maximum property.
|
protected static String |
PROP_MINIMUM
Constant for the minimum property.
|
Constructor and Description |
---|
NumberTransformerBase() |
Modifier and Type | Method and Description |
---|---|
protected abstract T |
convert(Number n)
Converts the specified number to the target type supported by this
transformer.
|
protected abstract NumberFormat |
createFormat(Locale locale)
Creates the format object for parsing a number.
|
protected ValidationResult |
errorResult(String errorKey,
TransformerContext ctx,
Object... params)
Creates a validation result if an error occurred.
|
protected abstract T |
fetchProperty(org.apache.commons.configuration.Configuration config,
String property,
T defaultValue)
Fetches a property of the supported type from the specified configuration
object.
|
T |
getMaximum()
Returns the maximum value.
|
T |
getMinimum()
Returns the minimum value.
|
protected ValidationResult |
isNumberValid(T n,
NumberFormat fmt,
TransformerContext ctx,
T min,
T max)
Validates an entered number.
|
ValidationResult |
isValid(Object o,
TransformerContext ctx)
Validates the specified object.
|
void |
setMaximum(T maximum)
Sets the maximum value.
|
void |
setMinimum(T minimum)
Sets the minimum value.
|
Object |
transform(Object o,
TransformerContext ctx)
Transforms the specified object to the target format.
|
protected T |
transformToNumber(Object o,
TransformerContext ctx,
NumberFormat fmt)
Transforms the given object into a number.
|
protected static final String PROP_MINIMUM
protected static final String PROP_MAXIMUM
public T getMinimum()
public void setMinimum(T minimum)
minimum
- the minimum valuepublic T getMaximum()
public void setMaximum(T maximum)
maximum
- the maximum valuepublic Object transform(Object o, TransformerContext ctx) throws Exception
Number
of the
type specified by the generics parameter for this class. This is done by
using a java.text.NumberFormat
object. If the passed in
object is null, null will also be returned.transform
in interface Transformer
o
- the object to be transformedctx
- the transformer contextException
- if conversion failspublic ValidationResult isValid(Object o, TransformerContext ctx)
isNumberValid()
will be called to check whether the number
lies in a valid range. Depending on these checks a validation result
object is returned. A null object or an empty string are
considered valid.protected T transformToNumber(Object o, TransformerContext ctx, NumberFormat fmt) throws ParseException
transform()
and isValid()
. It performs the
actual transformation. The passed in object may be null or empty,
in which case null is returned.o
- the object to be transformedctx
- the transformer contextfmt
- the format object to be usedParseException
- if transformation failsprotected ValidationResult isNumberValid(T n, NumberFormat fmt, TransformerContext ctx, T min, T max)
isValid()
if the passed in object can be successfully
converted into a number. It checks this number against the minimum and
maximum values (if defined).n
- the number to checkfmt
- the format object (used for formatting the minimum and/or
maximum values in error messages)ctx
- the transformation contextmin
- the minimum value (can be null)max
- the maximum value (can be null)protected ValidationResult errorResult(String errorKey, TransformerContext ctx, Object... params)
errorKey
- the key of the error messagectx
- the transformer contextparams
- optional parameters for the error messageprotected abstract T convert(Number n)
NumberFormat
object. There is no guarantee
that the result of this parsing process matches the desired type. If this
is not the case, this method will be called.n
- the number to be convertedIllegalArgumentException
- if the number cannot be converted to the
target type (e.g. because it does not fit into the supported
range)protected abstract NumberFormat createFormat(Locale locale)
locale
- the locale to useprotected abstract T fetchProperty(org.apache.commons.configuration.Configuration config, String property, T defaultValue)
Configuration
object.config
- the configuration objectproperty
- the name of the property to be obtaineddefaultValue
- the default value for this propertyCopyright © 2016 The JGUIraffe Team. All rights reserved.