com.vaadin.flow.data.converter.
Class AbstractStringToNumberConverter<T extends Number>
- java.lang.Object
-
- com.vaadin.flow.data.converter.AbstractStringToNumberConverter<T>
-
Type Parameters:
T
- the converter number typeAll Implemented Interfaces:
Converter<String,T>
,Serializable
Direct Known Subclasses:
StringToBigDecimalConverter
,StringToBigIntegerConverter
,StringToDoubleConverter
,StringToFloatConverter
,StringToIntegerConverter
,StringToLongConverter
public abstract class AbstractStringToNumberConverter<T extends Number> extends Object implements Converter<String,T>
A converter that converts from the number type T to
String
and back. Uses the given locale andNumberFormat
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.Since:
1.0
Author:
Vaadin Ltd
See Also:
-
-
Constructor Summary
Constructors Modifier Constructor 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.
-
Method Summary
All Methods Modifier and Type Method 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)
andConverter.convertToModel(Object, ValueContext)
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.vaadin.flow.data.converter.Converter
chain, convertToModel
-
-
-
-
Constructor Detail
-
AbstractStringToNumberConverter
protected AbstractStringToNumberConverter(T emptyValue, ErrorMessageProvider errorMessageProvider)
Creates a new converter instance with the given presentation value for empty string and error message provider.
Parameters:
emptyValue
- the presentation value to return when converting an empty string, may benull
errorMessageProvider
- the error message provider to use if conversion fails
-
AbstractStringToNumberConverter
protected AbstractStringToNumberConverter(T emptyValue, String errorMessage)
Creates a new converter instance with the given presentation value for empty string and error message.
Parameters:
emptyValue
- the presentation value to return when converting an empty string, may benull
errorMessage
- the error message to use if conversion fails
-
-
Method Detail
-
getFormat
protected NumberFormat getFormat(Locale locale)
Returns the format used by
Converter.convertToPresentation(Object, ValueContext)
andConverter.convertToModel(Object, ValueContext)
.Parameters:
locale
- The locale to useReturns:
A NumberFormat instance
-
convertToNumber
protected Result<Number> convertToNumber(String value, ValueContext context)
Convert the value to a Number using the given locale and
getFormat(Locale)
.Parameters:
value
- The value to convertcontext
- The value context for conversionReturns:
The converted value
-
getErrorMessage
protected String getErrorMessage(ValueContext context)
Gets the error message to use when conversion fails.
Parameters:
context
- The value context for generating the error messageReturns:
the error message
-
convertToPresentation
public String convertToPresentation(T value, ValueContext context)
Description copied from interface:
Converter
Converts the given value from model type to presentation type.
A converter can optionally use locale to do the conversion.
Specified by:
convertToPresentation
in interfaceConverter<String,T extends Number>
Parameters:
value
- The value to convert. Can be nullcontext
- The value context for the conversion.Returns:
The converted value compatible with the source type
-
-