1 | |
package org.jtheque.core.utils.ui.constraints; |
2 | |
|
3 | |
import org.jtheque.core.managers.error.JThequeError; |
4 | |
import org.jtheque.core.utils.ui.ValidationUtils; |
5 | |
|
6 | |
import java.util.Collection; |
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public final class MaxLengthConstraint implements Constraint { |
30 | |
private final int maxLength; |
31 | |
private final String fieldName; |
32 | |
private final boolean canBenNull; |
33 | |
private final boolean numerical; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public MaxLengthConstraint(int maxLength, String fieldName, boolean canBenNull, boolean numerical) { |
44 | 0 | super(); |
45 | |
|
46 | 0 | this.maxLength = maxLength; |
47 | 0 | this.fieldName = fieldName; |
48 | 0 | this.canBenNull = canBenNull; |
49 | 0 | this.numerical = numerical; |
50 | 0 | } |
51 | |
|
52 | |
@Override |
53 | |
public int maxLength() { |
54 | 0 | return maxLength; |
55 | |
} |
56 | |
|
57 | |
@Override |
58 | |
public boolean mustBeNumerical() { |
59 | 0 | return numerical; |
60 | |
} |
61 | |
|
62 | |
@Override |
63 | |
public boolean canBeNullOrEmpty() { |
64 | 0 | return canBenNull; |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
public boolean mustControlLength() { |
69 | 0 | return true; |
70 | |
} |
71 | |
|
72 | |
@Override |
73 | |
public void validate(Object field, Collection<JThequeError> errors) { |
74 | 0 | CharSequence str = (CharSequence) field; |
75 | |
|
76 | 0 | if (!canBenNull) { |
77 | 0 | ValidationUtils.rejectIfEmpty(str, fieldName, errors); |
78 | |
} |
79 | |
|
80 | 0 | ValidationUtils.rejectIfLongerThan(str, fieldName, maxLength, errors); |
81 | 0 | } |
82 | |
} |