Skip to content

Commit efb8534

Browse files
committed
Make Point and its subclasses more consistent
1 parent 3827fb1 commit efb8534

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

src/main/java/bwapi/Point.java

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

3-
class Point implements Comparable<Point>{
3+
public abstract class Point<T extends Point> implements Comparable<Point>{
44
static final int TILE_WALK_FACTOR = 4; // 32 / 8
55

66
public final int x;
77
public final int y;
88
private final int scalar;
99

10-
Point(final int x, final int y, final int type) {
10+
protected Point(final int x, final int y, final int type) {
1111
this.x = x;
1212
this.y = y;
1313
this.scalar = type;
@@ -29,9 +29,9 @@ public String toString() {
2929
return "[" + x + ", " + y + "]";
3030
}
3131

32-
protected double getDistance(final int x, final int y) {
33-
final int dx = x - this.x;
34-
final int dy = y - this.y;
32+
public double getDistance(T point) {
33+
final int dx = point.x - this.x;
34+
final int dy = point.y - this.y;
3535
return Math.sqrt(dx * dx + dy * dy);
3636
}
3737

@@ -52,10 +52,17 @@ private static int getApproxDistance(final int x1, final int y1, final int x2, f
5252
return (minCalc >> 5) + minCalc + max - (max >> 4) - (max >> 6);
5353
}
5454

55-
public int getApproxDistance(final Point point) {
55+
public int getApproxDistance(final T point) {
5656
return getApproxDistance(x, y, point.x, point.y);
5757
}
5858

59+
public abstract T subtract(T other);
60+
61+
public abstract T add(T other);
62+
63+
public abstract T divide(int divisor);
64+
65+
public abstract T multiply(int multiplier);
5966

6067
@Override
6168
public boolean equals(Object o) {

src/main/java/bwapi/Position.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package bwapi;
22

33

4-
public class Position extends Point {
4+
public class Position extends Point<Position> {
55

66
public static final int SIZE_IN_PIXELS = 1;
77
public static final Position Invalid = new Position(32000 / SIZE_IN_PIXELS, 32000 / SIZE_IN_PIXELS);
@@ -40,12 +40,4 @@ public Position divide(final int divisor) {
4040
public Position multiply(final int multiplier) {
4141
return new Position(x * multiplier, y * multiplier);
4242
}
43-
44-
public double getDistance(final Position position) {
45-
return getDistance(position.x, position.y);
46-
}
47-
48-
public int getApproxDistance(Position position) {
49-
return super.getApproxDistance(position);
50-
}
5143
}

src/main/java/bwapi/TilePosition.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package bwapi;
22

3-
public class TilePosition extends Point {
3+
public class TilePosition extends Point<TilePosition> {
44

55
public static final int SIZE_IN_PIXELS = 32;
66
public static final TilePosition Invalid = new TilePosition(32000 / SIZE_IN_PIXELS, 32000 / SIZE_IN_PIXELS);
@@ -39,8 +39,4 @@ public TilePosition divide(final int divisor) {
3939
public TilePosition multiply(final int multiplier) {
4040
return new TilePosition(x * multiplier, y * multiplier);
4141
}
42-
43-
public double getDistance(final TilePosition position) {
44-
return getDistance(position.x, position.y);
45-
}
4642
}

src/main/java/bwapi/WalkPosition.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package bwapi;
22

3-
public class WalkPosition extends Point {
3+
public class WalkPosition extends Point<WalkPosition> {
44

55
public static final int SIZE_IN_PIXELS = 8;
66
public static final WalkPosition Invalid = new WalkPosition(32000 / SIZE_IN_PIXELS, 32000 / SIZE_IN_PIXELS);
@@ -35,8 +35,4 @@ public WalkPosition divide(final int divisor) {
3535
public WalkPosition multiply(final int multiplier) {
3636
return new WalkPosition(x * multiplier, y * multiplier);
3737
}
38-
39-
public double getDistance(final WalkPosition position) {
40-
return getDistance(position.x, position.y);
41-
}
4238
}

src/test/java/bwapi/PointTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,13 @@ public void pointEqualsTest() {
2020

2121
assertNotEquals(new TilePosition(1, 1), null);
2222
}
23+
24+
@Test
25+
public void pointDistanceAccesibleTest() {
26+
TilePosition tp = TilePosition.Origin;
27+
28+
assertEquals(0, tp.getApproxDistance(tp));
29+
assertEquals(0, tp.getDistance(tp), 0.001);
30+
assertEquals(0 , tp.getLength(), 0.001);
31+
}
2332
}

0 commit comments

Comments
 (0)