Skip to content

Method: toString()

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel24-demo.
5: *
6: * Ipspiel24-demo 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: * Ipspiel24-demo 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 ipspiel24-demo. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.ipspiel24.demo.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.ipspiel24.demo.domain.DemoPlayer;
26:
27: /**
28: * Implements {@link DemoPlayer}.
29: */
30: final class DemoPlayerImpl extends AbstractPlayer<DemoPlayer> implements DemoPlayer {
31:
32: /**
33: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
34: * for the second-level map is the answer of the second player.
35: */
36: private final Map<Boolean, Map<Boolean, Double>> possibleOutcomes;
37: /**
38: * The answer of the player.
39: */
40: private Optional<Boolean> answer;
41:
42: /**
43: * Creates a Demo player.
44: *
45: * @param name The name of the player.
46: * @param possibleOutcomes The possible outcomes of this player. The key for the first-level map is the answer of
47: * the first player, the key for the second-level map is the answer of the second player.
48: */
49: DemoPlayerImpl(final String name,
50: final Map<Boolean, Map<Boolean, Double>> possibleOutcomes) {
51: super(name);
52: this.possibleOutcomes = Collections
53: .unmodifiableMap(new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
54: this.answer = Optional.empty();
55: }
56:
57: /**
58: * Creates a Demo player.
59: *
60: * @param source The {@link DemoPlayer} to copy.
61: */
62: DemoPlayerImpl(final DemoPlayer source) {
63: super(source);
64: this.possibleOutcomes = source.getPossibleOutcomes();
65: this.answer = source.getAnswer();
66: }
67:
68: @Override
69: public String toString() {
70: return String
71: .format("DemoPlayer[name=%s, state=%s, outcome=%s, answer=%s]", this.getName(), this.getState(),
72: this.getOutcome(),
73: this.answer);
74: }
75:
76: @Override
77: public boolean equals(final Object obj) {
78: if (obj instanceof DemoPlayerImpl) {
79: final DemoPlayerImpl other = (DemoPlayerImpl) obj;
80: return super.equals(obj) && this.answer.equals(other.answer)
81: && this.possibleOutcomes.equals(other.possibleOutcomes);
82: }
83: return false;
84: }
85:
86: @SuppressWarnings("PMD.UselessOverridingMethod")
87: @Override
88: public int hashCode() {
89: return super.hashCode() ^ Objects.hash(this.answer, this.possibleOutcomes);
90: }
91:
92: @Override
93: public Map<Boolean, Map<Boolean, Double>> getPossibleOutcomes() {
94: return this.possibleOutcomes;
95: }
96:
97: @Override
98: public Optional<Boolean> getAnswer() {
99: return this.answer;
100: }
101:
102: @Override
103: public void setAnswer(final boolean 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 DemoPlayer deepCopy() {
112: return new DemoPlayerImpl(this);
113: }
114: }