瀏覽代碼

Add a builder for ValidatorChains.

This allows them to be created inline without using type-unsafe
varargs (albeit a bit more verbosely).

new ValidatorChain(v1, v2, ...) becomes

ValidatorChain.builder()
    .addValidator(v1)
    .addValidator(v2)
    ...
    .build();

Change-Id: I6c92142bf294335419fbd16593125416dd33c74e
Reviewed-on: http://gerrit.dmdirc.com/3168
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
changes/68/3168/2
Chris Smith 10 年之前
父節點
當前提交
c03aa4e53e

+ 28
- 8
src/com/dmdirc/util/validators/ValidatorChain.java 查看文件

@@ -19,35 +19,45 @@
19 19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 20
  * SOFTWARE.
21 21
  */
22
-
23 22
 package com.dmdirc.util.validators;
24 23
 
25
-import java.util.ArrayList;
26 24
 import java.util.Arrays;
25
+import java.util.Collections;
27 26
 import java.util.List;
28 27
 
29 28
 /**
30 29
  * Allows multiple validators to be chained together.
31 30
  *
32 31
  * @param <A> The type of class that this chain validates
33
- * @author chris
34 32
  */
35 33
 public class ValidatorChain<A> implements Validator<A> {
36 34
 
37
-    /** A list of validators to use. */
38
-    private final List<Validator<A>> validatorList = new ArrayList<>();
35
+    /**
36
+     * A list of validators to use.
37
+     */
38
+    private final List<Validator<A>> validatorList;
39 39
 
40 40
     /**
41 41
      * Creates a new validator chain containing the specified validators.
42 42
      *
43 43
      * @param validators The validators to be used in this chain.
44
+     * @deprecated Use a builder or pass a list to avoid unchecked warnings.
44 45
      */
45 46
     @SuppressWarnings({"unchecked", "varargs"})
46
-    public ValidatorChain(final Validator<A> ... validators) {
47
-        validatorList.addAll(Arrays.asList(validators));
47
+    @Deprecated
48
+    public ValidatorChain(final Validator<A>... validators) {
49
+        validatorList = Arrays.asList(validators);
50
+    }
51
+
52
+    /**
53
+     * Creates a new validator chain containing the specified validators.
54
+     *
55
+     * @param validators The validators to be used in this chain.
56
+     */
57
+    public ValidatorChain(final List<Validator<A>> validators) {
58
+        validatorList = Collections.unmodifiableList(validators);
48 59
     }
49 60
 
50
-    /** {@inheritDoc} */
51 61
     @Override
52 62
     public ValidationResponse validate(final A object) {
53 63
         for (Validator<A> validator : validatorList) {
@@ -61,4 +71,14 @@ public class ValidatorChain<A> implements Validator<A> {
61 71
         return new ValidationResponse();
62 72
     }
63 73
 
74
+    /**
75
+     * Creates a builder which can be used to construct a new
76
+     * {@link ValidatorChain}.
77
+     *
78
+     * @param <T> The type of validator that will be in the chain.
79
+     * @return A builder to use to make a validator.
80
+     */
81
+    public static <T> ValidatorChainBuilder<T> builder() {
82
+        return new ValidatorChainBuilder<>();
83
+    }
64 84
 }

+ 48
- 0
src/com/dmdirc/util/validators/ValidatorChainBuilder.java 查看文件

@@ -0,0 +1,48 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+package com.dmdirc.util.validators;
23
+
24
+import java.util.ArrayList;
25
+import java.util.List;
26
+
27
+/**
28
+ * Simple builder to create a {@link ValidatorChain} with a dynamic number of
29
+ * validators.
30
+ *
31
+ * @param <T> The type of validator to build.
32
+ */
33
+public final class ValidatorChainBuilder<T> {
34
+
35
+    private final List<Validator<T>> validators = new ArrayList<>();
36
+
37
+    ValidatorChainBuilder() {
38
+    }
39
+
40
+    public ValidatorChainBuilder<T> addValidator(final Validator<T> validator) {
41
+        validators.add(validator);
42
+        return this;
43
+    }
44
+
45
+    public ValidatorChain<T> build() {
46
+        return new ValidatorChain<>(validators);
47
+    }
48
+}

Loading…
取消
儲存