Skip to content

Commit 8397b5e

Browse files
Minor improvements
1 parent d73ca87 commit 8397b5e

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/main/java/com/mapcode/Boundary.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ public Boundaries extendBounds(final int xExtension, final int yExtension) {
7272

7373
public boolean containsPoint(@Nonnull final Point p) {
7474
final int y = p.getLatMicroDeg();
75-
if ((miny <= y) && (y < maxy)) {
76-
int x = p.getLonMicroDeg();
77-
if ((minx <= x) && (x < maxx)) { return true; }
78-
if (x < minx) { x += 360000000; } else { x -= 360000000; }
79-
if ((minx <= x) && (x < maxx)) { return true; }
80-
}
81-
return false;
75+
if ((miny > y) || (y >= maxy)) { return false; }
76+
final int x = p.getLonMicroDeg();
77+
// longitude boundaries can extend (slightly) outside the [-180,180) range
78+
if (x < minx) { return (minx <= x + 360000000) && (x + 360000000 < maxx); }
79+
if (x >= maxx) { return (minx <= x - 360000000) && (x - 360000000 < maxx); }
80+
return true;
8281
}
8382

8483
public String toString() {

src/main/java/com/mapcode/DataAccess.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,24 @@ class DataAccess {
6262
final byte[] bytes = outputStream.toByteArray();
6363
assert total == bytes.length;
6464

65-
// read 8-byte header: SIGNATURE "MC", VERSION, NR TERRITORIES, NR RECTRANGLE RECORD
66-
assert total > 8;
65+
// read SIGNATURE "MC", VERSION
66+
assert total > 12;
6767
assert (char) bytes[0] == 'M';
6868
assert (char) bytes[1] == 'C';
6969
final int dataVersion = (bytes[2] & 255) + ((bytes[3] & 255) << 8);
70+
assert (dataVersion >= 220);
71+
final int HEADER_SIZE = 8;
72+
73+
// read header: NR TERRITORIES, NR RECTRANGLE RECORD
7074
nrTerritoryRecords = (bytes[4] & 255) + ((bytes[5] & 255) << 8);
7175
nrTerritories = (bytes[6] & 255) + ((bytes[7] & 255) << 8);
7276
LOG.info("version={} nrTerritories={} nrTerritoryRecords={}",dataVersion,nrTerritories,nrTerritoryRecords);
73-
assert (8 + ((nrTerritories + 1) * 2) + (nrTerritoryRecords * 20) == total);
77+
final int expectedsize = HEADER_SIZE + ((nrTerritories + 1) * 2) + (nrTerritoryRecords * 20);
78+
assert (expectedsize == total);
7479

7580
// read DATA+START array (2 bytes per territory, plus closing record)
7681
DATA_START = new int[nrTerritories + 1];
77-
int i = 8;
82+
int i = HEADER_SIZE;
7883
for (int k=0; k <= nrTerritories; k++) {
7984
DATA_START[k] = (bytes[i] & 255) + ((bytes[i + 1] & 255) << 8);
8085
i += 2;

0 commit comments

Comments
 (0)