Skip to content

Package: KopfundZahlundKantePlayerBuilderImpl

KopfundZahlundKantePlayerBuilderImpl

nameinstructionbranchcomplexitylinemethod
KopfundZahlundKantePlayerBuilderImpl()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
build()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
changeName(String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
changePossibleOutcomes(Map)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
checkPossibleOutcome(Map, KopfundZahlundKanteAnswerEnum, KopfundZahlundKanteAnswerEnum)
M: 21 C: 9
30%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 2
50%
M: 0 C: 1
100%
checkPossibleOutcomes(Map)
M: 0 C: 47
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%

Coverage

1: package de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.impl;
2:
3: import java.util.Collections;
4: import java.util.Map;
5: import java.util.Optional;
6:
7: import de.fhdw.gaming.core.domain.GameException;
8: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.KopfundZahlundKanteAnswerEnum;
9: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.KopfundZahlundKantePlayer;
10: import de.fhdw.gaming.ipspiel22.kopfundzahlundkante.domain.KopfundZahlundKantePlayerBuilder;
11:
12: /**
13: * Implements {@link KopfundZahlundKantePlayerBuilder}.
14: */
15: public final class KopfundZahlundKantePlayerBuilderImpl implements KopfundZahlundKantePlayerBuilder {
16:
17: /**
18: * The name of the player.
19: */
20: private Optional<String> name;
21:
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<KopfundZahlundKanteAnswerEnum, Map<KopfundZahlundKanteAnswerEnum, Double>>> possibleOutcomes;
27:
28: /**
29: * Creates an {@link KopfundZahlundKantePlayerBuilderImpl}.
30: */
31: public KopfundZahlundKantePlayerBuilderImpl() {
32: this.name = Optional.empty();
33: this.possibleOutcomes = Optional.empty();
34: }
35:
36: @Override
37: public KopfundZahlundKantePlayerBuilderImpl changeName(final String newName) {
38: this.name = Optional.of(newName);
39: return this;
40: }
41:
42: @Override
43: public KopfundZahlundKantePlayerBuilder changePossibleOutcomes(final Map<KopfundZahlundKanteAnswerEnum,
44: Map<KopfundZahlundKanteAnswerEnum,
45: Double>> newpossibleOutcomes) {
46: this.possibleOutcomes = Optional.of(newpossibleOutcomes);
47: return this;
48: }
49:
50: @Override
51: public KopfundZahlundKantePlayer build() throws GameException {
52: return new KopfundZahlundKantePlayerImpl(
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<KopfundZahlundKanteAnswerEnum, Map<KopfundZahlundKanteAnswerEnum, Double>> checkPossibleOutcomes(
63: final Map<KopfundZahlundKanteAnswerEnum, Map<KopfundZahlundKanteAnswerEnum, Double>> outcomes) {
64: this.checkPossibleOutcome(outcomes, KopfundZahlundKanteAnswerEnum.TAIL, KopfundZahlundKanteAnswerEnum.HEAD);
65: this.checkPossibleOutcome(outcomes, KopfundZahlundKanteAnswerEnum.TAIL, KopfundZahlundKanteAnswerEnum.EDGE);
66: this.checkPossibleOutcome(outcomes, KopfundZahlundKanteAnswerEnum.TAIL, KopfundZahlundKanteAnswerEnum.TAIL);
67: this.checkPossibleOutcome(outcomes, KopfundZahlundKanteAnswerEnum.HEAD, KopfundZahlundKanteAnswerEnum.TAIL);
68: this.checkPossibleOutcome(outcomes, KopfundZahlundKanteAnswerEnum.HEAD, KopfundZahlundKanteAnswerEnum.HEAD);
69: this.checkPossibleOutcome(outcomes, KopfundZahlundKanteAnswerEnum.HEAD, KopfundZahlundKanteAnswerEnum.EDGE);
70: this.checkPossibleOutcome(outcomes, KopfundZahlundKanteAnswerEnum.EDGE, KopfundZahlundKanteAnswerEnum.HEAD);
71: this.checkPossibleOutcome(outcomes, KopfundZahlundKanteAnswerEnum.EDGE, KopfundZahlundKanteAnswerEnum.TAIL);
72: this.checkPossibleOutcome(outcomes, KopfundZahlundKanteAnswerEnum.EDGE, KopfundZahlundKanteAnswerEnum.EDGE);
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<KopfundZahlundKanteAnswerEnum, Map<KopfundZahlundKanteAnswerEnum,
84: Double>> outcomes, final KopfundZahlundKanteAnswerEnum firstChoice,
85: final KopfundZahlundKanteAnswerEnum secondChoice) {
86:• if (outcomes.getOrDefault(firstChoice, Collections.emptyMap()).get(secondChoice) == null) {
87: throw new IllegalArgumentException(
88: String.format(
89: "No outcome defined for player '%s' and combination %s/%s.",
90: this.name,
91: firstChoice,
92: secondChoice));
93: }
94: }
95:
96: }