Skip to content

Commit ad4c99b

Browse files
author
bytekeeper
committed
Changes from PR review.
1 parent 3d400cd commit ad4c99b

File tree

7 files changed

+46
-118
lines changed

7 files changed

+46
-118
lines changed

src/main/java/bwapi/ClientData.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -596,17 +596,17 @@ public Shape(int myOffset) {
596596
}
597597
ShapeType getType() {
598598
int offset = myOffset + 0;
599-
return ShapeType.withId(sharedMemory.getInt(offset));
599+
return ShapeType.idToEnum[sharedMemory.getInt(offset)];
600600
}
601601
void setType(ShapeType value) {
602-
sharedMemory.putInt(myOffset + 0, value.getId());
602+
sharedMemory.putInt(myOffset + 0, value.id);
603603
}
604604
CoordinateType getCtype() {
605605
int offset = myOffset + 4;
606-
return CoordinateType.withId(sharedMemory.getInt(offset));
606+
return CoordinateType.idToEnum[sharedMemory.getInt(offset)];
607607
}
608608
void setCtype(CoordinateType value) {
609-
sharedMemory.putInt(myOffset + 4, value.getId());
609+
sharedMemory.putInt(myOffset + 4, value.id);
610610
}
611611
int getX1() {
612612
int offset = myOffset + 8;
@@ -672,10 +672,10 @@ public Command(int myOffset) {
672672
}
673673
CommandType getType() {
674674
int offset = myOffset + 0;
675-
return CommandType.withId(sharedMemory.getInt(offset));
675+
return CommandType.idToEnum[sharedMemory.getInt(offset)];
676676
}
677677
void setType(CommandType value) {
678-
sharedMemory.putInt(myOffset + 0, value.getId());
678+
sharedMemory.putInt(myOffset + 0, value.id);
679679
}
680680
int getValue1() {
681681
int offset = myOffset + 4;
@@ -721,10 +721,10 @@ public Event(int myOffset) {
721721
}
722722
EventType getType() {
723723
int offset = myOffset + 0;
724-
return EventType.withId(sharedMemory.getInt(offset));
724+
return EventType.idToEnum[sharedMemory.getInt(offset)];
725725
}
726726
void setType(EventType value) {
727-
sharedMemory.putInt(myOffset + 0, value.getId());
727+
sharedMemory.putInt(myOffset + 0, value.id);
728728
}
729729
int getV1() {
730730
int offset = myOffset + 4;
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bwapi;
22

3-
public enum CommandType implements WithId {
3+
import java.util.Arrays;
4+
5+
public enum CommandType {
46
None(0),
57
SetScreenPosition(1),
68
PingMinimap(2),
@@ -21,28 +23,15 @@ public enum CommandType implements WithId {
2123
SetCommandOptimizerLevel(17),
2224
SetRevealAll(18);
2325

24-
public final int value;
25-
26-
CommandType(final int value) {
27-
this.value = value;
28-
}
29-
30-
@Override
31-
public int getId() {
32-
return value;
33-
}
26+
static final CommandType[] idToEnum = new CommandType[19];
3427

35-
static CommandType withId(int id) {
36-
if (id < 0) return null;
37-
CommandType commandType = IdMapper.commandTypeForId[id];
38-
if (commandType == null) {
39-
throw new IllegalArgumentException("No CommandType with id " + id);
40-
}
41-
return commandType;
28+
static {
29+
Arrays.stream(CommandType.values()).forEach(v -> idToEnum[v.id] = v);
4230
}
4331

44-
private static class IdMapper {
32+
public final int id;
4533

46-
static final CommandType[] commandTypeForId = IdMapperHelper.toIdTypeArray(CommandType.class);
34+
CommandType(final int id) {
35+
this.id = id;
4736
}
4837
}
Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
package bwapi;
22

3-
public enum CoordinateType implements WithId {
3+
import java.util.Arrays;
4+
5+
public enum CoordinateType {
46
None(0),
57
Screen(1),
68
Map(2),
79
Mouse(3);
810

9-
public final int value;
10-
11-
CoordinateType(final int value) {
12-
this.value = value;
13-
}
14-
15-
@Override
16-
public int getId() {
17-
return value;
18-
}
11+
static final CoordinateType[] idToEnum = new CoordinateType[4];
1912

20-
static CoordinateType withId(int id) {
21-
if (id < 0) return null;
22-
CoordinateType coordinateType = IdMapper.coordinateTypes[id];
23-
if (coordinateType == null) {
24-
throw new IllegalArgumentException("No CoordinateType with id " + id);
25-
}
26-
return coordinateType;
13+
static {
14+
Arrays.stream(CoordinateType.values()).forEach(v -> idToEnum[v.id] = v);
2715
}
2816

29-
private static class IdMapper {
17+
public final int id;
3018

31-
static final CoordinateType[] coordinateTypes = IdMapperHelper.toIdTypeArray(CoordinateType.class);
19+
CoordinateType(final int id) {
20+
this.id = id;
3221
}
33-
3422
}

src/main/java/bwapi/EventType.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bwapi;
22

3-
public enum EventType implements WithId {
3+
import java.util.Arrays;
4+
5+
public enum EventType {
46
MatchStart(0),
57
MatchEnd(1),
68
MatchFrame(2),
@@ -22,32 +24,16 @@ public enum EventType implements WithId {
2224
//TriggerAction,
2325
None(18);
2426

25-
private final int value;
26-
27-
EventType(int value) {
28-
29-
this.value = value;
30-
}
27+
static final EventType[] idToEnum = new EventType[19];
3128

32-
@Override
33-
public int getId() {
34-
return value;
29+
static {
30+
Arrays.stream(EventType.values()).forEach(v -> idToEnum[v.id] = v);
3531
}
3632

37-
static EventType withId(int id) {
38-
if (id < 0) {
39-
return null;
40-
}
41-
EventType eventType = IdMapper.eventTypes[id];
42-
if (eventType == null) {
43-
throw new IllegalArgumentException("No EventType with id " + id);
44-
}
45-
return eventType;
46-
}
33+
final int id;
4734

48-
private static class IdMapper {
35+
EventType(int id) {
4936

50-
static final EventType[] eventTypes = IdMapperHelper.toIdTypeArray(EventType.class);
37+
this.id = id;
5138
}
52-
5339
}

src/main/java/bwapi/IdMapperHelper.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/java/bwapi/ShapeType.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bwapi;
22

3-
enum ShapeType implements WithId {
3+
import java.util.Arrays;
4+
5+
enum ShapeType {
46
None(0),
57
Text(1),
68
Box(2),
@@ -10,28 +12,15 @@ enum ShapeType implements WithId {
1012
Dot(6),
1113
Line(7);
1214

13-
public final int value;
14-
15-
ShapeType(final int value) {
16-
this.value = value;
17-
}
18-
19-
@Override
20-
public int getId() {
21-
return value;
22-
}
15+
static final ShapeType[] idToEnum = new ShapeType[8];
2316

24-
static ShapeType withId(int id) {
25-
if (id < 0) return null;
26-
ShapeType shapeType = IdMapper.shapeTypeForId[id];
27-
if (shapeType == null) {
28-
throw new IllegalArgumentException("No ShapeType with id " + id);
29-
}
30-
return shapeType;
17+
static {
18+
Arrays.stream(ShapeType.values()).forEach(v -> idToEnum[v.id] = v);
3119
}
3220

33-
private static class IdMapper {
21+
public final int id;
3422

35-
static final ShapeType[] shapeTypeForId = IdMapperHelper.toIdTypeArray(ShapeType.class);
23+
ShapeType(final int id) {
24+
this.id = id;
3625
}
3726
}

src/test/java/DumpToClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static void main(String[] args) throws IOException {
188188
out.printf("Buffers.toString(sharedMemory, offset, %d)", v.arraySizes.size());
189189
break;
190190
case ENUM:
191-
out.print(v.enumName + ".withId(sharedMemory.getInt(offset))");
191+
out.print(v.enumName + ".idToEnum[sharedMemory.getInt(offset)]");
192192
break;
193193
case DOUBLE:
194194
out.print("sharedMemory.getDouble(offset)");
@@ -220,7 +220,7 @@ public static void main(String[] args) throws IOException {
220220
break;
221221
case ENUM:
222222
out.printf("%s value) {\n", v.enumName);
223-
out.printf(" sharedMemory.putInt(%s, value.getId());\n", offsetString);
223+
out.printf(" sharedMemory.putInt(%s, value.id);\n", offsetString);
224224
break;
225225
case UNSIGNED_SHORT:
226226
out.print("short value) {");

0 commit comments

Comments
 (0)