Class AbstractStringToNumberConverter<T extends Number>

java.lang.Object
com.vaadin.flow.data.converter.AbstractStringToNumberConverter<T>
Type Parameters:
T - the converter number type
All 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 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.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • 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 be null
      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 be null
      errorMessage - the error message to use if conversion fails
  • Method Details

    • getFormat

      protected NumberFormat getFormat(Locale locale)
      Parameters:
      locale - The locale to use
      Returns:
      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 convert
      context - The value context for conversion
      Returns:
      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 message
      Returns:
      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 interface Converter<String,T extends Number>
      Parameters:
      value - The value to convert. Can be null
      context - The value context for the conversion.
      Returns:
      The converted value compatible with the source type