1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.util.prefs;
21
22 import org.xnap.XNap;
23
24 /***
25 * An integer validator. A range can be defined.
26 */
27 public class IntValidator implements Validator
28 {
29
30
31
32 private int min;
33 private int max;
34
35
36
37 public IntValidator(int min, int max)
38 {
39 if (min > max) {
40 throw new IllegalArgumentException("min must not be greater than max (" + min + " > " + max + ")");
41 }
42
43 this.min = min;
44 this.max = max;
45 }
46
47 public IntValidator(int min)
48 {
49 this(min, Integer.MAX_VALUE);
50 }
51
52 public IntValidator()
53 {
54 this(Integer.MIN_VALUE, Integer.MAX_VALUE);
55 }
56
57
58
59 /***
60 * Validates <code>String</code>.
61 * @exception IllegalArgumentException if newValue is invalid.
62 */
63 public void validate(String value)
64 {
65 if (value == null) {
66 throw new IllegalArgumentException
67 (XNap.tr("Value must not be null"));
68 }
69
70 int i = Integer.parseInt(value);
71 if (i < min || i > max) {
72 throw(new IllegalArgumentException(XNap.tr("Value out of range.")));
73 }
74 }
75
76 }