aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/data/validator/StringLengthValidator.java
blob: 06f3463531cea040f2f1d2febb36b33956cc266f (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
/* 
@VaadinApache2LicenseForJavaFiles@
 */

package com.vaadin.data.validator;

/**
 * This <code>StringLengthValidator</code> is used to validate the length of
 * strings.
 * 
 * @author Vaadin Ltd.
 * @since 3.0
 */
@SuppressWarnings("serial")
public class StringLengthValidator extends AbstractStringValidator {

    private Integer minLength = null;

    private Integer maxLength = null;

    private boolean allowNull = true;

    /**
     * Creates a new StringLengthValidator with a given error message.
     * 
     * @param errorMessage
     *            the message to display in case the value does not validate.
     */
    public StringLengthValidator(String errorMessage) {
        super(errorMessage);
    }

    /**
     * Creates a new StringLengthValidator with a given error message and
     * minimum and maximum length limits.
     * 
     * @param errorMessage
     *            the message to display in case the value does not validate.
     * @param minLength
     *            the minimum permissible length of the string or null for no
     *            limit. A negative value for no limit is also supported for
     *            backwards compatibility.
     * @param maxLength
     *            the maximum permissible length of the string or null for no
     *            limit. A negative value for no limit is also supported for
     *            backwards compatibility.
     * @param allowNull
     *            Are null strings permissible? This can be handled better by
     *            setting a field as required or not.
     */
    public StringLengthValidator(String errorMessage, Integer minLength,
            Integer maxLength, boolean allowNull) {
        this(errorMessage);
        setMinLength(minLength);
        setMaxLength(maxLength);
        setNullAllowed(allowNull);
    }

    /**
     * Checks if the given value is valid.
     * 
     * @param value
     *            the value to validate.
     * @return <code>true</code> for valid value, otherwise <code>false</code>.
     */
    @Override
    protected boolean isValidValue(String value) {
        if (value == null) {
            return allowNull;
        }
        final int len = value.length();
        if ((minLength != null && minLength > -1 && len < minLength)
                || (maxLength != null && maxLength > -1 && len > maxLength)) {
            return false;
        }
        return true;
    }

    /**
     * Returns <code>true</code> if null strings are allowed.
     * 
     * @return <code>true</code> if allows null string, otherwise
     *         <code>false</code>.
     */
    @Deprecated
    public final boolean isNullAllowed() {
        return allowNull;
    }

    /**
     * Gets the maximum permissible length of the string.
     * 
     * @return the maximum length of the string or null if there is no limit
     */
    public Integer getMaxLength() {
        return maxLength;
    }

    /**
     * Gets the minimum permissible length of the string.
     * 
     * @return the minimum length of the string or null if there is no limit
     */
    public Integer getMinLength() {
        return minLength;
    }

    /**
     * Sets whether null-strings are to be allowed. This can be better handled
     * by setting a field as required or not.
     */
    @Deprecated
    public void setNullAllowed(boolean allowNull) {
        this.allowNull = allowNull;
    }

    /**
     * Sets the maximum permissible length of the string.
     * 
     * @param maxLength
     *            the maximum length to accept or null for no limit
     */
    public void setMaxLength(Integer maxLength) {
        this.maxLength = maxLength;
    }

    /**
     * Sets the minimum permissible length.
     * 
     * @param minLength
     *            the minimum length to accept or null for no limit
     */
    public void setMinLength(Integer minLength) {
        this.minLength = minLength;
    }

}