Class StringToBooleanConverter

  • All Implemented Interfaces:
    Converter<java.lang.String,​java.lang.Boolean>, java.io.Serializable

    public class StringToBooleanConverter
    extends java.lang.Object
    implements Converter<java.lang.String,​java.lang.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:
    8.0
    Author:
    Vaadin Ltd
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      StringToBooleanConverter​(ErrorMessageProvider errorMessageProvider)
      Creates a new converter instance with the given error message provider.
      StringToBooleanConverter​(java.lang.String errorMessage)
      Creates converter with default string representations - "true" and "false".
      StringToBooleanConverter​(java.lang.String trueString, java.lang.String falseString, ErrorMessageProvider errorMessageProvider)
      Creates converter with custom string representation.
      StringToBooleanConverter​(java.lang.String errorMessage, java.lang.String trueString, java.lang.String falseString)
      Creates converter with custom string representation.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Result<java.lang.Boolean> convertToModel​(java.lang.String value, ValueContext context)
      Converts the given value from model type to presentation type.
      java.lang.String convertToPresentation​(java.lang.Boolean value, ValueContext context)
      Converts the given value from presentation type to model type.
      protected java.lang.String getFalseString​(java.util.Locale locale)
      Gets the locale-depended string representation for false.
      protected java.lang.String getTrueString​(java.util.Locale locale)
      Gets the locale-depended string representation for true.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StringToBooleanConverter

        public StringToBooleanConverter​(java.lang.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
        Since:
        8.4
      • StringToBooleanConverter

        public StringToBooleanConverter​(java.lang.String errorMessage,
                                        java.lang.String trueString,
                                        java.lang.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​(java.lang.String trueString,
                                        java.lang.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
        Since:
        8.4
    • Method Detail

      • convertToModel

        public Result<java.lang.Boolean> convertToModel​(java.lang.String 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:
        convertToModel in interface Converter<java.lang.String,​java.lang.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 java.lang.String convertToPresentation​(java.lang.Boolean 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:
        convertToPresentation in interface Converter<java.lang.String,​java.lang.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 java.lang.String getFalseString​(java.util.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 java.lang.String getTrueString​(java.util.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