Skip to content

Commit aa936a5

Browse files
committed
Use autogenerated equals & make enums more consistent
1 parent fa47f62 commit aa936a5

File tree

14 files changed

+86
-77
lines changed

14 files changed

+86
-77
lines changed

src/main/java/bwapi/Bullet.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import bwapi.ClientData.BulletData;
44

5+
import java.util.Objects;
6+
57
public class Bullet {
68
private final BulletData bulletData;
79
private final int id;
@@ -69,15 +71,16 @@ public boolean isVisible(final Player player) {
6971
return bulletData.isVisible(player.getID());
7072
}
7173

72-
public boolean equals(final Object that) {
73-
if (!(that instanceof Bullet)) {
74-
return false;
75-
}
76-
return getID() == ((Bullet) that).getID();
74+
@Override
75+
public boolean equals(Object o) {
76+
if (this == o) return true;
77+
if (o == null || getClass() != o.getClass()) return false;
78+
Bullet bullet = (Bullet) o;
79+
return id == bullet.id;
7780
}
7881

82+
@Override
7983
public int hashCode() {
80-
return getID();
84+
return Objects.hash(id);
8185
}
82-
8386
}

src/main/java/bwapi/Color.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ int blue() {
118118
}
119119

120120
@Override
121-
public boolean equals(final Object o) {
122-
if (!(o instanceof Color)) {
123-
return false;
124-
}
125-
return id == ((Color) o).id;
121+
public boolean equals(Object o) {
122+
if (this == o) return true;
123+
if (o == null || getClass() != o.getClass()) return false;
124+
Color color = (Color) o;
125+
return id == color.id;
126126
}
127127

128128
@Override

src/main/java/bwapi/Force.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import bwapi.ClientData.ForceData;
44

55
import java.util.List;
6+
import java.util.Objects;
67
import java.util.stream.Collectors;
78

89
public class Force {
@@ -31,14 +32,16 @@ public List<Player> getPlayers() {
3132
.collect(Collectors.toList());
3233
}
3334

34-
public boolean equals(final Object that) {
35-
if (!(that instanceof Force)) {
36-
return false;
37-
}
38-
return getID() == ((Force) that).getID();
35+
@Override
36+
public boolean equals(Object o) {
37+
if (this == o) return true;
38+
if (o == null || getClass() != o.getClass()) return false;
39+
Force force = (Force) o;
40+
return id == force.id;
3941
}
4042

43+
@Override
4144
public int hashCode() {
42-
return getID();
45+
return Objects.hash(id);
4346
}
4447
}

src/main/java/bwapi/Game.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,11 @@ public Position getMousePosition() {
420420
}
421421

422422
public boolean getMouseState(final MouseButton button) {
423-
return gameData.getMouseState(button.value);
423+
return gameData.getMouseState(button.id);
424424
}
425425

426426
public boolean getKeyState(final Key key) {
427-
return gameData.getKeyState(key.value);
427+
return gameData.getKeyState(key.id);
428428
}
429429

430430
public Position getScreenPosition() {

src/main/java/bwapi/Key.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ public enum Key {
230230
K_PA1(253),
231231
K_OEM_CLEAR(254);
232232

233-
final int value;
233+
final int id;
234234

235-
Key(final int value) {
236-
this.value = value;
235+
Key(final int id) {
236+
this.id = id;
237237
}
238238
}

src/main/java/bwapi/Latency.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public enum Latency {
1212
BattlenetMedium(19),
1313
BattlenetHigh(24);
1414

15-
final int id;
16-
17-
Latency(final int id) {
18-
this.id = id;
19-
}
20-
2115
static final Latency[] idToEnum = new Latency[24 + 1];
2216

2317
static {
2418
Arrays.stream(Latency.values()).forEach(v -> idToEnum[v.id] = v);
2519
}
20+
21+
final int id;
22+
23+
Latency(final int id) {
24+
this.id = id;
25+
}
2626
}

src/main/java/bwapi/MouseButton.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ public enum MouseButton {
66
M_MIDDLE(2),
77
M_MAX(3);
88

9-
final int value;
9+
final int id;
1010

11-
MouseButton(final int value) {
12-
this.value = value;
11+
MouseButton(final int id) {
12+
this.id = id;
1313
}
1414

1515
}

src/main/java/bwapi/Order.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ public enum Order {
195195
None(189),
196196
Unknown(190);
197197

198-
static final Order[] orders = new Order[190 + 1];
198+
static final Order[] idToEnum = new Order[190 + 1];
199199

200200
static {
201-
Arrays.stream(Order.values()).forEach(v -> orders[v.id] = v);
201+
Arrays.stream(Order.values()).forEach(v -> idToEnum[v.id] = v);
202202
}
203203

204204

src/main/java/bwapi/Player.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import bwapi.ClientData.PlayerData;
44

55
import java.util.List;
6+
import java.util.Objects;
67
import java.util.stream.Collectors;
78

89
import static bwapi.UnitType.*;
@@ -24,7 +25,7 @@ public class Player {
2425
this.game = game;
2526
this.id = id;
2627
this.name = playerData.getName();
27-
this.race = Race.races[playerData.getRace()];
28+
this.race = Race.idToEnum[playerData.getRace()];
2829
this.playerType = PlayerType.idToEnum[playerData.getType()];
2930
this.force = game.getForce(playerData.getForce());
3031
this.startLocation = new TilePosition(playerData.getStartLocationX(), playerData.getStartLocationY());
@@ -396,14 +397,16 @@ public boolean hasUnitTypeRequirement(final UnitType unit, final int amount) {
396397
}
397398
}
398399

399-
public boolean equals(final Object that) {
400-
if (!(that instanceof Player)) {
401-
return false;
402-
}
403-
return getID() == ((Player) that).getID();
400+
@Override
401+
public boolean equals(Object o) {
402+
if (this == o) return true;
403+
if (o == null || getClass() != o.getClass()) return false;
404+
Player player = (Player) o;
405+
return id == player.id;
404406
}
405407

408+
@Override
406409
public int hashCode() {
407-
return getID();
410+
return Objects.hash(id);
408411
}
409412
}

src/main/java/bwapi/Race.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public enum Race {
1616
None(7),
1717
Unknown(8);
1818

19-
public static final Race[] races = new Race[8 + 1];
2019
static final UnitType[] workerTypes = {
2120
Zerg_Drone, Terran_SCV, Protoss_Probe,
2221
UnitType.None, UnitType.None, UnitType.None, // unused
@@ -43,8 +42,10 @@ public enum Race {
4342
UnitType.Unknown, UnitType.None, UnitType.Unknown // random, none, unk
4443
};
4544

45+
static final Race[] idToEnum = new Race[8 + 1];
46+
4647
static {
47-
Arrays.stream(Race.values()).forEach(v -> races[v.id] = v);
48+
Arrays.stream(Race.values()).forEach(v -> idToEnum[v.id] = v);
4849
}
4950

5051
final int id;

0 commit comments

Comments
 (0)