Skip to content

Method: getFields()

1: /*
2: * Copyright © 2021 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel23-tictactoe-core.
5: *
6: * ipspiel23-tictactoe-core is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * ipspiel23-tictactoe-core is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * ipspiel23-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel23.tictactoe.core.domain.impl;
20:
21: import java.util.List;
22: import java.util.Objects;
23: import java.util.Optional;
24:
25: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeField;
26: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeFieldState;
27: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeRow;
28: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeRowType;
29:
30: /**
31: * Implements {@link TicTacToeRow}.
32: */
33: public final class TicTacToeRowImpl implements TicTacToeRow {
34:
35: /**
36: * The type of this row.
37: */
38: private final TicTacToeRowType type;
39:
40: /**
41: * The zero-based index of this row.
42: */
43: private final int index;
44:
45: /**
46: * The fields of this row.
47: */
48: private final List<? extends TicTacToeField> fields;
49:
50: /**
51: * Creates a row.
52: *
53: * @param type The type of this row.
54: * @param index The zero-based index of this row.
55: * @param fields The fields of this row.
56: */
57: public TicTacToeRowImpl(final TicTacToeRowType type, final int index, final List<? extends TicTacToeField> fields) {
58: this.type = type;
59: this.index = index;
60: this.fields = fields;
61: }
62:
63: @Override
64: public boolean equals(final Object obj) {
65: if (obj instanceof TicTacToeRowImpl) {
66: final TicTacToeRowImpl other = (TicTacToeRowImpl) obj;
67: return this.type.equals(other.type) && this.index == other.index && this.fields.equals(other.fields);
68: }
69: return false;
70: }
71:
72: @Override
73: public int hashCode() {
74: return Objects.hash(this.type, this.index, this.fields);
75: }
76:
77: @Override
78: public String toString() {
79: return String.format("%s@%d%s", this.type, this.index, this.fields);
80: }
81:
82: @Override
83: public TicTacToeRowType getType() {
84: return this.type;
85: }
86:
87: @Override
88: public int getIndex() {
89: return this.index;
90: }
91:
92: @Override
93: public List<? extends TicTacToeField> getFields() {
94: return this.fields;
95: }
96:
97: @Override
98: public Optional<TicTacToeFieldState> getState() {
99: TicTacToeFieldState lastState = null;
100: for (final TicTacToeField field : this.fields) {
101: if (lastState == null) {
102: lastState = field.getState();
103: } else if (!lastState.equals(field.getState())) {
104: return Optional.empty();
105: }
106: }
107: return Optional.ofNullable(lastState); // may be empty if row is
108: }
109: }