Skip to content

Package: GDPlayerBuilderImpl

GDPlayerBuilderImpl

nameinstructionbranchcomplexitylinemethod
GDPlayerBuilderImpl()
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, boolean, boolean)
M: 23 C: 11
32%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 4 C: 2
33%
M: 0 C: 1
100%
checkPossibleOutcomes(Map)
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
toAnswer(boolean)
M: 0 C: 6
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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