Skip to content

Method: FGPlayerImpl(FGPlayer)

1: package de.fhdw.gaming.ipspiel24.fg.domain.impl;
2:
3: import java.util.Collections;
4: import java.util.LinkedHashMap;
5: import java.util.Map;
6: import java.util.Objects;
7: import java.util.Optional;
8:
9: import de.fhdw.gaming.core.domain.AbstractPlayer;
10: import de.fhdw.gaming.ipspiel24.fg.domain.FGActivity;
11: import de.fhdw.gaming.ipspiel24.fg.domain.FGPlayer;
12:
13: /**
14: * Implements {@link FGPlayer}.
15: */
16: final class FGPlayerImpl extends AbstractPlayer<FGPlayer> implements FGPlayer {
17:
18: /**
19: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
20: * for the second-level map is the answer of the second player.
21: */
22: private final Map<FGActivity, Map<FGActivity, Double>> possibleOutcomes;
23: /**
24: * The answer of the player.
25: */
26: private Optional<FGActivity> answer;
27:
28: /**
29: * Creates a FG player.
30: *
31: * @param name The name of the player.
32: * @param possibleOutcomes The possible outcomes of this player. The key for the first-level map is the answer of
33: * the first player, the key for the second-level map is the answer of the second player.
34: */
35: FGPlayerImpl(final String name,
36: final Map<FGActivity, Map<FGActivity, Double>> possibleOutcomes) {
37: super(name);
38: this.possibleOutcomes = Collections
39: .unmodifiableMap(new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
40: this.answer = Optional.empty();
41: }
42:
43: /**
44: * Creates a FG player.
45: *
46: * @param source The {@link FGPlayer} to copy.
47: */
48: FGPlayerImpl(final FGPlayer source) {
49: super(source);
50: this.possibleOutcomes = source.getPossibleOutcomes();
51: this.answer = source.getAnswer();
52: }
53:
54: @Override
55: public String toString() {
56: return String
57: .format("FGPlayer[name=%s, state=%s, outcome=%s, answer=%s]", this.getName(), this.getState(),
58: this.getOutcome(),
59: this.answer);
60: }
61:
62: @Override
63: public boolean equals(final Object obj) {
64: if (obj instanceof FGPlayerImpl) {
65: final FGPlayerImpl other = (FGPlayerImpl) obj;
66: return super.equals(obj) && this.answer.equals(other.answer)
67: && this.possibleOutcomes.equals(other.possibleOutcomes);
68: }
69: return false;
70: }
71:
72: @SuppressWarnings("PMD.UselessOverridingMethod")
73: @Override
74: public int hashCode() {
75: return super.hashCode() ^ Objects.hash(this.answer, this.possibleOutcomes);
76: }
77:
78: @Override
79: public Map<FGActivity, Map<FGActivity, Double>> getPossibleOutcomes() {
80: return this.possibleOutcomes;
81: }
82:
83: @Override
84: public Optional<FGActivity> getAnswer() {
85: return this.answer;
86: }
87:
88: @Override
89: public void setAnswer(final FGActivity newAnswer) {
90: if (this.answer.isPresent()) {
91: throw new IllegalStateException(String.format("Player %s tried to change her answer.", this.getName()));
92: }
93: this.answer = Optional.of(newAnswer);
94: }
95:
96: @Override
97: public FGPlayer deepCopy() {
98: return new FGPlayerImpl(this);
99: }
100: }