Skip to contentMethod: NonInteractiveInputProvider()
      1: /*
2:  * Copyright © 2020 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of gaming-core.
5:  *
6:  * Gaming-core is free software: you can redistribute it and/or modify it under
7:  * the terms of the GNU General Public License as published by the Free Software
8:  * Foundation, either version 3 of the License, or (at your option) any later
9:  * version.
10:  *
11:  * Gaming-core is distributed in the hope that it will be useful, but WITHOUT
12:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13:  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14:  * details.
15:  *
16:  * You should have received a copy of the GNU General Public License along with
17:  * gaming-core. If not, see <http://www.gnu.org/licenses/>.
18:  */
19: package de.fhdw.gaming.core.ui.util;
20: 
21: import java.util.LinkedHashMap;
22: import java.util.Map;
23: import java.util.Optional;
24: import java.util.Set;
25: 
26: import de.fhdw.gaming.core.domain.GameBuilder;
27: import de.fhdw.gaming.core.ui.InputProvider;
28: import de.fhdw.gaming.core.ui.InputProviderException;
29: import de.fhdw.gaming.core.ui.type.BooleanFieldType;
30: import de.fhdw.gaming.core.ui.type.FieldType;
31: import de.fhdw.gaming.core.ui.type.IntegerFieldType;
32: import de.fhdw.gaming.core.ui.type.ObjectFieldType;
33: import de.fhdw.gaming.core.ui.type.StringFieldType;
34: import de.fhdw.gaming.core.ui.type.validator.Validator;
35: 
36: /**
37:  * A non-interactive {@link InputProvider} implementation returning predefined data only.
38:  * <p>
39:  * Unlike interactive input providers, it is possible to create chains of {@link NonInteractiveInputProvider}s in
40:  * advance. This is helpful e.g. when creating a {@link GameBuilder}, as building a game requires parameters for the
41:  * game as a whole as well as parameters for each participating player. Each parameter set is expected to be returned by
42:  * a separate {@link InputProvider}.
43:  */
44: public final class NonInteractiveInputProvider implements InputProvider {
45: 
46:     /**
47:      * The provided data.
48:      */
49:     private final Map<String, Object> providedData;
50:     /**
51:      * The needed data.
52:      */
53:     private final Map<String, FieldType<?>> neededData;
54: 
55:     /**
56:      * Creates a {@link NonInteractiveInputProvider}.
57:      */
58:     public NonInteractiveInputProvider() {
59:         this.providedData = new LinkedHashMap<>();
60:         this.neededData = new LinkedHashMap<>();
61:     }
62: 
63:     @Override
64:     @SafeVarargs
65:     public final NonInteractiveInputProvider needString(final String id, final String prompt,
66:             final Optional<String> defaultValue, final Validator<String>... validators) {
67: 
68:         this.neededData.put(id, new StringFieldType(defaultValue).validateBy(validators));
69:         return this;
70:     }
71: 
72:     @Override
73:     public NonInteractiveInputProvider fixedString(final String id, final String fixedValue) {
74:         this.providedData.put(id, fixedValue);
75:         return this;
76:     }
77: 
78:     @Override
79:     @SafeVarargs
80:     public final NonInteractiveInputProvider needInteger(final String id, final String prompt,
81:             final Optional<Integer> defaultValue, final Validator<Integer>... validators) {
82: 
83:         this.neededData.put(id, new IntegerFieldType(defaultValue).validateBy(validators));
84:         return this;
85:     }
86: 
87:     @Override
88:     public NonInteractiveInputProvider fixedInteger(final String id, final Integer fixedValue) {
89:         this.providedData.put(id, fixedValue);
90:         return this;
91:     }
92: 
93:     @Override
94:     @SafeVarargs
95:     public final NonInteractiveInputProvider needBoolean(final String id, final String prompt,
96:             final Optional<Boolean> defaultValue, final Validator<Boolean>... validators) {
97: 
98:         this.neededData.put(id, new BooleanFieldType(defaultValue).validateBy(validators));
99:         return this;
100:     }
101: 
102:     @Override
103:     public NonInteractiveInputProvider fixedBoolean(final String id, final Boolean fixedValue) {
104:         this.providedData.put(id, fixedValue);
105:         return this;
106:     }
107: 
108:     @Override
109:     public InputProvider needObject(final String id, final String prompt, final Optional<Object> defaultValue,
110:             final Set<? extends Object> objectSet) {
111: 
112:         this.neededData.put(id, new ObjectFieldType(defaultValue, objectSet));
113:         return this;
114:     }
115: 
116:     @Override
117:     public InputProvider fixedObject(final String id, final Object fixedValue) {
118:         this.providedData.put(id, fixedValue);
119:         return this;
120:     }
121: 
122:     /**
123:      * {@inheritDoc}
124:      * <p>
125:      * Note that if no predefined value is available for a given field, {@code null} is stored.
126:      */
127:     @Override
128:     public Map<String, Object> requestData(final String title) throws InputProviderException {
129:         final Map<String, Object> result = new LinkedHashMap<>();
130:         for (final Map.Entry<String, FieldType<?>> entry : this.neededData.entrySet()) {
131:             final FieldType<?> fieldType = entry.getValue();
132:             result.put(
133:                     entry.getKey(),
134:                     this.providedData.getOrDefault(entry.getKey(), fieldType.getDefaultValue().orElse(null)));
135:         }
136: 
137:         return result;
138:     }
139: 
140:     @Override
141:     public InputProvider getNext(final Map<String, Object> lastDataSet) {
142:         return new NonInteractiveInputProvider();
143:     }
144: }