diff options
Diffstat (limited to 'src/main/java/fr/umlv/java/wallj/board/BoardValidator.java')
-rw-r--r-- | src/main/java/fr/umlv/java/wallj/board/BoardValidator.java | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/board/BoardValidator.java b/src/main/java/fr/umlv/java/wallj/board/BoardValidator.java index 011ca8b..d7d3583 100644 --- a/src/main/java/fr/umlv/java/wallj/board/BoardValidator.java +++ b/src/main/java/fr/umlv/java/wallj/board/BoardValidator.java | |||
@@ -13,6 +13,39 @@ import java.util.stream.IntStream; | |||
13 | */ | 13 | */ |
14 | public class BoardValidator { | 14 | public class BoardValidator { |
15 | 15 | ||
16 | private final Board board; | ||
17 | private final ValidationException errors = new ValidationException(); | ||
18 | |||
19 | /** | ||
20 | * @param board the board to validate | ||
21 | */ | ||
22 | public BoardValidator(Board board) { | ||
23 | this.board = board; | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Tests the board against a given validator, using the supplied error message if the validation fails. | ||
28 | * | ||
29 | * @param validator a validity test | ||
30 | * @param msg a failure message | ||
31 | * @return the board validator | ||
32 | */ | ||
33 | public BoardValidator validate(Predicate<Board> validator, String msg) { | ||
34 | if (!validator.test(board)) errors.addSuppressed(new ValidationException(msg)); | ||
35 | return this; | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * @return the validated board | ||
40 | * @throws ValidationException in case of failure | ||
41 | */ | ||
42 | public Board get() throws ValidationException { | ||
43 | if (errors.getSuppressed().length > 0) | ||
44 | throw errors; | ||
45 | else | ||
46 | return board; | ||
47 | } | ||
48 | |||
16 | /** | 49 | /** |
17 | * A validation exception, witness of validation error(s). | 50 | * A validation exception, witness of validation error(s). |
18 | */ | 51 | */ |
@@ -33,6 +66,10 @@ public class BoardValidator { | |||
33 | 66 | ||
34 | public static final int NB_DROPPABLE_BOMBS = 3; | 67 | public static final int NB_DROPPABLE_BOMBS = 3; |
35 | 68 | ||
69 | private Constraint() { | ||
70 | // static class | ||
71 | } | ||
72 | |||
36 | private static boolean inBoard(TileVec2 dim, TileVec2 v) { | 73 | private static boolean inBoard(TileVec2 dim, TileVec2 v) { |
37 | return v.getRow() >= 0 && v.getRow() < dim.getRow() && | 74 | return v.getRow() >= 0 && v.getRow() < dim.getRow() && |
38 | v.getCol() >= 0 && v.getRow() < dim.getCol(); | 75 | v.getCol() >= 0 && v.getRow() < dim.getCol(); |
@@ -104,43 +141,6 @@ public class BoardValidator { | |||
104 | b.stream().filter(block -> block.getValue() == BlockType.FREE).count() >= NB_DROPPABLE_BOMBS; | 141 | b.stream().filter(block -> block.getValue() == BlockType.FREE).count() >= NB_DROPPABLE_BOMBS; |
105 | } | 142 | } |
106 | 143 | ||
107 | private Constraint() { | ||
108 | // static class | ||
109 | } | ||
110 | |||
111 | } | ||
112 | |||
113 | private final Board board; | ||
114 | private final ValidationException errors = new ValidationException(); | ||
115 | |||
116 | /** | ||
117 | * @param board the board to validate | ||
118 | */ | ||
119 | public BoardValidator(Board board) { | ||
120 | this.board = board; | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * Tests the board against a given validator, using the supplied error message if the validation fails. | ||
125 | * | ||
126 | * @param validator a validity test | ||
127 | * @param msg a failure message | ||
128 | * @return the board validator | ||
129 | */ | ||
130 | public BoardValidator validate(Predicate<Board> validator, String msg) { | ||
131 | if (!validator.test(board)) errors.addSuppressed(new ValidationException(msg)); | ||
132 | return this; | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * @return the validated board | ||
137 | * @throws ValidationException in case of failure | ||
138 | */ | ||
139 | public Board get() throws ValidationException { | ||
140 | if (errors.getSuppressed().length > 0) | ||
141 | throw errors; | ||
142 | else | ||
143 | return board; | ||
144 | } | 144 | } |
145 | 145 | ||
146 | } | 146 | } |