com.vaadin.flow.data.converter.
Class StringToBooleanConverter
- java.lang.Object
-
- com.vaadin.flow.data.converter.StringToBooleanConverter
-
All Implemented Interfaces:
public class StringToBooleanConverter extends Object implements Converter<String,Boolean>
A converter that converts from
String
toBoolean
and back. The String representation is given byBoolean.toString()
or provided in constructorStringToBooleanConverter(String, String, String)
.Leading and trailing white spaces are ignored when converting from a String.
For language-dependent representation, subclasses should overwrite
getFalseString(Locale)
andgetTrueString(Locale)
Since:
1.0
Author:
Vaadin Ltd
See Also:
-
-
Constructor Summary
Constructors Constructor and Description StringToBooleanConverter(ErrorMessageProvider errorMessageProvider)
Creates a new converter instance with the given error message provider.
StringToBooleanConverter(String errorMessage)
Creates converter with default string representations - "true" and "false".
StringToBooleanConverter(String trueString, String falseString, ErrorMessageProvider errorMessageProvider)
Creates converter with custom string representation.
StringToBooleanConverter(String errorMessage, String trueString, String falseString)
Creates converter with custom string representation.
-
Method Summary
All Methods Modifier and Type Method and Description Result<Boolean>
convertToModel(String value, ValueContext context)
Converts the given value from model type to presentation type.
String
convertToPresentation(Boolean value, ValueContext context)
Converts the given value from presentation type to model type.
protected String
getFalseString(Locale locale)
Gets the locale-depended string representation for false.
protected String
getTrueString(Locale locale)
Gets the locale-depended string representation for true.
-
-
-
Constructor Detail
-
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 failsfalseString
- string representation forfalse
trueString
- string representation fortrue
-
StringToBooleanConverter
public StringToBooleanConverter(String trueString, String falseString, ErrorMessageProvider errorMessageProvider)
Creates converter with custom string representation.
Parameters:
falseString
- string representation forfalse
trueString
- string representation fortrue
errorMessageProvider
- the error message provider to use if conversion fails
-
-
Method Detail
-
convertToModel
public Result<Boolean> convertToModel(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 interfaceConverter<String,Boolean>
Parameters:
value
- The value to convert. Can be nullcontext
- 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 presentation type to model type.
A converter can optionally use locale to do the conversion.
Specified by:
convertToPresentation
in interfaceConverter<String,Boolean>
Parameters:
value
- The value to convert. Can be nullcontext
- 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 usedReturns:
the string representation for false
-
-