Skip to content

Method: equals(Object)

1: package de.fhdw.gaming.ipspiel23.ht.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.ipspiel23.ht.domain.Answer;
11: import de.fhdw.gaming.ipspiel23.ht.domain.IHTPlayer;
12:
13: /**
14: * Represents an Heads or Tails player implementation.
15: */
16: final class HTPlayer extends AbstractPlayer<IHTPlayer> implements IHTPlayer {
17:
18: /**
19: * The possible outcomes of the player.
20: */
21: private final Map<Answer, Map<Answer, Double>> possibleOutcomes;
22:
23: /**
24: * The answer of the player.
25: */
26: private Optional<Answer> answer = Optional.empty();
27:
28: /**
29: * Creates a new instance of {@link HTPlayer}.
30: *
31: * @param name The name of the player.
32: * @param possibleOutcomes The possible outcomes of the player.
33: */
34: protected HTPlayer(final String name, final Map<Answer, Map<Answer, Double>> possibleOutcomes) {
35: super(name);
36: this.possibleOutcomes = Collections
37: .unmodifiableMap(new LinkedHashMap<>(Objects
38: .requireNonNull(possibleOutcomes, "possibleOutcomes")));
39: }
40:
41: /**
42: * Creates a new instance of {@link HTPlayer}.
43: *
44: * @param source The source player.
45: */
46: protected HTPlayer(final IHTPlayer source) {
47: super(source);
48: possibleOutcomes = source.getPossibleOutcomes();
49: answer = source.getAnswer();
50: }
51:
52: @Override
53: public IHTPlayer deepCopy() {
54: return new HTPlayer(this);
55: }
56:
57: @Override
58: public Map<Answer, Map<Answer, Double>> getPossibleOutcomes() {
59: return possibleOutcomes;
60: }
61:
62: @Override
63: public Optional<Answer> getAnswer() {
64: return answer;
65: }
66:
67: @Override
68: public void setAnswer(final Answer newAnswer) {
69: if (answer.isEmpty()) {
70: answer = Optional.of(newAnswer);
71: return;
72: }
73: throw new IllegalStateException(String.format("Player '%s' tried to change their answer.", this.getName()));
74: }
75:
76: @Override
77: public String toString() {
78: return String
79: .format("%s[name=%s, state=%s, outcome=%s, answer=%s]",
80: this.getClass().getSimpleName(), this.getName(), this.getState(),
81: this.getOutcome(),
82: this.answer);
83: }
84:
85: @Override
86: public boolean equals(final Object obj) {
87:• if (obj instanceof HTPlayer) {
88: final HTPlayer other = (HTPlayer) obj;
89:• return super.equals(obj) && this.answer.equals(other.answer)
90:• && this.possibleOutcomes.equals(other.possibleOutcomes);
91: }
92: return false;
93: }
94:
95: @Override
96: public int hashCode() {
97: return super.hashCode() ^ Objects.hash(this.answer, this.possibleOutcomes);
98: }
99: }