blob: 734ff481af4b4d21caae492488211c7de9a2147e (
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.data.validator;
/**
* An base implementation for validating any objects that implement
* {@link Comparable}.
*
* Verifies that the value is of the given type and within the (optionally)
* given limits. Typically you want to use a sub class of this like
* {@link IntegerRangeValidator}, {@link DoubleRangeValidator} or
* {@link DateRangeValidator} in applications.
* <p>
* Note that {@link RangeValidator} always accept null values. Make a field
* required to ensure that no empty values are accepted or override
* {@link #isValidValue(Comparable)}.
* </p>
*
* @param <T>
* The type of Number to validate. Must implement Comparable so that
* minimum and maximum checks work.
* @author Vaadin Ltd.
* @since 7.0
*/
public class RangeValidator<T extends Comparable> extends AbstractValidator<T> {
private T minValue = null;
private boolean minValueIncluded = true;
private T maxValue = null;
private boolean maxValueIncluded = true;
private Class<T> type;
/**
* Creates a new range validator of the given type.
*
* @param errorMessage
* The error message to use if validation fails
* @param type
* The type of object the validator can validate.
* @param minValue
* The minimum value that should be accepted or null for no limit
* @param maxValue
* The maximum value that should be accepted or null for no limit
*/
public RangeValidator(String errorMessage, Class<T> type, T minValue,
T maxValue) {
super(errorMessage);
this.type = type;
this.minValue = minValue;
this.maxValue = maxValue;
}
/**
* Checks if the minimum value is part of the accepted range
*
* @return true if the minimum value is part of the range, false otherwise
*/
public boolean isMinValueIncluded() {
return minValueIncluded;
}
/**
* Sets if the minimum value is part of the accepted range
*
* @param minValueIncluded
* true if the minimum value should be part of the range, false
* otherwise
*/
public void setMinValueIncluded(boolean minValueIncluded) {
this.minValueIncluded = minValueIncluded;
}
/**
* Checks if the maximum value is part of the accepted range
*
* @return true if the maximum value is part of the range, false otherwise
*/
public boolean isMaxValueIncluded() {
return maxValueIncluded;
}
/**
* Sets if the maximum value is part of the accepted range
*
* @param maxValueIncluded
* true if the maximum value should be part of the range, false
* otherwise
*/
public void setMaxValueIncluded(boolean maxValueIncluded) {
this.maxValueIncluded = maxValueIncluded;
}
/**
* Gets the minimum value of the range
*
* @return the minimum value
*/
public T getMinValue() {
return minValue;
}
/**
* Sets the minimum value of the range. Use
* {@link #setMinValueIncluded(boolean)} to control whether this value is
* part of the range or not.
*
* @param minValue
* the minimum value
*/
public void setMinValue(T minValue) {
this.minValue = minValue;
}
/**
* Gets the maximum value of the range
*
* @return the maximum value
*/
public T getMaxValue() {
return maxValue;
}
/**
* Sets the maximum value of the range. Use
* {@link #setMaxValueIncluded(boolean)} to control whether this value is
* part of the range or not.
*
* @param maxValue
* the maximum value
*/
public void setMaxValue(T maxValue) {
this.maxValue = maxValue;
}
/*
* (non-Javadoc)
*
* @see
* com.vaadin.data.validator.AbstractValidator#isValidValue(java.lang.Object
* )
*/
@Override
protected boolean isValidValue(T value) {
if (value == null) {
return true;
}
if (getMinValue() != null) {
// Ensure that the min limit is ok
int result = value.compareTo(getMinValue());
if (result < 0) {
// value less than min value
return false;
} else if (result == 0 && !isMinValueIncluded()) {
// values equal and min value not included
return false;
}
}
if (getMaxValue() != null) {
// Ensure that the Max limit is ok
int result = value.compareTo(getMaxValue());
if (result > 0) {
// value greater than max value
return false;
} else if (result == 0 && !isMaxValueIncluded()) {
// values equal and max value not included
return false;
}
}
return true;
}
/*
* (non-Javadoc)
*
* @see com.vaadin.data.validator.AbstractValidator#getType()
*/
@Override
public Class<T> getType() {
return type;
}
}
|