/* * Copyright 2000-2021 Vaadin Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.vaadin.data.converter; import java.util.Locale; import com.vaadin.data.Converter; import com.vaadin.data.ErrorMessageProvider; import com.vaadin.data.Result; import com.vaadin.data.ValueContext; /** * A converter that converts from {@link String} to {@link Boolean} and back. * The String representation is given by {@link Boolean#toString()} or provided * in constructor * {@link StringToBooleanConverter#StringToBooleanConverter(String, String, String)}. *
* Leading and trailing white spaces are ignored when converting from a String. *
** For language-dependent representation, subclasses should overwrite * {@link #getFalseString(Locale)} and {@link #getTrueString(Locale)} *
* * @author Vaadin Ltd * @since 8.0 */ public class StringToBooleanConverter implements Converternull
.
*
* @param errorMessageProvider
* the error message provider to use if conversion fails
*
* @since 8.4
*/
public StringToBooleanConverter(ErrorMessageProvider errorMessageProvider) {
this(Boolean.TRUE.toString(), Boolean.FALSE.toString(),
errorMessageProvider);
}
/**
* Creates converter with custom string representation.
*
* @param errorMessage
* the error message to use if conversion fails
* @param falseString
* string representation for false
* @param trueString
* string representation for true
*/
public StringToBooleanConverter(String errorMessage, String trueString,
String falseString) {
this(trueString, falseString, ctx -> errorMessage);
}
/**
* Creates converter with custom string representation.
*
* @param falseString
* string representation for false
* @param trueString
* string representation for true
* @param errorMessageProvider
* the error message provider to use if conversion fails
*
* @since 8.4
*/
public StringToBooleanConverter(String trueString, String falseString,
ErrorMessageProvider errorMessageProvider) {
this.errorMessageProvider = errorMessageProvider;
this.trueString = trueString;
this.falseString = falseString;
}
@Override
public Result