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