1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.gridsystems.config.tools;
25
26
27
28
29
30
31
32 public class NumberVerifier extends TextVerifier {
33
34
35
36 public static final String ID_EMPTY = "EmptyField";
37
38
39
40
41 public static final String ID_OUTOFRANGE = "OutOfRange";
42
43
44
45
46 public static final String ID_INVALID = "InvalidNumber";
47
48
49
50
51 private int min;
52
53
54
55
56 private int max;
57
58
59
60
61 private boolean emptyAllowed;
62
63
64
65
66 protected String id;
67
68
69
70
71
72
73
74 public NumberVerifier(ErrorSupport support, String id) {
75 this(support, id, Integer.MIN_VALUE, Integer.MAX_VALUE, false);
76 }
77
78
79
80
81
82
83
84
85 public NumberVerifier(ErrorSupport support, String id, boolean emptyAllowed) {
86 this(support, id, Integer.MIN_VALUE, Integer.MAX_VALUE, emptyAllowed);
87 }
88
89
90
91
92
93
94
95
96
97
98 public NumberVerifier(ErrorSupport support, String id, int min, int max,
99 boolean emptyAllowed) {
100 super(support);
101 this.id = id;
102 this.min = min;
103 this.max = max;
104 this.emptyAllowed = emptyAllowed;
105 }
106
107
108
109
110
111
112
113 public boolean verify(String text) {
114 int value = 0;
115
116 boolean v1 = emptyAllowed || (text != null && !text.equals(""));
117 boolean v2 = true;
118 try {
119 value = Integer.parseInt(text.trim());
120 } catch (Exception e) {
121 v2 = false;
122 }
123
124 boolean v3 = (value >= min && value <= max);
125 boolean valid = v1 && v2 && v3;
126
127 if (id == null) {
128 updateError(ID_OUTOFRANGE, !v3);
129 updateError(ID_INVALID, !v2);
130 updateError(ID_EMPTY, !v1);
131 } else {
132 updateError(id, !valid);
133 }
134
135 return valid;
136 }
137
138
139
140
141 public void cleanErrors() {
142 if (id == null) {
143 updateError(ID_OUTOFRANGE, false);
144 updateError(ID_INVALID, false);
145 updateError(ID_EMPTY, false);
146 } else {
147 updateError(id, false);
148 }
149 }
150 }