Skip to contentMethod: validate(Integer)
      1: package de.fhdw.gaming.ipspiel23.c4.domain.impl.validation;
2: 
3: import java.util.function.Consumer;
4: 
5: import de.fhdw.gaming.core.ui.InputProviderException;
6: import de.fhdw.gaming.core.ui.type.validator.Validator;
7: 
8: /**
9:  * Validates and stores user-supplied values against a shared instances of {@link C4BoardLimits}
10:  * in order to handle cross dependencies between the different allowed board settings.
11:  */
12: public final class C4BoardValidator implements Validator<Integer> {
13: 
14:     /**
15:      * The limits to validate against.
16:      */
17:     private final C4BoardLimits state;
18: 
19:     /**
20:      * The action to execute when the validator is called.
21:      */
22:     private final Consumer<C4BoardValidationStateUpdateContext> updateAction;
23: 
24:     /**
25:      * The update context to use when the validator is called.
26:      */
27:     private final C4BoardValidationStateUpdateContext updateContext;
28: 
29:     /**
30:      * The info to display when the validator is called.
31:      */
32:     private final C4BoardValidatorInfo info;
33: 
34:     /**
35:      * Creates a new instance of {@link C4BoardValidator}.
36:      * 
37:      * @param state The limits to validate against.
38:      * @param updateAction The action used to update the validation state.
39:      * @param info The info to display when the validator is called.
40:      */
41:     public C4BoardValidator(final C4BoardLimits state, 
42:             final Consumer<C4BoardValidationStateUpdateContext> updateAction, 
43:             final C4BoardValidatorInfo info) {
44:         this.updateAction = updateAction;
45:         this.state = state;
46:         this.updateContext = new C4BoardValidationStateUpdateContext(state, 0);
47:         this.info = info;
48:     }
49: 
50:     @Override
51:     public Integer validate(final Integer value) throws InputProviderException {
52:         this.updateContext.setValue(value);
53:         this.updateAction.accept(this.updateContext);
54:         this.state.assertIsValid();
55:         return value;
56:     }
57: 
58:     @Override
59:     public String getInfo() {
60:         return this.info.toInfoString(this.state.getPlayerCount());
61:     }
62: }