Skip to content

Method: getIndex()

1: /*
2: * Copyright © 2021 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel21-tictactoe-core.
5: *
6: * ipspiel21-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: * ipspiel21-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: * ipspiel21-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel21.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.ipspiel21.tictactoe.core.domain.TicTacToeField;
26: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeFieldState;
27: import de.fhdw.gaming.ipspiel21.tictactoe.core.domain.TicTacToeRow;
28: import de.fhdw.gaming.ipspiel21.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: * The zero-based index of this row.
41: */
42: private final int index;
43: /**
44: * The fields of this row.
45: */
46: private final List<? extends TicTacToeField> fields;
47:
48: /**
49: * Creates a row.
50: *
51: * @param type The type of this row.
52: * @param index The zero-based index of this row.
53: * @param fields The fields of this row.
54: */
55: public TicTacToeRowImpl(final TicTacToeRowType type, final int index, final List<? extends TicTacToeField> fields) {
56: this.type = type;
57: this.index = index;
58: this.fields = fields;
59: }
60:
61: @Override
62: public boolean equals(final Object obj) {
63: if (obj instanceof TicTacToeRowImpl) {
64: final TicTacToeRowImpl other = (TicTacToeRowImpl) obj;
65: return this.type.equals(other.type) && this.index == other.index && this.fields.equals(other.fields);
66: }
67: return false;
68: }
69:
70: @Override
71: public int hashCode() {
72: return Objects.hash(this.type, this.index, this.fields);
73: }
74:
75: @Override
76: public String toString() {
77: return String.format("%s@%d%s", this.type, this.index, this.fields);
78: }
79:
80: @Override
81: public TicTacToeRowType getType() {
82: return this.type;
83: }
84:
85: @Override
86: public int getIndex() {
87: return this.index;
88: }
89:
90: @Override
91: public List<? extends TicTacToeField> getFields() {
92: return this.fields;
93: }
94:
95: @Override
96: public Optional<TicTacToeFieldState> getState() {
97: TicTacToeFieldState lastState = null;
98: for (final TicTacToeField field : this.fields) {
99: if (lastState == null) {
100: lastState = field.getState();
101: } else if (!lastState.equals(field.getState())) {
102: return Optional.empty();
103: }
104: }
105: return Optional.ofNullable(lastState); // may be empty if row is
106: }
107: }