Skip to content

Commit 0ec5e53

Browse files
committed
Cleaned up
1 parent 94d95e6 commit 0ec5e53

File tree

17 files changed

+753
-603
lines changed

17 files changed

+753
-603
lines changed

src/main/java/com/mapcode/Alphabet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* This enum defines all alphabets supported for mapcodes. Note that an alphabet is different from a
2525
* language or locale. An alternative name for an alphabet is "script".
26-
*
26+
* <p/>
2727
* Mapcodes can be safely converted between alphabets and fed to the mapcode decoder in the regular
2828
* ASCII Roman alphabet or any other.
2929
*/

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

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,72 +16,83 @@
1616

1717
package com.mapcode;
1818

19-
import org.slf4j.Logger;
20-
import org.slf4j.LoggerFactory;
21-
2219
import javax.annotation.Nonnull;
23-
import javax.annotation.Nullable;
2420

2521
/**
2622
* ----------------------------------------------------------------------------------------------
2723
* Package private implementation class. For internal use within the mapcode implementation only.
2824
* ----------------------------------------------------------------------------------------------
29-
*
30-
* This class handles the territory ractangles for mapcodes.
25+
* <p/>
26+
* This class handles the territory rectangles for mapcodes.
3127
*/
32-
class Boundaries {
33-
private int minx, maxx, miny, maxy;
28+
class Boundary {
29+
private int minX;
30+
private int maxX;
31+
private int minY;
32+
private int maxY;
3433

35-
/**
36-
* Public interface
37-
* Note that after construction, all calls are safe.
38-
*/
34+
private Boundary() {
35+
// Disabled.
36+
}
3937

40-
public static Boundaries getBoundaries(final int m) {
41-
Boundaries b = new Boundaries();
42-
b.minx = DataAccess.minx(m);
43-
b.miny = DataAccess.miny(m);
44-
b.maxx = DataAccess.maxx(m);
45-
b.maxy = DataAccess.maxy(m);
46-
return b;
38+
// You have to use this factory method instead of a ctor.
39+
@Nonnull
40+
static Boundary createFromTerritoryRecord(final int territoryRecord) {
41+
Boundary boundary = new Boundary();
42+
boundary.minX = DataAccess.getMinX(territoryRecord);
43+
boundary.minY = DataAccess.getMinY(territoryRecord);
44+
boundary.maxX = DataAccess.getMaxX(territoryRecord);
45+
boundary.maxY = DataAccess.getMaxY(territoryRecord);
46+
return boundary;
4747
}
4848

49-
public int getMinX() {
50-
return minx;
49+
int getMinX() {
50+
return minX;
5151
}
5252

53-
public int getMinY() {
54-
return miny;
53+
int getMinY() {
54+
return minY;
5555
}
5656

57-
public int getMaxX() {
58-
return maxx;
57+
int getMaxX() {
58+
return maxX;
5959
}
6060

61-
public int getMaxY() {
62-
return maxy;
61+
int getMaxY() {
62+
return maxY;
6363
}
6464

65-
public Boundaries extendBounds(final int xExtension, final int yExtension) {
66-
minx -= xExtension;
67-
miny -= yExtension;
68-
maxx += xExtension;
69-
maxy += yExtension;
65+
@Nonnull
66+
Boundary extendBoundary(final int xExtension, final int yExtension) {
67+
minX -= xExtension;
68+
minY -= yExtension;
69+
maxX += xExtension;
70+
maxY += yExtension;
7071
return this;
7172
}
7273

73-
public boolean containsPoint(@Nonnull final Point p) {
74+
boolean containsPoint(@Nonnull final Point p) {
75+
if (!p.isDefined()) {
76+
return false;
77+
}
7478
final int y = p.getLatMicroDeg();
75-
if ((miny > y) || (y >= maxy)) { return false; }
79+
if ((minY > y) || (y >= maxY)) {
80+
return false;
81+
}
7682
final int x = p.getLonMicroDeg();
7783
// 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); }
84+
if (x < minX) {
85+
return (minX <= (x + 360000000)) && ((x + 360000000) < maxX);
86+
}
87+
if (x >= maxX) {
88+
return (minX <= (x - 360000000)) && ((x - 360000000) < maxX);
89+
}
8090
return true;
8191
}
8292

93+
@Nonnull
8394
public String toString() {
84-
return "[" + (miny / 1000000.0) + ", " + (maxy / 1000000.0) +
85-
"), [" + (minx / 1000000.0) + ", " + (maxx / 1000000.0) + ")";
95+
return "[" + (minY / 1000000.0) + ", " + (maxY / 1000000.0) +
96+
"), [" + (minX / 1000000.0) + ", " + (maxX / 1000000.0) + ')';
8697
}
8798
}

src/main/java/com/mapcode/CheckArgs.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mapcode;
1818

19+
import javax.annotation.CheckForNull;
1920
import javax.annotation.Nonnull;
2021
import javax.annotation.Nullable;
2122

@@ -26,6 +27,7 @@
2627
* Package private implementation class. For internal use within the Mapcode implementation only.
2728
* ----------------------------------------------------------------------------------------------
2829
*/
30+
@SuppressWarnings("OverlyBroadThrowsClause")
2931
class CheckArgs {
3032

3133
private CheckArgs() {
@@ -39,6 +41,14 @@ static void checkNonnull(@Nonnull final String param, @Nullable final Object obj
3941
}
4042
}
4143

44+
static void checkDefined(@Nonnull final String param, @Nonnull final Point point)
45+
throws IllegalArgumentException {
46+
checkNonnull(param, point);
47+
if (!point.isDefined()) {
48+
throw new IllegalArgumentException("Parameter " + param + " must be defined");
49+
}
50+
}
51+
4252
static void checkMapcodeCode(@Nonnull final String param, @Nullable final String code)
4353
throws IllegalArgumentException {
4454
checkNonnull(param, code);

src/main/java/com/mapcode/Common.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* ----------------------------------------------------------------------------------------------
2121
* Package private implementation class. For internal use within the Mapcode implementation only.
2222
* ----------------------------------------------------------------------------------------------
23-
*
23+
* <p/>
2424
* This class contains common data structures and methods used by the Mapcode implementation.
2525
*/
2626
class Common {
@@ -81,15 +81,15 @@ static int xDivider(final int minY, final int maxY) {
8181
static int countCityCoordinatesForCountry(final int sameCodex, final int index, final int firstCode) {
8282
final int i = getFirstNamelessRecord(sameCodex, index, firstCode);
8383
int e = index;
84-
while (Data.calcCodex(e) == sameCodex) {
84+
while (Data.getCodex(e) == sameCodex) {
8585
e++;
8686
}
8787
return e - i;
8888
}
8989

9090
static int getFirstNamelessRecord(final int sameCodex, final int index, final int firstCode) {
9191
int i = index;
92-
while ((i >= firstCode) && Data.isNameless(i) && (Data.calcCodex(i) == sameCodex)) {
92+
while ((i >= firstCode) && Data.isNameless(i) && (Data.getCodex(i) == sameCodex)) {
9393
i--;
9494
}
9595
i++;

src/main/java/com/mapcode/Data.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
package com.mapcode;
1818

1919
import javax.annotation.Nonnull;
20-
import javax.annotation.Nullable;
2120

2221
/**
2322
* ----------------------------------------------------------------------------------------------
2423
* Package private implementation class. For internal use within the Mapcode implementation only.
2524
* ----------------------------------------------------------------------------------------------
26-
*
25+
* <p/>
2726
* This class the data class for Mapcode codex items.
2827
*/
2928
class Data {
@@ -34,36 +33,42 @@ class Data {
3433
'A', 'E', 'U'
3534
};
3635

37-
static boolean isNameless(final int i) {
38-
return (DataAccess.dataFlags(i) & 64) != 0;
36+
private Data() {
37+
// Disabled.
38+
}
39+
40+
static boolean isNameless(final int territoryRecord) {
41+
return (DataAccess.getDataFlags(territoryRecord) & 64) != 0;
3942
}
4043

41-
static boolean isSpecialShape(final int i) {
42-
return (DataAccess.dataFlags(i) & 1024) != 0;
44+
static boolean isSpecialShape(final int territoryRecord) {
45+
return (DataAccess.getDataFlags(territoryRecord) & 1024) != 0;
4346
}
4447

45-
static int recType(final int i) {
46-
return (DataAccess.dataFlags(i) >> 7) & 3; // 1=pipe 2=plus 3=star
48+
static int getTerritoryRecordType(final int territoryRecord) {
49+
return (DataAccess.getDataFlags(territoryRecord) >> 7) & 3; // 1=pipe 2=plus 3=star
4750
}
4851

49-
static boolean isRestricted(final int i) {
50-
return (DataAccess.dataFlags(i) & 512) != 0;
52+
static boolean isRestricted(final int territoryRecord) {
53+
return (DataAccess.getDataFlags(territoryRecord) & 512) != 0;
5154
}
5255

53-
static int calcCodex(final int i) {
54-
final int codexflags = DataAccess.dataFlags(i) & 31;
56+
static int getCodex(final int territoryRecord) {
57+
final int codexflags = DataAccess.getDataFlags(territoryRecord) & 31;
5558
return (10 * (codexflags / 5)) + (codexflags % 5) + 1;
5659
}
5760

61+
@Nonnull
5862
static String headerLetter(final int i) {
59-
final int flags = DataAccess.dataFlags(i);
63+
final int flags = DataAccess.getDataFlags(i);
6064
if (((flags >> 7) & 3) == 1) {
6165
return Character.toString(ENCODE_CHARS[(flags >> 11) & 31]);
6266
}
6367
return "";
6468
}
6569

66-
static Boundaries getBoundaries(final int i) {
67-
return Boundaries.getBoundaries(i);
70+
@Nonnull
71+
static Boundary getBoundary(final int territoryRecord) {
72+
return Boundary.createFromTerritoryRecord(territoryRecord);
6873
}
6974
}

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

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@
2727
* ----------------------------------------------------------------------------------------------
2828
* Package private implementation class. For internal use within the Mapcode implementation only.
2929
* ----------------------------------------------------------------------------------------------
30-
*
30+
* <p/>
3131
* This class contains the module that reads the Mapcode areas into memory and processes them.
3232
*/
3333
class DataAccess {
3434
private static final Logger LOG = LoggerFactory.getLogger(DataAccess.class);
3535

36-
private static final int nrTerritories;
37-
private static final int nrTerritoryRecords;
36+
private static final int NR_TERRITORIES;
37+
private static final int NR_TERRITORY_RECORDS;
3838
private static final int[] DATA_START;
3939
private static final int[] FILE_DATA;
4040

4141
private static final String FILE_NAME = "/com/mapcode/mminfo.dat";
42+
private static final int HEADER_SIZE = 8;
4243

4344
// Read data only once in static initializer.
4445
static {
@@ -58,40 +59,39 @@ class DataAccess {
5859
nrBytes = inputStream.read(readBuffer);
5960
}
6061

61-
// Copy stream into data
62+
// Copy stream into data.
6263
final byte[] bytes = outputStream.toByteArray();
6364
assert total == bytes.length;
64-
65-
// read SIGNATURE "MC", VERSION
65+
66+
// Read SIGNATURE "MC", VERSION.
6667
assert total > 12;
6768
assert (char) bytes[0] == 'M';
6869
assert (char) bytes[1] == 'C';
6970
final int dataVersion = (bytes[2] & 255) + ((bytes[3] & 255) << 8);
7071
assert (dataVersion >= 220);
71-
final int HEADER_SIZE = 8;
7272

73-
// read header: NR TERRITORIES, NR RECTRANGLE RECORD
74-
nrTerritoryRecords = (bytes[4] & 255) + ((bytes[5] & 255) << 8);
75-
nrTerritories = (bytes[6] & 255) + ((bytes[7] & 255) << 8);
76-
LOG.info("version={} nrTerritories={} nrTerritoryRecords={}",dataVersion,nrTerritories,nrTerritoryRecords);
77-
final int expectedsize = HEADER_SIZE + ((nrTerritories + 1) * 2) + (nrTerritoryRecords * 20);
73+
// Read header: NR TERRITORIES, NR RECTANGLE RECORD.
74+
NR_TERRITORY_RECORDS = (bytes[4] & 255) + ((bytes[5] & 255) << 8);
75+
NR_TERRITORIES = (bytes[6] & 255) + ((bytes[7] & 255) << 8);
76+
LOG.info("version={} NR_TERRITORIES={} NR_TERRITORY_RECORDS={}", dataVersion, NR_TERRITORIES, NR_TERRITORY_RECORDS);
77+
final int expectedsize = HEADER_SIZE + ((NR_TERRITORIES + 1) * 2) + (NR_TERRITORY_RECORDS * 20);
7878
assert (expectedsize == total);
7979

80-
// read DATA+START array (2 bytes per territory, plus closing record)
81-
DATA_START = new int[nrTerritories + 1];
80+
// Read DATA+START array (2 bytes per territory, plus closing record).
81+
DATA_START = new int[NR_TERRITORIES + 1];
8282
int i = HEADER_SIZE;
83-
for (int k=0; k <= nrTerritories; k++) {
83+
for (int k = 0; k <= NR_TERRITORIES; k++) {
8484
DATA_START[k] = (bytes[i] & 255) + ((bytes[i + 1] & 255) << 8);
8585
i += 2;
8686
}
87-
88-
// read territory rectangle data (5 longs per record)
89-
FILE_DATA = new int[nrTerritoryRecords * 5];
90-
for (int k=0; k < nrTerritoryRecords * 5; k++) {
91-
FILE_DATA[k] = ((bytes[i] & 255)) +
92-
((bytes[i + 1] & 255) << 8) +
93-
((bytes[i + 2] & 255) << 16) +
94-
((bytes[i + 3] & 255) << 24);
87+
88+
// Read territory rectangle data (5 longs per record).
89+
FILE_DATA = new int[NR_TERRITORY_RECORDS * 5];
90+
for (int k = 0; k < (NR_TERRITORY_RECORDS * 5); k++) {
91+
FILE_DATA[k] = ((bytes[i] & 255)) +
92+
((bytes[i + 1] & 255) << 8) +
93+
((bytes[i + 2] & 255) << 16) +
94+
((bytes[i + 3] & 255) << 24);
9595
i += 4;
9696
}
9797
} finally {
@@ -111,36 +111,36 @@ private DataAccess() {
111111
// Empty.
112112
}
113113

114-
static int minx(final int i) {
115-
return FILE_DATA[i * 5];
114+
static int getMinX(final int territoryRecord) {
115+
return FILE_DATA[territoryRecord * 5];
116116
}
117-
118-
static int miny(final int i) {
119-
return FILE_DATA[(i * 5) + 1];
117+
118+
static int getMinY(final int territoryRecord) {
119+
return FILE_DATA[(territoryRecord * 5) + 1];
120120
}
121-
122-
static int maxx(final int i) {
123-
return FILE_DATA[(i * 5) + 2];
121+
122+
static int getMaxX(final int territoryRecord) {
123+
return FILE_DATA[(territoryRecord * 5) + 2];
124124
}
125125

126-
static int maxy(final int i) {
127-
return FILE_DATA[(i * 5) + 3];
126+
static int getMaxY(final int territoryRecord) {
127+
return FILE_DATA[(territoryRecord * 5) + 3];
128128
}
129129

130-
static int dataFlags(final int i) {
131-
return FILE_DATA[(i * 5) + 4] & 65535;
130+
static int getDataFlags(final int territoryRecord) {
131+
return FILE_DATA[(territoryRecord * 5) + 4] & 65535;
132132
}
133133

134-
static int smartDiv(final int i) {
135-
return FILE_DATA[(i * 5) + 4] >> 16;
134+
static int getSmartDiv(final int territoryRecord) {
135+
return FILE_DATA[(territoryRecord * 5) + 4] >> 16;
136136
}
137137

138138
// / low-level routines for data access
139-
static int dataFirstRecord(final int ccode) {
140-
return DATA_START[ccode];
139+
static int getDataFirstRecord(final int territoryNumber) {
140+
return DATA_START[territoryNumber];
141141
}
142142

143-
static int dataLastRecord(final int ccode) {
144-
return DATA_START[ccode + 1] - 1;
143+
static int getDataLastRecord(final int territoryNumber) {
144+
return DATA_START[territoryNumber + 1] - 1;
145145
}
146146
}

0 commit comments

Comments
 (0)