Skip to contentMethod: toString()
      1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel23-Ssp.
5:  *
6:  * Ipspiel23-Ssp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7:  * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
8:  * version.
9:  *
10:  * Ipspiel23-Ssp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
11:  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12:  *
13:  * You should have received a copy of the GNU General Public License along with ipspiel23-Ssp. If not, see
14:  * <http://www.gnu.org/licenses/>.
15:  */
16: package de.fhdw.gaming.ipspiel23.ssp.domain.impl;
17: 
18: import java.util.Collections;
19: import java.util.LinkedHashMap;
20: import java.util.Map;
21: import java.util.Objects;
22: import java.util.Optional;
23: 
24: import de.fhdw.gaming.core.domain.AbstractPlayer;
25: import de.fhdw.gaming.ipspiel23.ssp.domain.SspPlayer;
26: import de.fhdw.gaming.ipspiel23.ssp.domain.impl.outcomes.SspAnswer;
27: 
28: /**
29:  * Implements {@link SspPlayer}.
30:  */
31: final class SspPlayerImpl extends AbstractPlayer<SspPlayer> implements SspPlayer {
32: 
33:     /**
34:      * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
35:      * for the second-level map is the answer of the second player.
36:      */
37:     private final Map<SspAnswer, Map<SspAnswer, Double>> possibleOutcomes;
38:     /**
39:      * The answer of the player.
40:      */
41:     private Optional<SspAnswer> answer;
42: 
43:     /**
44:      * Creates a Ssp player.
45:      *
46:      * @param name             The name of the player.
47:      * @param possibleOutcomes The possible outcomes of this player. The key for the first-level map is the answer of
48:      *                         the first player, the key for the second-level map is the answer of the second player.
49:      */
50:     SspPlayerImpl(final String name,
51:             final Map<SspAnswer, Map<SspAnswer, Double>> possibleOutcomes) {
52:         super(name);
53:         this.possibleOutcomes = Collections
54:                 .unmodifiableMap(new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
55:         this.answer = Optional.empty();
56:     }
57: 
58:     /**
59:      * Creates a Ssp player.
60:      *
61:      * @param source The {@link SspPlayer} to copy.
62:      */
63:     SspPlayerImpl(final SspPlayer source) {
64:         super(source);
65:         this.possibleOutcomes = source.getPossibleOutcomes();
66:         this.answer = source.getAnswer();
67:     }
68: 
69:     @Override
70:     public String toString() {
71:         return String
72:                 .format("SspPlayer[name=%s, state=%s, outcome=%s, answer=%s]", this.getName(), this.getState(),
73:                         this.getOutcome(),
74:                         this.answer);
75:     }
76: 
77:     @Override
78:     public boolean equals(final Object obj) {
79:         if (obj instanceof SspPlayerImpl) {
80:             final SspPlayerImpl other = (SspPlayerImpl) obj;
81:             return super.equals(obj) && this.answer.equals(other.answer)
82:                     && this.possibleOutcomes.equals(other.possibleOutcomes);
83:         }
84:         return false;
85:     }
86: 
87:     @Override
88:     public int hashCode() {
89:         return super.hashCode() ^ Objects.hash(this.answer, this.possibleOutcomes);
90:     }
91: 
92:     @Override
93:     public Map<SspAnswer, Map<SspAnswer, Double>> getPossibleOutcomes() {
94:         return this.possibleOutcomes;
95:     }
96: 
97:     @Override
98:     public Optional<SspAnswer> getAnswer() {
99:         return this.answer;
100:     }
101: 
102:     @Override
103:     public void setAnswer(final SspAnswer newAnswer) {
104:         if (this.answer.isPresent()) {
105:             throw new IllegalStateException(String.format("Player %s tried to change her answer.", this.getName()));
106:         }
107:         this.answer = Optional.of(newAnswer);
108:     }
109: 
110:     @Override
111:     public SspPlayer deepCopy() {
112:         return new SspPlayerImpl(this);
113:     }
114: }