Skip to content

Commit e14d930

Browse files
committed
improve point equals & hashcode
1 parent fa3e9c1 commit e14d930

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

src/main/java/bwapi/Point.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ protected double getDistance(final int x, final int y) {
3636
}
3737

3838
public boolean equals(final Object o) {
39-
if (!(o instanceof Point)) {
40-
return false;
39+
if (o != null && this.getClass().equals(o.getClass())) {
40+
final Point point = (Point) o;
41+
return x == point.x && y == point.y;
4142
}
42-
final Point point = (Point) o;
43-
return scalar == point.scalar && x == point.x && y == point.y;
43+
return false;
44+
4445
}
4546

4647
/**
@@ -53,7 +54,6 @@ public boolean isValid(final Game game) {
5354
}
5455

5556
public int hashCode() {
56-
//alternatively return Objects.hash(x, y); ?
57-
return (x << 16) + y;
57+
return (x << 16) ^ y;
5858
}
5959
}

src/test/java/bwapi/PointTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package bwapi;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertNotEquals;
7+
8+
9+
public class PointTest {
10+
11+
@Test
12+
public void pointEqualsTest() {
13+
assertEquals(new TilePosition(1, 1), new TilePosition(1, 1));
14+
15+
assertNotEquals(new TilePosition(1, 1), new TilePosition(1, 2));
16+
assertNotEquals(new TilePosition(1, 1), new TilePosition(2, 1));
17+
assertNotEquals(new TilePosition(1, 1), new TilePosition(2, 2));
18+
19+
assertNotEquals(new TilePosition(1, 1), new Position(1, 1));
20+
21+
assertNotEquals(new TilePosition(1, 1), null);
22+
}
23+
}
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package bwapi;
22

3-
4-
5-
import org.junit.Assert;
63
import org.junit.Test;
74

85
import static org.junit.Assert.assertEquals;
96

7+
108
public class UnitTypeContainerTest {
9+
1110
@Test
1211
public void checkContainerSizes() {
1312
final int expected = UnitType.values().length;
14-
Assert.assertEquals(UnitTypeContainer.defaultArmorAmount.length, expected);
15-
Assert.assertEquals(UnitTypeContainer.defaultOreCost.length, expected);
16-
Assert.assertEquals(UnitTypeContainer.defaultGasCost.length, expected);
17-
Assert.assertEquals(UnitTypeContainer.defaultTimeCost.length, expected);
18-
Assert.assertEquals(UnitTypeContainer.unitSupplyProvided.length, expected);
19-
Assert.assertEquals(UnitTypeContainer.unitSupplyRequired.length, expected);
20-
Assert.assertEquals(UnitTypeContainer.unitSpaceRequired.length, expected);
21-
Assert.assertEquals(UnitTypeContainer.unitSpaceProvided.length, expected);
22-
Assert.assertEquals(UnitTypeContainer.unitBuildScore.length, expected);
23-
Assert.assertEquals(UnitTypeContainer.unitDestroyScore.length, expected);
13+
assertEquals(UnitTypeContainer.defaultArmorAmount.length, expected);
14+
assertEquals(UnitTypeContainer.defaultOreCost.length, expected);
15+
assertEquals(UnitTypeContainer.defaultGasCost.length, expected);
16+
assertEquals(UnitTypeContainer.defaultTimeCost.length, expected);
17+
assertEquals(UnitTypeContainer.unitSupplyProvided.length, expected);
18+
assertEquals(UnitTypeContainer.unitSupplyRequired.length, expected);
19+
assertEquals(UnitTypeContainer.unitSpaceRequired.length, expected);
20+
assertEquals(UnitTypeContainer.unitSpaceProvided.length, expected);
21+
assertEquals(UnitTypeContainer.unitBuildScore.length, expected);
22+
assertEquals(UnitTypeContainer.unitDestroyScore.length, expected);
2423
}
2524
}

0 commit comments

Comments
 (0)