Bläddra i källkod

Add integer validator.

Change-Id: If2dab9ae160c2152758ea4240175a0d928ae48c5
Reviewed-on: http://gerrit.dmdirc.com/3568
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/68/3568/2
Greg Holmes 10 år sedan
förälder
incheckning
459368e4a1
1 ändrade filer med 68 tillägg och 0 borttagningar
  1. 68
    0
      src/com/dmdirc/util/validators/IntegerValidator.java

+ 68
- 0
src/com/dmdirc/util/validators/IntegerValidator.java Visa fil

@@ -0,0 +1,68 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+
7
+package com.dmdirc.util.validators;
8
+
9
+/**
10
+ * Validates that a number is within certain bounds.
11
+ */
12
+public class IntegerValidator implements Validator<Integer> {
13
+
14
+    /**
15
+     * The minimum value for this number.
16
+     */
17
+    protected final int min;
18
+
19
+    /**
20
+     * The maximum value for this number.
21
+     */
22
+    protected final int max;
23
+
24
+    /**
25
+     * Creates a new numerical validator with the specified bounds.
26
+     *
27
+     * @param min The minimum value for the number, or -1 for unlimited.
28
+     * @param max The maximum value for the number, or -1 for unlimited.
29
+     */
30
+    public IntegerValidator(final int min, final int max) {
31
+        this.max = max == -1 ? Integer.MAX_VALUE : max;
32
+        this.min = min == -1 ? Integer.MIN_VALUE : min;
33
+        if (this.min > this.max) {
34
+            throw new IllegalArgumentException("min must be less than max.");
35
+        }
36
+    }
37
+
38
+    /**
39
+     * Retrieves the maximum value that this validator will allow.
40
+     *
41
+     * @return This validator's maximum value
42
+     */
43
+    public int getMax() {
44
+        return max;
45
+    }
46
+
47
+    /**
48
+     * Retrieves the minimum value that this validator will allow.
49
+     *
50
+     * @return This validator's minimum value
51
+     */
52
+    public int getMin() {
53
+        return min;
54
+    }
55
+
56
+    @Override
57
+    public ValidationResponse validate(final Integer object) {
58
+
59
+        if (object < min) {
60
+            return new ValidationResponse("Must be at least " + min);
61
+        } else if (object > max) {
62
+            return new ValidationResponse("Must be at most " + max);
63
+        } else {
64
+            return new ValidationResponse();
65
+        }
66
+    }
67
+
68
+}

Laddar…
Avbryt
Spara