瀏覽代碼

Merge pull request #254 from exercism/rectangles-polish

rectangles: polish example implementation
John Ryan 9 年之前
父節點
當前提交
01cb7e1089
共有 1 個檔案被更改,包括 30 行新增22 行删除
  1. 30
    22
      exercises/rectangles/src/example/java/RectangleCounter.java

+ 30
- 22
exercises/rectangles/src/example/java/RectangleCounter.java 查看文件

15
 
15
 
16
     private static final class Grid {
16
     private static final class Grid {
17
 
17
 
18
-        private enum Entry {
18
+        private enum Tile {
19
             CORNER,
19
             CORNER,
20
-            HLINE,
21
-            VLINE,
20
+            HORIZONTAL_WALL,
21
+            VERTICAL_WALL,
22
             SPACE;
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
                     case '+': return CORNER;
26
                     case '+': return CORNER;
27
-                    case '-': return HLINE;
28
-                    case '|': return VLINE;
27
+                    case '-': return HORIZONTAL_WALL;
28
+                    case '|': return VERTICAL_WALL;
29
                     case ' ': return SPACE;
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
         private int nRows, nCols;
43
         private int nRows, nCols;
36
-        private final Entry[][] entries;
44
+        private final Tile[][] tiles;
37
 
45
 
38
         private Grid(final int nRows, final int nCols, final String[] rawGrid) {
46
         private Grid(final int nRows, final int nCols, final String[] rawGrid) {
39
             this.nRows = nRows;
47
             this.nRows = nRows;
40
             this.nCols = nCols;
48
             this.nCols = nCols;
41
-            this.entries = new Entry[nRows][nCols];
49
+            this.tiles = new Tile[nRows][nCols];
42
 
50
 
43
             for (int nRow = 0; nRow < nRows; nRow++) {
51
             for (int nRow = 0; nRow < nRows; nRow++) {
44
                 for (int nCol = 0; nCol < nCols; nCol++) {
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
                 final int leftCol,
84
                 final int leftCol,
77
                 final int rightCol) {
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
                     && isHorizontalLineSegment(topRow, leftCol, rightCol)
91
                     && isHorizontalLineSegment(topRow, leftCol, rightCol)
84
                     && isHorizontalLineSegment(bottomRow, leftCol, rightCol)
92
                     && isHorizontalLineSegment(bottomRow, leftCol, rightCol)
85
                     && isVerticalLineSegment(leftCol, topRow, bottomRow)
93
                     && isVerticalLineSegment(leftCol, topRow, bottomRow)
88
 
96
 
89
         private boolean isHorizontalLineSegment(final int row, final int leftCol, final int rightCol) {
97
         private boolean isHorizontalLineSegment(final int row, final int leftCol, final int rightCol) {
90
             return stream(copyOfRange(getRow(row), leftCol, rightCol))
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
         private boolean isVerticalLineSegment(final int col, final int topRow, final int bottomRow) {
102
         private boolean isVerticalLineSegment(final int col, final int topRow, final int bottomRow) {
95
             return stream(copyOfRange(getCol(col), topRow, bottomRow))
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
             for (int nRow = 0; nRow < nRows; nRow++) {
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
             return result;
118
             return result;