ソースを参照

robot-simulator: improve equals for GridPosition

We recently improved the implementation of equals for GridPosition so
that we could use assertEquals in the tests. However, we only changed
the reference implementation, not the starter implementation. This
changes the starter implementation to be the same as the reference
implementation.
Frida Tveit 9 年 前
コミット
e783c6d807
共有1 個のファイルを変更した19 個の追加15 個の削除を含む
  1. 19
    15
      exercises/robot-simulator/src/main/java/GridPosition.java

+ 19
- 15
exercises/robot-simulator/src/main/java/GridPosition.java ファイルの表示

9
         this.y = y;
9
         this.y = y;
10
     }
10
     }
11
 
11
 
12
-    /*
13
-     * This equals method is of deliberately narrow scope (only allows comparison with another GridPosition) to increase
14
-     * readability. In general, one should provide a full implementation of Object.equals(Object obj) and a
15
-     * corresponding implementation of Object.hashCode(). See
16
-     *
17
-     * https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)
18
-     *
19
-     * and
20
-     *
21
-     * https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()
22
-     *
23
-     * for more information.
24
-     */
25
-    boolean equals(final GridPosition gridPosition) {
26
-        return this.x == gridPosition.x && this.y == gridPosition.y;
12
+    @Override
13
+    public int hashCode() {
14
+        final int prime = 31;
15
+        int result = 1;
16
+        result = prime * result + x;
17
+        result = prime * result + y;
18
+        return result;
27
     }
19
     }
28
 
20
 
21
+    @Override
22
+    public boolean equals(Object obj) {
23
+        if (this == obj) {
24
+            return true;
25
+        } else if (obj == null || getClass() != obj.getClass()) {
26
+            return false;
27
+        } else if (x != ((GridPosition) obj).x || y != ((GridPosition)obj).y) {
28
+            return false;
29
+        } else {
30
+            return true;
31
+        }
32
+    }
29
 }
33
 }