Explorar el Código

Polish example rectangles implementation

Stuart Kent hace 9 años
padre
commit
5119e20e4b
Se han modificado 1 ficheros con 30 adiciones y 22 borrados
  1. 30
    22
      exercises/rectangles/src/example/java/RectangleCounter.java

+ 30
- 22
exercises/rectangles/src/example/java/RectangleCounter.java Ver fichero

@@ -15,34 +15,42 @@ final class RectangleCounter {
15 15
 
16 16
     private static final class Grid {
17 17
 
18
-        private enum Entry {
18
+        private enum Tile {
19 19
             CORNER,
20
-            HLINE,
21
-            VLINE,
20
+            HORIZONTAL_WALL,
21
+            VERTICAL_WALL,
22 22
             SPACE;
23 23
 
24
-            private static Entry fromChar(final char rawGridEntry) {
25
-                switch (rawGridEntry) {
24
+            private static Tile fromChar(final char rawGridTile) {
25
+                switch (rawGridTile) {
26 26
                     case '+': return CORNER;
27
-                    case '-': return HLINE;
28
-                    case '|': return VLINE;
27
+                    case '-': return HORIZONTAL_WALL;
28
+                    case '|': return VERTICAL_WALL;
29 29
                     case ' ': return SPACE;
30
-                    default:  throw new IllegalStateException("Grid entry " + rawGridEntry + " not recognized.");
30
+                    default:  throw new IllegalStateException("Grid tile " + rawGridTile + " not recognized.");
31 31
                 }
32 32
             }
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
+            }
33 41
         }
34 42
 
35 43
         private int nRows, nCols;
36
-        private final Entry[][] entries;
44
+        private final Tile[][] tiles;
37 45
 
38 46
         private Grid(final int nRows, final int nCols, final String[] rawGrid) {
39 47
             this.nRows = nRows;
40 48
             this.nCols = nCols;
41
-            this.entries = new Entry[nRows][nCols];
49
+            this.tiles = new Tile[nRows][nCols];
42 50
 
43 51
             for (int nRow = 0; nRow < nRows; nRow++) {
44 52
                 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));
46 54
                 }
47 55
             }
48 56
         }
@@ -76,10 +84,10 @@ final class RectangleCounter {
76 84
                 final int leftCol,
77 85
                 final int rightCol) {
78 86
 
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)
83 91
                     && isHorizontalLineSegment(topRow, leftCol, rightCol)
84 92
                     && isHorizontalLineSegment(bottomRow, leftCol, rightCol)
85 93
                     && isVerticalLineSegment(leftCol, topRow, bottomRow)
@@ -88,23 +96,23 @@ final class RectangleCounter {
88 96
 
89 97
         private boolean isHorizontalLineSegment(final int row, final int leftCol, final int rightCol) {
90 98
             return stream(copyOfRange(getRow(row), leftCol, rightCol))
91
-                    .allMatch(entry -> entry.equals(Entry.HLINE) || entry.equals(Entry.CORNER));
99
+                    .allMatch(Tile::isHorizontalConnector);
92 100
         }
93 101
 
94 102
         private boolean isVerticalLineSegment(final int col, final int topRow, final int bottomRow) {
95 103
             return stream(copyOfRange(getCol(col), topRow, bottomRow))
96
-                    .allMatch(entry -> entry.equals(Entry.VLINE) || entry.equals(Entry.CORNER));
104
+                    .allMatch(Tile::isVerticalConnector);
97 105
         }
98 106
 
99
-        private Entry[] getRow(final int number) {
100
-            return entries[number];
107
+        private Tile[] getRow(final int number) {
108
+            return tiles[number];
101 109
         }
102 110
 
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];
105 113
 
106 114
             for (int nRow = 0; nRow < nRows; nRow++) {
107
-                result[nRow] = entries[nRow][number];
115
+                result[nRow] = tiles[nRow][number];
108 116
             }
109 117
 
110 118
             return result;