aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/data/converter/StringToBooleanConverter.java
blob: edb2908afad22a7f63325b9e3653119b4c2f062f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright 2000-2016 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)}.
 * <p>
 * Leading and trailing white spaces are ignored when converting from a String.
 * </p>
 * <p>
 * For language-dependent representation, subclasses should overwrite
 * {@link #getFalseString(Locale)} and {@link #getTrueString(Locale)}
 * </p>
 *
 * @author Vaadin Ltd
 * @since 8.0
 */
public class StringToBooleanConverter implements Converter<String, Boolean> {

    private final String trueString;

    private final String falseString;

    private ErrorMessageProvider errorMessageProvider;

    /**
     * Creates converter with default string representations - "true" and
     * "false".
     *
     * @param errorMessage
     *            the error message to use if conversion fails
     */
    public StringToBooleanConverter(String errorMessage) {
        this(errorMessage, Boolean.TRUE.toString(), Boolean.FALSE.toString());
    }

    /**
     * Creates a new converter instance with the given error message provider.
     * Empty strings are converted to <code>null</code>.
     *
     * @param errorMessageProvider
     *            the error message provider to use if conversion fails
     *
     * @since
     */
    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 <code>false</code>
     * @param trueString
     *            string representation for <code>true</code>
     */
    public StringToBooleanConverter(String errorMessage, String trueString,
            String falseString) {
        this(trueString, falseString, ctx -> errorMessage);
    }

    /**
     * Creates converter with custom string representation.
     *
     * @param falseString
     *            string representation for <code>false</code>
     * @param trueString
     *            string representation for <code>true</code>
     * @param errorMessageProvider
     *            the error message provider to use if conversion fails
     *
     * @since
     */
    public StringToBooleanConverter(String trueString, String falseString,
            ErrorMessageProvider errorMessageProvider) {
        this.errorMessageProvider = errorMessageProvider;
        this.trueString = trueString;
        this.falseString = falseString;
    }

    @Override
    public Result<Boolean> convertToModel(String value, ValueContext context) {
        if (value == null) {
            return Result.ok(null);
        }

        // Remove leading and trailing white space
        value = value.trim();

        Locale locale = context.getLocale().orElse(null);
        if (getTrueString(locale).equals(value)) {
            return Result.ok(true);
        } else if (getFalseString(locale).equals(value)) {
            return Result.ok(false);
        } else if (value.isEmpty()) {
            return Result.ok(null);
        } else {
            return Result.error(errorMessageProvider.apply(context));
        }
    }

    @Override
    public String convertToPresentation(Boolean value, ValueContext context) {
        if (value == null) {
            return null;
        }
        Locale locale = context.getLocale().orElse(null);
        if (value) {
            return getTrueString(locale);
        } else {
            return getFalseString(locale);
        }
    }

    /**
     * Gets the locale-depended string representation for false. Default is
     * locale-independent value {@code false}
     *
     * @param locale
     *            to be used
     * @return the string representation for false
     */
    protected String getFalseString(Locale locale) {
        return falseString;
    }

    /**
     * Gets the locale-depended string representation for true. Default is
     * locale-independent value {@code true}
     *
     * @param locale
     *            to be used
     * @return the string representation for true
     */
    protected String getTrueString(Locale locale) {
        return trueString;
    }

}