Skip to content

Method: build()

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