Skip to content

Commit 389bb68

Browse files
committed
- remove methods not needed any more.
1 parent fe2f21a commit 389bb68

1 file changed

Lines changed: 13 additions & 43 deletions

File tree

  • 2026-01-23-Private_Constructors/src/main/java/de/hilling/chess
Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package de.hilling.chess;
22

3-
import java.util.ArrayList;
4-
import java.util.Collection;
53
import java.util.HashMap;
64
import java.util.Map;
75
import java.util.regex.Pattern;
@@ -12,23 +10,19 @@
1210
* Represents a position on the board.
1311
* Objects of this class are immutable, correct coordinates are checked when the constructor is called.
1412
*/
15-
// TODO:
16-
// * implement accessors.
17-
// * collections of valid positions in static initializer.
18-
// * then remove hashCode() and equals().
1913

20-
public class Position extends Object{
21-
private static final Pattern POSITION_PATTERN = Pattern.compile("[a-h][1-8]");
22-
private static final char[] X_CHARACTERS = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
23-
private static final Map<String, Position> VALID_POSITIONS = new HashMap<>();
14+
public class Position extends Object {
15+
private static final Pattern POSITION_PATTERN = Pattern.compile("[a-h][1-8]");
16+
private static final char[] X_CHARACTERS = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
17+
private static final Map<String, Position> VALID_POSITIONS = new HashMap<>();
2418

25-
public final int x;
26-
public final int y;
19+
public final int x;
20+
public final int y;
2721
public final String formattedPosition;
2822

2923
static {
30-
for(int x = 0; x < 8; x++) {
31-
for(int y = 0; y < 8; y++) {
24+
for (int x = 0; x < 8; x++) {
25+
for (int y = 0; y < 8; y++) {
3226
Position position = new Position(x, y);
3327
VALID_POSITIONS.put(position.formattedPosition, position);
3428
}
@@ -39,30 +33,25 @@ private Position(int x, int y) {
3933
validatePosition(x, y);
4034
this.x = x;
4135
this.y = y;
42-
formattedPosition = getFormattedPosition(x, y);
36+
formattedPosition = String.format("%c%d", X_CHARACTERS[x], y + 1);
4337
}
4438

4539
private static void validatePosition(int x, int y) {
46-
if(x <0 || y <0 || x >7 || y >7) {
40+
if (x < 0 || y < 0 || x > 7 || y > 7) {
4741
throw new IllegalArgumentException("x and y need to be between 0 and 7");
4842
}
4943
}
5044

51-
private static @NonNull String getFormattedPosition(int x, int y) {
52-
return String.format("%c%d", X_CHARACTERS[x], y + 1);
53-
}
54-
5545
/**
5646
* Access Position object via position string (chess notation).
5747
*
5848
* @param positionString position between "a1 and h8".
5949
* @return Position element
60-
*
6150
* @throws IllegalArgumentException if positionString is invalid.
6251
*/
6352
public static @NonNull Position of(@NonNull String positionString) {
6453
Position position = VALID_POSITIONS.get(positionString);
65-
if(position == null) {
54+
if (position == null) {
6655
throw new IllegalArgumentException("Illegal position: " + positionString);
6756
}
6857
return position;
@@ -74,34 +63,15 @@ private static void validatePosition(int x, int y) {
7463
* @param x x-offset, starting at 0.
7564
* @param y y-offset, starting at 0
7665
* @return Position element
77-
*
7866
* @throws IllegalArgumentException if x or y are not between 0 and 7.
7967
*/
8068
public static @NonNull Position of(int x, int y) {
8169
validatePosition(x, y);
82-
return of(getFormattedPosition(x, y));
70+
return of(String.format("%c%d", X_CHARACTERS[x], y + 1));
8371
}
8472

8573
@Override
8674
public String toString() {
8775
return formattedPosition;
8876
}
89-
90-
@Override
91-
public boolean equals(Object obj) {
92-
if (obj == this) {
93-
return true;
94-
}
95-
if (!(obj instanceof Position)) {
96-
return false;
97-
} else {
98-
Position other = (Position) obj;
99-
return x == other.x && y == other.y;
100-
}
101-
}
102-
103-
@Override
104-
public int hashCode() {
105-
return x + y * 8;
106-
}
107-
}
77+
}

0 commit comments

Comments
 (0)