Class StringToBooleanConverter

java.lang.Object
com.vaadin.flow.data.converter.StringToBooleanConverter
All Implemented Interfaces:
Converter<String,Boolean>, Serializable

public class StringToBooleanConverter extends Object implements Converter<String,Boolean>
A converter that converts from String to Boolean and back. The String representation is given by Boolean.toString() or provided in constructor StringToBooleanConverter(String, String, String).

Leading and trailing white spaces are ignored when converting from a String.

For language-dependent representation, subclasses should overwrite getFalseString(Locale) and getTrueString(Locale)

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

    • StringToBooleanConverter

      public StringToBooleanConverter(String errorMessage)
      Creates converter with default string representations - "true" and "false".
      Parameters:
      errorMessage - the error message to use if conversion fails
    • StringToBooleanConverter

      public StringToBooleanConverter(ErrorMessageProvider errorMessageProvider)
      Creates a new converter instance with the given error message provider. Empty strings are converted to null.
      Parameters:
      errorMessageProvider - the error message provider to use if conversion fails
    • StringToBooleanConverter

      public StringToBooleanConverter(String errorMessage, String trueString, String falseString)
      Creates converter with custom string representation.
      Parameters:
      errorMessage - the error message to use if conversion fails
      falseString - string representation for false
      trueString - string representation for true
    • StringToBooleanConverter

      public StringToBooleanConverter(String trueString, String falseString, ErrorMessageProvider errorMessageProvider)
      Creates converter with custom string representation.
      Parameters:
      falseString - string representation for false
      trueString - string representation for true
      errorMessageProvider - the error message provider to use if conversion fails
  • Method Details

    • convertToModel

      public Result<Boolean> convertToModel(String value, ValueContext context)
      Description copied from interface: Converter
      Converts the given value from presentation type to model type.

      A converter can optionally use locale to do the conversion.

      Specified by:
      convertToModel in interface Converter<String,Boolean>
      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
    • convertToPresentation

      public String convertToPresentation(Boolean 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,Boolean>
      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
    • getFalseString

      protected String getFalseString(Locale locale)
      Gets the locale-depended string representation for false. Default is locale-independent value false
      Parameters:
      locale - to be used
      Returns:
      the string representation for false
    • getTrueString

      protected String getTrueString(Locale locale)
      Gets the locale-depended string representation for true. Default is locale-independent value true
      Parameters:
      locale - to be used
      Returns:
      the string representation for true