T
- the converter number typepublic abstract class AbstractStringToNumberConverter<T extends Number> extends Object implements Converter<String,T>
String
and back.
Uses the given locale and NumberFormat
for formatting and parsing.
Automatically trims the input string, removing any leading and trailing white
space.
Override and overwrite getFormat(Locale)
to use a different format.
Modifier | Constructor and Description |
---|---|
protected |
AbstractStringToNumberConverter(T emptyValue,
ErrorMessageProvider errorMessageProvider)
Creates a new converter instance with the given presentation value for
empty string and error message provider.
|
protected |
AbstractStringToNumberConverter(T emptyValue,
String errorMessage)
Creates a new converter instance with the given presentation value for
empty string and error message.
|
Modifier and Type | Method and Description |
---|---|
protected Result<Number> |
convertToNumber(String value,
ValueContext context)
Convert the value to a Number using the given locale and
getFormat(Locale) . |
String |
convertToPresentation(T value,
ValueContext context)
Converts the given value from model type to presentation type.
|
protected String |
getErrorMessage(ValueContext context)
Gets the error message to use when conversion fails.
|
protected NumberFormat |
getFormat(Locale locale)
Returns the format used by
Converter.convertToPresentation(Object, ValueContext) and
Converter.convertToModel(Object, ValueContext) . |
protected AbstractStringToNumberConverter(T emptyValue, ErrorMessageProvider errorMessageProvider)
emptyValue
- the presentation value to return when converting an empty
string, may be null
errorMessageProvider
- the error message provider to use if conversion failsprotected AbstractStringToNumberConverter(T emptyValue, String errorMessage)
emptyValue
- the presentation value to return when converting an empty
string, may be null
errorMessage
- the error message to use if conversion failsprotected NumberFormat getFormat(Locale locale)
Converter.convertToPresentation(Object, ValueContext)
and
Converter.convertToModel(Object, ValueContext)
.locale
- The locale to useprotected Result<Number> convertToNumber(String value, ValueContext context)
getFormat(Locale)
.value
- The value to convertcontext
- The value context for conversionprotected String getErrorMessage(ValueContext context)
context
- The value context for generating the error messagepublic String convertToPresentation(T value, ValueContext context)
Converter
A converter can optionally use locale to do the conversion.
convertToPresentation
in interface Converter<String,T extends Number>
value
- The value to convert. Can be nullcontext
- The value context for the conversion.Copyright © 2023. All rights reserved.