Skip to content

Method: equals(Object)

1: package de.fhdw.gaming.ipspiel23.c4.domain.impl;
2:
3: import java.util.Optional;
4:
5: import de.fhdw.gaming.ipspiel23.c4.domain.C4Direction;
6: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Board;
7: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Position;
8: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Field;
9: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Player;
10:
11: /**
12: * The default implementation of {@link IC4Field}.
13: */
14: public class C4FieldHeavy implements IC4Field {
15:
16: /**
17: * The parent board that contains this field.
18: */
19: private final IC4Board board;
20:
21: /**
22: * The lazily initialized {@link IC4Position} instance that represents the position of this field.
23: */
24: private final IC4Position position;
25:
26: /**
27: * The player who occupies the field.
28: */
29: private Optional<IC4Player> occupyingPlayer;
30:
31: /**
32: * Creates a new instance of {@link C4Field}.
33: *
34: * @param board The parent board that contains this field.
35: * @param position The position of this field.
36: */
37: public C4FieldHeavy(final IC4Board board, final IC4Position position) {
38: this.board = board;
39: this.position = position;
40: this.occupyingPlayer = Optional.empty();
41: }
42:
43: @Override
44: public IC4Board getBoard() {
45: return this.board;
46: }
47:
48: @Override
49: public IC4Position getBoardPosition() {
50: return this.position;
51: }
52:
53: @Override
54: public Optional<IC4Player> getOccupyingPlayer() {
55: return this.occupyingPlayer;
56: }
57:
58: @Override
59: public boolean trySetOccupyingPlayer(final IC4Player player, final boolean allowOverride) {
60: if (this.occupyingPlayer.isPresent() && !allowOverride) {
61: return false;
62: }
63: this.occupyingPlayer = Optional.ofNullable(player);
64: return true;
65: }
66:
67: @Override
68: public boolean hasNeighbor(final C4Direction direction) {
69: final IC4Position neighborPosition = direction.stepFrom(this.getBoardPosition(), 1);
70: return this.getBoard().checkBounds(neighborPosition);
71: }
72:
73: @Override
74: public IC4Field getNeighbor(final C4Direction direction) {
75: return getNeighborInternal(direction, true);
76: }
77:
78: @Override
79: public Optional<IC4Field> tryGetNeighbor(final C4Direction direction) {
80: final IC4Field neighbor = getNeighborInternal(direction, false);
81: return Optional.ofNullable(neighbor);
82: }
83:
84: /**
85: * Gets the neighbor of this field in the provided direction.
86: *
87: * @param direction The direction to get the neighbor in.
88: * @param throwOob Whether to throw an {@link IndexOutOfBoundsException} if the neighbor is
89: * out of bounds, or to return null.
90: * @return The neighbor of this field in the provided direction, or null if the neighbor is
91: * out of bounds and throwOob is false.
92: */
93: private IC4Field getNeighborInternal(final C4Direction direction, final boolean throwOob) {
94: final IC4Position neighborPosition = direction.stepFrom(this.getBoardPosition(), 1);
95:
96: if (!this.getBoard().checkBounds(neighborPosition)) {
97: if (throwOob) {
98: throw new IndexOutOfBoundsException("The provided direction violates the bounds of the game board.");
99: }
100: return null;
101: }
102: return this.getBoard().tryGetField(neighborPosition).orElseThrow();
103: }
104:
105: @Override
106: public boolean equals(final Object obj) {
107:• if (obj == null) {
108: return false;
109: }
110:• if (!(obj instanceof IC4Field)) {
111: return false;
112: }
113: final IC4Field other = (IC4Field) obj;
114:• return this.getBoardPosition().equals(other.getBoardPosition())
115:• && this.getBoard().equals(other.getBoard());
116: }
117:
118: @Override
119: public int hashCode() {
120: int hash = 7;
121: hash = hash * 31 + this.getBoardPosition().hashCode();
122: hash = hash * 31 + this.getBoard().hashCode();
123: return hash;
124: }
125:
126: @Override
127: public String toString() {
128: final StringBuilder sb = new StringBuilder(32);
129: sb.append("C4FieldHeavy[player=").append(this.getOccupyingPlayer().map(IC4Player::getName).orElse("empty"))
130: .append(", position=").append(this.getBoardPosition())
131: .append(']');
132: return sb.toString();
133: }
134:
135: @Override
136: public IC4Field deepCopy() {
137: return new C4FieldHeavy(this.board, this.position);
138: }
139: }