Skip to contentMethod: SSPPlayerBuilderImpl()
      1: package de.schereSteinPapier.domain.impl;
2: 
3: import static de.schereSteinPapier.SSPConstants.AuswahlConstants.PAPIER;
4: import static de.schereSteinPapier.SSPConstants.AuswahlConstants.SCHERE;
5: import static de.schereSteinPapier.SSPConstants.AuswahlConstants.STEIN;
6: 
7: import java.util.Collections;
8: import java.util.Map;
9: import java.util.Optional;
10: 
11: import de.fhdw.gaming.core.domain.GameException;
12: import de.schereSteinPapier.domain.SSPPlayer;
13: import de.schereSteinPapier.domain.SSPPlayerBuilder;
14: 
15: /**
16:  * Implements {@link SSPPlayerBuilder}.
17:  */
18: public final class SSPPlayerBuilderImpl implements SSPPlayerBuilder {
19: 
20:     /**
21:      * The name of the player.
22:      */
23:     private Optional<String> name;
24:     /**
25:      * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
26:      * for the second-level map is the answer of the second player.
27:      */
28:     private Optional<Map<String, Map<String, Double>>> possibleOutcomes;
29: 
30:     /**
31:      * Creates an {@link SSPPlayerBuilderImpl}.
32:      */
33:     SSPPlayerBuilderImpl() {
34:         this.name = Optional.empty();
35:         this.possibleOutcomes = Optional.empty();
36:     }
37: 
38:     @Override
39:     public SSPPlayerBuilderImpl changeName(final String newName) {
40:         this.name = Optional.of(newName);
41:         return this;
42:     }
43: 
44:     @Override
45:     public SSPPlayerBuilder changePossibleOutcomes(final Map<String, Map<String, Double>> newPossibleOutcomes) {
46:         this.possibleOutcomes = Optional.of(newPossibleOutcomes);
47:         return this;
48:     }
49: 
50:     @Override
51:     public SSPPlayer build() throws GameException {
52:         return new SSPPlayerImpl(
53:                 this.name.orElseThrow(),
54:                 this.checkPossibleOutcomes(this.possibleOutcomes.orElseThrow()));
55:     }
56: 
57:     /**
58:      * Checks if all possible outcomes are defined for a player.
59:      *
60:      * @param outcomes The possible outcomes for the player.
61:      */
62:     private Map<String, Map<String, Double>> checkPossibleOutcomes(
63:             final Map<String, Map<String, Double>> outcomes) {
64:         this.checkPossibleOutcome(outcomes, SCHERE, SCHERE);
65:         this.checkPossibleOutcome(outcomes, SCHERE, STEIN);
66:         this.checkPossibleOutcome(outcomes, SCHERE, PAPIER);
67:         this.checkPossibleOutcome(outcomes, STEIN, STEIN);
68:         this.checkPossibleOutcome(outcomes, STEIN, SCHERE);
69:         this.checkPossibleOutcome(outcomes, STEIN, PAPIER);
70:         this.checkPossibleOutcome(outcomes, PAPIER, PAPIER);
71:         this.checkPossibleOutcome(outcomes, PAPIER, STEIN);
72:         this.checkPossibleOutcome(outcomes, PAPIER, SCHERE);
73:         return outcomes;
74:     }
75: 
76:     /**
77:      * Checks if a given outcome is defined for a player.
78:      *
79:      * @param outcomes     The possible outcomes for the player.
80:      * @param firstChoice  The choice of the first player.
81:      * @param secondChoice The choice of the second player.
82:      */
83:     private void checkPossibleOutcome(final Map<String, Map<String, Double>> outcomes, final String firstChoice,
84:             final String secondChoice) {
85:         if (outcomes.getOrDefault(firstChoice, Collections.emptyMap()).get(secondChoice) == null) {
86:             throw new IllegalArgumentException(
87:                     String.format(
88:                             "No outcome defined for player '%s' and combination %s/%s.",
89:                             this.name,
90:                             firstChoice,
91:                             secondChoice));
92:         }
93:     }
94: 
95: //    /**
96: //     * Maps a boolean value to a "yes" or "no" answer.
97: //     *
98: //     * @param firstChoice The value to be mapped.
99: //     */
100: //    private static String toAnswer(final String firstChoice) {
101: //        return "yes";
102: //    }
103: }