Skip to content

Commit ee897d9

Browse files
committed
Hygiene improvements
1 parent 989186d commit ee897d9

File tree

13 files changed

+657
-609
lines changed

13 files changed

+657
-609
lines changed

README.md

Lines changed: 589 additions & 12 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<artifactId>mapcode</artifactId>
99

1010
<packaging>jar</packaging>
11-
<version>2.4.4-SNAPSHOT</version>
11+
<version>2.4.4</version>
1212

1313
<name>Mapcode Java Library</name>
1414
<description>
@@ -36,7 +36,7 @@
3636
<name>Rijn Buve</name>
3737
<organization>Mapcode Foundation</organization>
3838
<roles>
39-
<role>Managing Director, development</role>
39+
<role>Managing Director, main contributor</role>
4040
</roles>
4141
</developer>
4242

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,28 @@
2626
* This class handles territory rectangles for mapcodes.
2727
*/
2828
class Boundary {
29-
private int lonMicroDegMin; // Minimum longitude (in microdegrees). Inclusive.
30-
private int lonMicroDegMax; // Maximum longitude (in microdegrees). Exclusive.
3129
private int latMicroDegMin; // Minimum latitude (in microdegrees). Inclusive.
30+
private int lonMicroDegMin; // Minimum longitude (in microdegrees). Inclusive.
3231
private int latMicroDegMax; // Minimum latitude (in microdegrees). Exclusive.
32+
private int lonMicroDegMax; // Maximum longitude (in microdegrees). Exclusive.
3333

34+
// Get the singleton for the data model.
3435
private static final DataModel DATA_MODEL = DataModel.getInstance();
3536

36-
private Boundary(final int lonMicroDegMin, final int lonMicroDegMax,
37-
final int latMicroDegMin, final int latMicroDegMax) {
37+
private Boundary(final int latMicroDegMin, final int lonMicroDegMin, final int latMicroDegMax, final int lonMicroDegMax) {
3838
this.lonMicroDegMin = lonMicroDegMin;
3939
this.latMicroDegMin = latMicroDegMin;
40-
this.lonMicroDegMax = lonMicroDegMax;
4140
this.latMicroDegMax = latMicroDegMax;
41+
this.lonMicroDegMax = lonMicroDegMax;
4242
}
4343

4444
// You have to use this factory method instead of a ctor.
4545
@Nonnull
4646
static Boundary createBoundaryForTerritoryRecord(final int territoryRecord) {
4747
return new Boundary(
48-
DATA_MODEL.getLonMicroDegMin(territoryRecord),
49-
DATA_MODEL.getLonMicroDegMax(territoryRecord),
50-
DATA_MODEL.getLatMicroDegMin(territoryRecord),
51-
DATA_MODEL.getLatMicroDegMax(territoryRecord));
48+
DATA_MODEL.getLatMicroDegMin(territoryRecord), DATA_MODEL.getLonMicroDegMin(territoryRecord),
49+
DATA_MODEL.getLatMicroDegMax(territoryRecord), DATA_MODEL.getLonMicroDegMax(territoryRecord)
50+
);
5251
}
5352

5453
int getLonMicroDegMin() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
/**
2525
* ----------------------------------------------------------------------------------------------
26-
* Package private implementation class. For internal use within the Mapcode implementation only.
26+
* Package private implementation class. For internal use within the mapcode implementation only.
27+
* ----------------------------------------------------------------------------------------------
2728
*
2829
* This class provides a number of helper methods to check (runtime) arguments.
29-
* ----------------------------------------------------------------------------------------------
3030
*/
3131
@SuppressWarnings("OverlyBroadThrowsClause")
3232
class CheckArgs {

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

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,41 +73,34 @@ private Common() {
7373
static {
7474

7575
// This code shows a message when assertions are active or disabled. It (ab)uses assert for that...
76+
// Some of the methods (and tests) take considerably longer with assertions checking, so it's useful
77+
// to have this information in the log file.
78+
7679
//noinspection UnusedAssignment
7780
boolean debug = false;
7881
//noinspection AssertWithSideEffects
7982
assert debug = true;
8083
//noinspection ConstantConditions
8184
if (debug) {
8285
LOG.info("Common: assertions are active (JVM runtime option '-ea')");
83-
}
84-
else {
86+
} else {
8587
LOG.debug("Common: assertions are not active, they are bypassed");
8688
}
8789
}
8890

89-
/**
90-
* This method returns a divider for longitude (multiplied by 4), for a given latitude.
91-
*
92-
* @param minY Latitude.
93-
* @param maxY Longitude.
94-
* @return Divider.
95-
*/
91+
// This method returns a divider for longitude (multiplied by 4), for a given latitude.
9692
// TODO: Need better names for minY and maxY.
97-
static int xDivider(final int minY, final int maxY) {
98-
assert minY < maxY;
99-
if (minY >= 0) {
100-
// maxY > minY > 0
101-
assert (maxY > minY) && (minY > 0);
102-
return X_DIVIDER_19[minY >> 19];
103-
} else if (maxY >= 0) {
104-
// maxY > 0 > minY
105-
assert (maxY > 0) && (0 > minY);
93+
static int xDivider(final int latMin, final int latMax) {
94+
assert latMin < latMax;
95+
if (latMin >= 0) {
96+
assert (latMax > latMin) && (latMin > 0);
97+
return X_DIVIDER_19[latMin >> 19];
98+
} else if (latMax >= 0) {
99+
assert (latMax > 0) && (0 > latMin);
106100
return X_DIVIDER_19[0];
107101
} else {
108-
// 0 > maxY > minY
109-
assert (0 > maxY) && (maxY > minY);
110-
return X_DIVIDER_19[(-maxY) >> 19];
102+
assert (0 > latMax) && (latMax > latMin);
103+
return X_DIVIDER_19[(-latMax) >> 19];
111104
}
112105
}
113106

src/main/java/com/mapcode/Decoder.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@
2323

2424
import static com.mapcode.Boundary.createBoundaryForTerritoryRecord;
2525

26+
/**
27+
* ----------------------------------------------------------------------------------------------
28+
* Package private implementation class. For internal use within the Mapcode implementation only.
29+
* ----------------------------------------------------------------------------------------------
30+
*
31+
* This class contains decoder for mapcodes.
32+
*/
2633
class Decoder {
2734
private static final Logger LOG = LoggerFactory.getLogger(Decoder.class);
2835

29-
private static final char GREEK_CAPITAL_ALPHA = '\u0391';
30-
31-
private static final DataModel dataModel = DataModel.getInstance();
36+
// Get direct access to the data model singleton.
37+
private static final DataModel DATA_MODEL = DataModel.getInstance();
3238

3339
private Decoder() {
3440
// Prevent instantiation.
@@ -77,20 +83,19 @@ static MapcodeZone decodeToMapcodeZone(@Nonnull final String argMapcode,
7783
territory = Territory.AAA;
7884
} else {
7985
final Territory parentTerritory = territory.getParentTerritory();
80-
if (((codexLen >= 8) && ((parentTerritory == Territory.USA) || (parentTerritory == Territory.CAN)
81-
|| (parentTerritory == Territory.AUS) || (parentTerritory == Territory.BRA)
82-
|| (parentTerritory == Territory.CHN) || (parentTerritory == Territory.RUS)))
83-
|| ((codexLen >= 7) &&
84-
((parentTerritory == Territory.IND) || (parentTerritory == Territory.MEX)))) {
85-
86+
if (((codexLen >= 8) &&
87+
((parentTerritory == Territory.USA) || (parentTerritory == Territory.CAN) ||
88+
(parentTerritory == Territory.AUS) || (parentTerritory == Territory.BRA) ||
89+
(parentTerritory == Territory.CHN) || (parentTerritory == Territory.RUS))) ||
90+
((codexLen >= 7) &&
91+
((parentTerritory == Territory.IND) || (parentTerritory == Territory.MEX)))) {
8692
territory = parentTerritory;
8793
}
8894
}
89-
9095
final int territoryNumber = territory.getNumber();
9196

92-
final int fromTerritoryRecord = dataModel.getDataFirstRecord(territoryNumber);
93-
final int uptoTerritoryRecord = dataModel.getDataLastRecord(territoryNumber);
97+
final int fromTerritoryRecord = DATA_MODEL.getDataFirstRecord(territoryNumber);
98+
final int uptoTerritoryRecord = DATA_MODEL.getDataLastRecord(territoryNumber);
9499

95100
// Determine the codex pattern as 2-digits: length-of-left-part * 10 + length-of-right-part.
96101
final int positionOfDot = mapcode.indexOf('.');
@@ -232,6 +237,9 @@ public Unicode2Ascii(final char min, final char max, @Nonnull final String conve
232237
}
233238
}
234239

240+
// Greek character A.
241+
private static final char GREEK_CAPITAL_ALPHA = '\u0391';
242+
235243
// Special character '?' indicating missing character in alphabet.
236244
private static final char MISSCODE = '?';
237245

@@ -339,7 +347,7 @@ private static MapcodeZone decodeGrid(
339347

340348
final int divx;
341349
int divy;
342-
divy = dataModel.getSmartDiv(m);
350+
divy = DATA_MODEL.getSmartDiv(m);
343351
if (divy == 1) {
344352
divx = Common.X_SIDE[prelen];
345353
divy = Common.Y_SIDE[prelen];
@@ -486,7 +494,7 @@ private static MapcodeZone decodeNameless(
486494

487495
final int territoryRecord = firstrec + nrX;
488496

489-
int side = dataModel.getSmartDiv(territoryRecord);
497+
int side = DATA_MODEL.getSmartDiv(territoryRecord);
490498
int xSIDE = side;
491499

492500
final Boundary boundary = createBoundaryForTerritoryRecord(territoryRecord);

src/main/java/com/mapcode/Encoder.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@
2727
import static com.mapcode.Boundary.createBoundaryForTerritoryRecord;
2828
import static com.mapcode.Common.*;
2929

30+
/**
31+
* ----------------------------------------------------------------------------------------------
32+
* Package private implementation class. For internal use within the Mapcode implementation only.
33+
* ----------------------------------------------------------------------------------------------
34+
*
35+
* This class contains encoder for mapcodes.
36+
*/
3037
class Encoder {
3138
private static final Logger LOG = LoggerFactory.getLogger(Encoder.class);
3239

33-
// Singleton of the data model.
34-
private static final DataModel dataModel = DataModel.getInstance();
40+
// Get direct access to data model singleton.
41+
private static final DataModel DATA_MODEL = DataModel.getInstance();
3542

3643
private Encoder() {
3744
// Prevent instantiation.
@@ -78,10 +85,10 @@ private static List<Mapcode> encode(
7885
for (int territoryRecord = firstTerritoryRecord; territoryRecord <= lastTerritoryRecord; territoryRecord++) {
7986

8087
// Check if the point to encode is covered by the last data record.
81-
final int firstSubTerritoryRecord = dataModel.getDataLastRecord(territoryRecord);
88+
final int firstSubTerritoryRecord = DATA_MODEL.getDataLastRecord(territoryRecord);
8289
if (createBoundaryForTerritoryRecord(firstSubTerritoryRecord).containsPoint(pointToEncode)) {
8390

84-
final int lastSubTerritoryRecord = dataModel.getDataFirstRecord(territoryRecord);
91+
final int lastSubTerritoryRecord = DATA_MODEL.getDataFirstRecord(territoryRecord);
8592
final Territory currentEncodeTerritory = Territory.fromNumber(territoryRecord);
8693

8794
for (int subTerritoryRecord = lastSubTerritoryRecord; subTerritoryRecord <= firstSubTerritoryRecord; subTerritoryRecord++) {
@@ -210,7 +217,7 @@ private static String encodeGrid(
210217
final int prelen = codexm / 10;
211218
final int postlen = codexm % 10;
212219
final int divx;
213-
int divy = dataModel.getSmartDiv(territoryNumber);
220+
int divy = DATA_MODEL.getSmartDiv(territoryNumber);
214221
if (divy == 1) {
215222
divx = X_SIDE[prelen];
216223
divy = Y_SIDE[prelen];
@@ -405,7 +412,7 @@ private static String encodeNameless(
405412
storage_offset = nrX * basePowerA;
406413
}
407414

408-
int side = dataModel.getSmartDiv(territoryRecord);
415+
int side = DATA_MODEL.getSmartDiv(territoryRecord);
409416
final int orgSide = side;
410417
int xSide = side;
411418

src/main/java/com/mapcode/MapcodeCodec.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public static Rectangle decodeToRectangle(@Nonnull final String mapcode, @Nullab
269269
final Point southWest = Point.fromLatLonFractions(mapcodeZone.getLatFractionMin(), mapcodeZone.getLonFractionMin());
270270
final Point northEast = Point.fromLatLonFractions(mapcodeZone.getLatFractionMax(), mapcodeZone.getLonFractionMax());
271271
final Rectangle rectangle = new Rectangle(southWest, northEast);
272+
assert rectangle.isDefined();
272273
return rectangle;
273274
}
274275

src/main/java/com/mapcode/MapcodeZone.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* ----------------------------------------------------------------------------------------------
2323
* Package private implementation class. For internal use within the Mapcode implementation only.
2424
* ----------------------------------------------------------------------------------------------
25+
*
2526
* Simple class to represent all the coordinates that would deliver a particular mapcode.
2627
*/
2728
class MapcodeZone {

src/main/java/com/mapcode/Rectangle.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ public Point getNorthEast() {
4343
return northEast;
4444
}
4545

46-
public boolean isDefined() {
47-
return southWest.isDefined() && northEast.isDefined();
48-
}
49-
50-
@Nonnull
46+
@Nonnull
5147
public Point getCenter() {
5248
if (!isDefined()) {
5349
return Point.undefined();
@@ -57,6 +53,10 @@ public Point getCenter() {
5753
return Point.fromDeg(centerLat, centerLon);
5854
}
5955

56+
boolean isDefined() {
57+
return southWest.isDefined() && northEast.isDefined();
58+
}
59+
6060
@Nonnull
6161
@Override
6262
public String toString() {

0 commit comments

Comments
 (0)