Skip to content

Commit 5119e20

Browse files
committed
Polish example rectangles implementation
1 parent 63020d4 commit 5119e20

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

‎exercises/rectangles/src/example/java/RectangleCounter.java‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,42 @@ int countRectangles(final String[] rawGrid) {
1515

1616
private static final class Grid {
1717

18-
private enum Entry {
18+
private enum Tile {
1919
CORNER,
20-
HLINE,
21-
VLINE,
20+
HORIZONTAL_WALL,
21+
VERTICAL_WALL,
2222
SPACE;
2323

24-
private static Entry fromChar(final char rawGridEntry) {
25-
switch (rawGridEntry) {
24+
private static Tile fromChar(final char rawGridTile) {
25+
switch (rawGridTile) {
2626
case '+': return CORNER;
27-
case '-': return HLINE;
28-
case '|': return VLINE;
27+
case '-': return HORIZONTAL_WALL;
28+
case '|': return VERTICAL_WALL;
2929
case ' ': return SPACE;
30-
default: throw new IllegalStateException("Grid entry " + rawGridEntry + " not recognized.");
30+
default: throw new IllegalStateException("Grid tile " + rawGridTile + " not recognized.");
3131
}
3232
}
33+
34+
private boolean isHorizontalConnector() {
35+
return this == CORNER || this == HORIZONTAL_WALL;
36+
}
37+
38+
private boolean isVerticalConnector() {
39+
return this == CORNER || this == VERTICAL_WALL;
40+
}
3341
}
3442

3543
private int nRows, nCols;
36-
private final Entry[][] entries;
44+
private final Tile[][] tiles;
3745

3846
private Grid(final int nRows, final int nCols, final String[] rawGrid) {
3947
this.nRows = nRows;
4048
this.nCols = nCols;
41-
this.entries = new Entry[nRows][nCols];
49+
this.tiles = new Tile[nRows][nCols];
4250

4351
for (int nRow = 0; nRow < nRows; nRow++) {
4452
for (int nCol = 0; nCol < nCols; nCol++) {
45-
entries[nRow][nCol] = Entry.fromChar(rawGrid[nRow].charAt(nCol));
53+
tiles[nRow][nCol] = Tile.fromChar(rawGrid[nRow].charAt(nCol));
4654
}
4755
}
4856
}
@@ -76,10 +84,10 @@ private boolean formsRectangle(
7684
final int leftCol,
7785
final int rightCol) {
7886

79-
return entries[topRow][leftCol].equals(Entry.CORNER)
80-
&& entries[topRow][rightCol].equals(Entry.CORNER)
81-
&& entries[bottomRow][leftCol].equals(Entry.CORNER)
82-
&& entries[bottomRow][rightCol].equals(Entry.CORNER)
87+
return tiles[topRow][leftCol].equals(Tile.CORNER)
88+
&& tiles[topRow][rightCol].equals(Tile.CORNER)
89+
&& tiles[bottomRow][leftCol].equals(Tile.CORNER)
90+
&& tiles[bottomRow][rightCol].equals(Tile.CORNER)
8391
&& isHorizontalLineSegment(topRow, leftCol, rightCol)
8492
&& isHorizontalLineSegment(bottomRow, leftCol, rightCol)
8593
&& isVerticalLineSegment(leftCol, topRow, bottomRow)
@@ -88,23 +96,23 @@ && isVerticalLineSegment(leftCol, topRow, bottomRow)
8896

8997
private boolean isHorizontalLineSegment(final int row, final int leftCol, final int rightCol) {
9098
return stream(copyOfRange(getRow(row), leftCol, rightCol))
91-
.allMatch(entry -> entry.equals(Entry.HLINE) || entry.equals(Entry.CORNER));
99+
.allMatch(Tile::isHorizontalConnector);
92100
}
93101

94102
private boolean isVerticalLineSegment(final int col, final int topRow, final int bottomRow) {
95103
return stream(copyOfRange(getCol(col), topRow, bottomRow))
96-
.allMatch(entry -> entry.equals(Entry.VLINE) || entry.equals(Entry.CORNER));
104+
.allMatch(Tile::isVerticalConnector);
97105
}
98106

99-
private Entry[] getRow(final int number) {
100-
return entries[number];
107+
private Tile[] getRow(final int number) {
108+
return tiles[number];
101109
}
102110

103-
private Entry[] getCol(final int number) {
104-
final Entry[] result = new Entry[nRows];
111+
private Tile[] getCol(final int number) {
112+
final Tile[] result = new Tile[nRows];
105113

106114
for (int nRow = 0; nRow < nRows; nRow++) {
107-
result[nRow] = entries[nRow][number];
115+
result[nRow] = tiles[nRow][number];
108116
}
109117

110118
return result;

0 commit comments

Comments
 (0)