Skip to content

Method: hashCode()

1: package de.fhdw.gaming.ipspiel23.dilemma.domain.internals;
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.dilemma.domain.DilemmaAnswerType;
11: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaPlayer;
12: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaStrategy;
13:
14: /**
15: * Implements {@link IDilemmaPlayer}.
16: */
17: public final class DilemmaPlayer extends AbstractPlayer<IDilemmaPlayer> implements IDilemmaPlayer {
18:
19: /**
20: * The answer of the player.
21: */
22: private Optional<DilemmaAnswerType> answer;
23:
24: /**
25: * The strategy of the player.
26: */
27: private IDilemmaStrategy strategy;
28:
29: /**
30: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
31: * for the second-level map is the answer of the second player.
32: */
33: private final Map<DilemmaAnswerType, Map<DilemmaAnswerType, Double>> possibleOutcomes;
34:
35: /**
36: * Creates a Dilemma player.
37: *
38: * @param builder The parent player builder that is used to create this player.
39: * @param name The name of the player.
40: * @param possibleOutcomes The possible outcomes of this player as DilemmaAnswerType.
41: * The key for the first-level map is the answer of the first player,
42: * the key for the second-level map is the answer of the second player.
43: */
44: DilemmaPlayer(final DilemmaPlayerBuilder builder, final String name,
45: final Map<DilemmaAnswerType, Map<DilemmaAnswerType, Double>> possibleOutcomes) {
46: super(name);
47: this.possibleOutcomes = Collections
48: .unmodifiableMap(new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
49: this.answer = Optional.empty();
50:
51: // Register the strategy hook
52: // This hook is used to carefully expose the strategy setter to the builder
53: // and nothing else.
54: // the builder will then use the hook to inject the strategy into the player.
55: builder.registerPlayerStrategyHook(this, newStrategy -> this.strategy = newStrategy);
56: }
57:
58: /**
59: * Creates a Dilemma player.
60: *
61: * @param source The {@link IDilemmaPlayer} to copy.
62: */
63: DilemmaPlayer(final IDilemmaPlayer source) {
64: super(source);
65: this.possibleOutcomes = source.getPossibleOutcomes();
66: this.answer = source.getAnswer();
67: this.strategy = source.getStrategy();
68: }
69:
70: @Override
71: public boolean equals(final Object object) {
72: if (object instanceof DilemmaPlayer) {
73: final DilemmaPlayer other = (DilemmaPlayer) object;
74: return super.equals(object) && this.answer.equals(other.answer)
75: && this.possibleOutcomes.equals(other.possibleOutcomes);
76: }
77: return false;
78: }
79:
80: @Override
81: public int hashCode() {
82: return Objects.hash(super.hashCode(), answer, possibleOutcomes);
83: }
84:
85: /**
86: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
87: * for the second-level map is the answer of the second player.
88: */
89: @Override
90: public Map<DilemmaAnswerType, Map<DilemmaAnswerType, Double>> getPossibleOutcomes() {
91: return this.possibleOutcomes;
92: }
93:
94: @Override
95: public Optional<DilemmaAnswerType> getAnswer() {
96: return this.answer;
97: }
98:
99: @Override
100: public void setAnswer(final DilemmaAnswerType newAnswer) {
101: if (this.answer.isPresent()) {
102: throw new IllegalStateException(String.format("Player %s tried to change her answer.", this.getName()));
103: }
104: this.answer = Optional.of(newAnswer);
105: }
106:
107: @Override
108: public IDilemmaPlayer deepCopy() {
109: return new DilemmaPlayer(this);
110: }
111:
112: /**
113: * Is called to provide the outcome of the game for the player.
114: */
115: @Override
116: public String toString() {
117: return String
118: .format("DilemmaPlayer[name=%s, state=%s, outcome=%s, answer=%s]",
119: this.getName(),
120: this.getState(),
121: this.getOutcome(),
122: this.answer);
123: }
124:
125: @Override
126: public IDilemmaStrategy getStrategy() {
127: if (this.strategy == null) {
128: throw new IllegalStateException("The strategy of player " + this.getName() + " was never set.");
129: }
130: return this.strategy;
131: }
132: }