Package com.vaadin.data.converter
Class AbstractStringToNumberConverter<T extends java.lang.Number>
- java.lang.Object
-
- com.vaadin.data.converter.AbstractStringToNumberConverter<T>
-
- All Implemented Interfaces:
Converter<java.lang.String,T>,java.io.Serializable
- Direct Known Subclasses:
StringToBigDecimalConverter,StringToBigIntegerConverter,StringToDoubleConverter,StringToFloatConverter,StringToIntegerConverter,StringToLongConverter
public abstract class AbstractStringToNumberConverter<T extends java.lang.Number> extends java.lang.Object implements Converter<java.lang.String,T>
A converter that converts from the number type T toStringand back. Uses the given locale andNumberFormatfor 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:
- 8.0
- Author:
- Vaadin Ltd
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractStringToNumberConverter(T emptyValue, ErrorMessageProvider errorMessageProvider)Creates a new converter instance with the given empty string value and error message provider.protectedAbstractStringToNumberConverter(T emptyValue, java.lang.String errorMessage)Creates a new converter instance with the given empty string value and error message.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Result<java.lang.Number>convertToNumber(java.lang.String value, ValueContext context)Convert the value to a Number using the given locale andgetFormat(Locale).java.lang.StringconvertToPresentation(T value, ValueContext context)Converts the given value from presentation type to model type.protected java.lang.StringgetErrorMessage(ValueContext context)Gets the error message to use when conversion fails.protected java.text.NumberFormatgetFormat(java.util.Locale locale)Returns the format used byConverter.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.data.Converter
chain, convertToModel
-
-
-
-
Constructor Detail
-
AbstractStringToNumberConverter
protected AbstractStringToNumberConverter(T emptyValue, ErrorMessageProvider errorMessageProvider)
Creates a new converter instance with the given empty string value and error message provider.- Parameters:
emptyValue- the presentation value to return when converting an empty string, may benullerrorMessageProvider- the error message provider to use if conversion fails- Since:
- 8.4
-
AbstractStringToNumberConverter
protected AbstractStringToNumberConverter(T emptyValue, java.lang.String errorMessage)
Creates a new converter instance with the given empty string value and error message.- Parameters:
emptyValue- the presentation value to return when converting an empty string, may benullerrorMessage- the error message to use if conversion fails
-
-
Method Detail
-
getFormat
protected java.text.NumberFormat getFormat(java.util.Locale locale)
Returns the format used byConverter.convertToPresentation(Object, ValueContext)andConverter.convertToModel(Object, ValueContext).- Parameters:
locale- The locale to use- Returns:
- A NumberFormat instance
-
convertToNumber
protected Result<java.lang.Number> convertToNumber(java.lang.String value, ValueContext context)
Convert the value to a Number using the given locale andgetFormat(Locale).- Parameters:
value- The value to convertcontext- The value context for conversion- Returns:
- The converted value
-
getErrorMessage
protected java.lang.String getErrorMessage(ValueContext context)
Gets the error message to use when conversion fails.- Returns:
- the error message
-
convertToPresentation
public java.lang.String convertToPresentation(T value, ValueContext context)
Description copied from interface:ConverterConverts the given value from presentation type to model type.A converter can optionally use locale to do the conversion.
- Specified by:
convertToPresentationin interfaceConverter<java.lang.String,T extends java.lang.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
-
-