Skip to content

Commit be4ce53

Browse files
committed
Updated copyright, minor readability improvements
1 parent eb9ad88 commit be4ce53

35 files changed

+225
-125
lines changed

NOTICE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ MAPCODE JAVA LIBRARY
55
Original C library created by Pieter Geelen. Work on Java version
66
of the Mapcode library by Rijn Buve (original port by Matthew Lowden).
77

8-
Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
8+
Copyright (C) 2014-2016 Stichting Mapcode Foundation (http://www.mapcode.com)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mapcode Library for Java
22

3-
Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
3+
Copyright (C) 2014-2016 Stichting Mapcode Foundation (http://www.mapcode.com)
44

55
----
66

pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@
7171
<jdk.version>1.6</jdk.version>
7272

7373
<!-- libraries. -->
74-
<gson.version>2.3.1</gson.version>
75-
<jsr305.version>3.0.0</jsr305.version>
74+
<gson.version>2.6.2</gson.version>
75+
<jsr305.version>3.0.1</jsr305.version>
7676
<junit.version>4.12</junit.version>
7777
<log4j.version>1.2.17</log4j.version>
78-
<slf4j.version>1.7.13</slf4j.version>
78+
<slf4j.version>1.7.21</slf4j.version>
7979

8080
<!-- Modules. -->
81-
<nexus-staging-maven-plugin.version>1.6.6</nexus-staging-maven-plugin.version>
82-
<maven-source-plugin.version>2.4</maven-source-plugin.version>
83-
<maven-javadoc-plugin.version>2.10.1</maven-javadoc-plugin.version>
81+
<nexus-staging-maven-plugin.version>1.6.7</nexus-staging-maven-plugin.version>
82+
<maven-source-plugin.version>3.0.0</maven-source-plugin.version>
83+
<maven-javadoc-plugin.version>2.10.3</maven-javadoc-plugin.version>
8484
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
85-
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
85+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
8686
</properties>
8787

8888
<distributionManagement>

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
2+
* Copyright (C) 2014-2016 Stichting Mapcode Foundation (http://www.mapcode.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,11 +21,15 @@
2121
import static com.mapcode.CheckArgs.checkNonnull;
2222

2323
/**
24-
* This enum defines all alphabets supported for mapcodes. Note that an alphabet is different from a
25-
* language or locale. An alternative name for an alphabet is "script".
24+
* This enum defines all alphabets supported for mapcodes. Note that an alphabet is different
25+
* from a language or locale. Note that the term alphabet was historically chosen. Script is a
26+
* more precise term.
2627
*
27-
* Mapcodes can be safely converted between alphabets and fed to the mapcode decoder in the regular
28-
* ASCII Roman alphabet or any other.
28+
* Mapcodes can be safely converted between alphabets/scripts and fed to the mapcode decoder
29+
* in the regular ASCII Roman alphabet or any other.
30+
*
31+
* The characters within an alphabet/script have been carefully chosen to resemble each other
32+
* as much as possible.
2933
*/
3034
public enum Alphabet {
3135
ROMAN(0), // The numeric codes for alphabets are used by the implementation

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
2+
* Copyright (C) 2014-2016 Stichting Mapcode Foundation (http://www.mapcode.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,9 +31,6 @@ class Boundary {
3131
private int latMicroDegMin; // Minimum latitude (in microdegrees). Inclusive.
3232
private int latMicroDegMax; // Minimum latitude (in microdegrees). Exclusive.
3333

34-
static final int MICRO_DEG_360 = 360000000;
35-
static final double DEG_TO_MICRO_DEG = 1000000.0;
36-
3734
private Boundary(
3835
final int lonMicroDegMin,
3936
final int lonMicroDegMax,
@@ -101,17 +98,17 @@ boolean containsPoint(@Nonnull final Point p) {
10198

10299
// Longitude boundaries can extend (slightly) outside the [-180,180) range
103100
if (lonMicroDeg < lonMicroDegMin) {
104-
return (lonMicroDegMin <= (lonMicroDeg + MICRO_DEG_360)) && ((lonMicroDeg + MICRO_DEG_360) < lonMicroDegMax);
101+
return (lonMicroDegMin <= (lonMicroDeg + Point.MICRO_DEG_360)) && ((lonMicroDeg + Point.MICRO_DEG_360) < lonMicroDegMax);
105102
} else if (lonMicroDeg >= lonMicroDegMax) {
106-
return (lonMicroDegMin <= (lonMicroDeg - MICRO_DEG_360)) && ((lonMicroDeg - MICRO_DEG_360) < lonMicroDegMax);
103+
return (lonMicroDegMin <= (lonMicroDeg - Point.MICRO_DEG_360)) && ((lonMicroDeg - Point.MICRO_DEG_360) < lonMicroDegMax);
107104
} else {
108105
return true;
109106
}
110107
}
111108

112109
@Nonnull
113110
public String toString() {
114-
return "[" + (latMicroDegMin / DEG_TO_MICRO_DEG) + ", " + (latMicroDegMax / DEG_TO_MICRO_DEG) +
115-
"), [" + (lonMicroDegMin / DEG_TO_MICRO_DEG) + ", " + (lonMicroDegMax / DEG_TO_MICRO_DEG) + ')';
111+
return "[" + (latMicroDegMin / Point.DEG_TO_MICRO_DEG) + ", " + (latMicroDegMax / Point.DEG_TO_MICRO_DEG) +
112+
"), [" + (lonMicroDegMin / Point.DEG_TO_MICRO_DEG) + ", " + (lonMicroDegMax / Point.DEG_TO_MICRO_DEG) + ')';
116113
}
117114
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
2+
* Copyright (C) 2014-2016 Stichting Mapcode Foundation (http://www.mapcode.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@
2424
/**
2525
* ----------------------------------------------------------------------------------------------
2626
* Package private implementation class. For internal use within the Mapcode implementation only.
27+
*
28+
* This class provides a number of helper methods to check (runtime) arguments.
2729
* ----------------------------------------------------------------------------------------------
2830
*/
2931
@SuppressWarnings("OverlyBroadThrowsClause")

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

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
2+
* Copyright (C) 2014-2016 Stichting Mapcode Foundation (http://www.mapcode.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
1616

1717
package com.mapcode;
1818

19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
1922
/**
2023
* ----------------------------------------------------------------------------------------------
2124
* Package private implementation class. For internal use within the Mapcode implementation only.
@@ -24,14 +27,16 @@
2427
* This class contains common data structures and methods used by the Mapcode implementation.
2528
*/
2629
class Common {
30+
private static final Logger LOG = LoggerFactory.getLogger(Common.class);
31+
2732
static final int[] nc = {
2833
1, 31, 961, 29791, 923521, 28629151, 887503681
2934
};
3035
static final int[] xSide = {
31-
0, 5, 31, 168, 961, 168 * 31, 29791, 165869, 923521, 5141947
36+
0, 5, 31, 168, 961, 5208, 29791, 165869, 923521, 5141947
3237
};
3338
static final int[] ySide = {
34-
0, 6, 31, 176, 961, 176 * 31, 29791, 165869, 923521, 5141947
39+
0, 6, 31, 176, 961, 5456, 29791, 165869, 923521, 5141947
3540
};
3641
private static final int[] xDivider19 = {
3742
360, 360, 360, 360, 360, 360, 361, 361, 361, 361,
@@ -58,6 +63,22 @@ private Common() {
5863
// Prevent instantiation.
5964
}
6065

66+
static {
67+
68+
// This code shows a message when assertions are active or disabled. It (ab)uses assert for that...
69+
//noinspection UnusedAssignment
70+
boolean debug = false;
71+
//noinspection AssertWithSideEffects
72+
assert debug = true;
73+
//noinspection ConstantConditions
74+
if (debug) {
75+
LOG.info("Common: assertions are active (JVM runtime option '-ea')");
76+
}
77+
else {
78+
LOG.debug("Common: assertions are not active, they are bypassed");
79+
}
80+
}
81+
6182
/**
6283
* This method returns a divider for longitude (multiplied by 4), for a given latitude.
6384
*
@@ -66,33 +87,45 @@ private Common() {
6687
* @return Divider.
6788
*/
6889
static int xDivider(final int minY, final int maxY) {
90+
assert minY < maxY;
6991
if (minY >= 0) {
7092
// maxY > minY > 0
93+
assert (maxY > minY) && (minY > 0);
7194
return xDivider19[minY >> 19];
72-
}
73-
if (maxY >= 0) {
95+
} else if (maxY >= 0) {
7496
// maxY > 0 > minY
97+
assert (maxY > 0) && (0 > minY);
7598
return xDivider19[0];
99+
} else {
100+
// 0 > maxY > minY
101+
assert (0 > maxY) && (maxY > minY);
102+
return xDivider19[(-maxY) >> 19];
76103
}
77-
// 0 > maxY > minY
78-
return xDivider19[(-maxY) >> 19];
79104
}
80105

81-
static int countCityCoordinatesForCountry(final int sameCodex, final int index, final int firstCode) {
82-
final int i = getFirstNamelessRecord(sameCodex, index, firstCode);
83-
int e = index;
84-
while (Data.getCodex(e) == sameCodex) {
106+
static int countCityCoordinatesForCountry(final int codex, final int territoryRecord, final int firstTerritoryRecord) {
107+
assert codex >= 0;
108+
assert territoryRecord >= 0;
109+
assert firstTerritoryRecord >= 0;
110+
final int i = getFirstNamelessRecord(codex, territoryRecord, firstTerritoryRecord);
111+
int e = territoryRecord;
112+
while (Data.getCodex(e) == codex) {
85113
e++;
86114
}
115+
assert i <= e;
87116
return e - i;
88117
}
89118

90-
static int getFirstNamelessRecord(final int sameCodex, final int index, final int firstCode) {
91-
int i = index;
92-
while ((i >= firstCode) && Data.isNameless(i) && (Data.getCodex(i) == sameCodex)) {
119+
static int getFirstNamelessRecord(final int codex, final int territoryRecord, final int firstTerritoryRecord) {
120+
assert codex >= 0;
121+
assert territoryRecord >= 0;
122+
assert firstTerritoryRecord >= 0;
123+
int i = territoryRecord;
124+
while ((i >= firstTerritoryRecord) && Data.isNameless(i) && (Data.getCodex(i) == codex)) {
93125
i--;
94126
}
95127
i++;
128+
assert i <= territoryRecord;
96129
return i;
97130
}
98131
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
2+
* Copyright (C) 2014-2016 Stichting Mapcode Foundation (http://www.mapcode.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,33 +27,43 @@
2727
*/
2828
class Data {
2929
static final char[] ENCODE_CHARS = {
30-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
31-
'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
30+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', // Numerals.
31+
'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', // Consonants.
3232
'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z',
33-
'A', 'E', 'U'
33+
'A', 'E', 'U' // Vowels.
3434
};
3535

3636
private Data() {
3737
// Disabled.
3838
}
3939

4040
static boolean isNameless(final int territoryRecord) {
41+
assert (0 <= territoryRecord) && (territoryRecord < DataAccess.getNrTerritoryRecords());
4142
return (DataAccess.getDataFlags(territoryRecord) & 64) != 0;
4243
}
4344

4445
static boolean isSpecialShape(final int territoryRecord) {
46+
assert (0 <= territoryRecord) && (territoryRecord < DataAccess.getNrTerritoryRecords());
4547
return (DataAccess.getDataFlags(territoryRecord) & 1024) != 0;
4648
}
4749

50+
static final int TERRITORY_RECORD_TYPE_NONE = 0;
51+
static final int TERRITORY_RECORD_TYPE_PIPE = 1;
52+
static final int TERRITORY_RECORD_TYPE_PLUS = 2;
53+
static final int TERRITORY_RECORD_TYPE_STAR = 3;
54+
4855
static int getTerritoryRecordType(final int territoryRecord) {
56+
assert (0 <= territoryRecord) && (territoryRecord < DataAccess.getNrTerritoryRecords());
4957
return (DataAccess.getDataFlags(territoryRecord) >> 7) & 3; // 1=pipe 2=plus 3=star
5058
}
5159

5260
static boolean isRestricted(final int territoryRecord) {
61+
assert (0 <= territoryRecord) && (territoryRecord < DataAccess.getNrTerritoryRecords());
5362
return (DataAccess.getDataFlags(territoryRecord) & 512) != 0;
5463
}
5564

5665
static int getCodex(final int territoryRecord) {
66+
assert (0 <= territoryRecord) && (territoryRecord < DataAccess.getNrTerritoryRecords());
5767
final int codexflags = DataAccess.getDataFlags(territoryRecord) & 31;
5868
return (10 * (codexflags / 5)) + (codexflags % 5) + 1;
5969
}

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
2+
* Copyright (C) 2014-2016 Stichting Mapcode Foundation (http://www.mapcode.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,6 +68,8 @@ class DataAccess {
6868
private static final String FILE_NAME = "/com/mapcode/mminfo.dat";
6969
private static final int FILE_BUFFER_SIZE = 50000;
7070

71+
private static final int DATA_VERSION_MIN = 220;
72+
7173
// Read data only once in static initializer.
7274
static {
7375
LOG.info("DataAccess: reading regions from file: {}", FILE_NAME);
@@ -78,6 +80,8 @@ class DataAccess {
7880
try {
7981
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
8082
try {
83+
84+
// Read the input stream, copy to memory buffer.
8185
int nrBytes = inputStream.read(readBuffer);
8286
while (nrBytes > 0) {
8387
total += nrBytes;
@@ -88,23 +92,35 @@ class DataAccess {
8892
// Copy stream into data.
8993
final byte[] bytes = outputStream.toByteArray();
9094
assert total == bytes.length;
95+
if (total < 12) {
96+
LOG.error("DataAccess: expected more than {} bytes", total);
97+
throw new IllegalStateException("Data file corrupt: " + FILE_NAME);
98+
}
9199

92-
// Read SIGNATURE "MC", VERSION.
93-
assert total > 12;
100+
// Read "MC", VERSION.
101+
assert total > 8; // "MC" (2) + VERSION (2) + NR TERRITORIES (2) + NR TERRITORY RECORDS (2).
94102
assert (char) bytes[HEADER_ID_1] == 'M';
95103
assert (char) bytes[HEADER_ID_2] == 'C';
96104
final int dataVersion = readIntLoHi(bytes[HEADER_VERSION_LO], bytes[HEADER_VERSION_HI]);
97-
assert (dataVersion >= 220);
105+
assert dataVersion >= DATA_VERSION_MIN;
98106

99-
// Read header: NR TERRITORIES, NR RECTANGLE RECORD.
107+
// Read header: NR TERRITORIES, NR RECTANGLE RECORDS.
100108
NR_TERRITORY_RECORDS = readIntLoHi(bytes[HEADER_NR_TERRITORIES_RECS_LO], bytes[HEADER_NR_TERRITORIES_RECS_HI]);
101109
NR_TERRITORIES = readIntLoHi(bytes[HEADER_NR_TERRITORIES_LO], bytes[HEADER_NR_TERRITORIES_HI]);
110+
111+
// Check if the number of territories matches the enumeration in Territory.
112+
if (NR_TERRITORIES != Territory.values().length) {
113+
LOG.error("DataAccess: expected {} territories, got {}", Territory.values().length, NR_TERRITORIES);
114+
throw new IllegalStateException("Data file corrupt: " + FILE_NAME);
115+
}
116+
117+
// Check if the expected file size matched what we found.
102118
final int expectedSize = HEADER_SIZE +
103119
((NR_TERRITORIES + 1) * BYTES_PER_INT) +
104120
(NR_TERRITORY_RECORDS * (DATA_FIELDS_PER_REC * BYTES_PER_LONG));
105121

106122
if (expectedSize != total) {
107-
LOG.error("DataAccess: expected {} territories, got {}", expectedSize, total);
123+
LOG.error("DataAccess: expected {} bytes, got {}", expectedSize, total);
108124
throw new IllegalStateException("Data file corrupt: " + FILE_NAME);
109125
}
110126
LOG.debug("DataAccess: version={} territories={} territory records={}", dataVersion, NR_TERRITORIES, NR_TERRITORY_RECORDS);
@@ -124,9 +140,11 @@ class DataAccess {
124140
i += 4;
125141
}
126142
} finally {
143+
//noinspection ThrowFromFinallyBlock
127144
outputStream.close();
128145
}
129146
} finally {
147+
//noinspection ThrowFromFinallyBlock
130148
inputStream.close();
131149
}
132150
} catch (final IOException e) {
@@ -148,6 +166,24 @@ private DataAccess() {
148166
// Empty.
149167
}
150168

169+
/**
170+
* Get number of territories.
171+
*
172+
* @return Number of territories.
173+
*/
174+
static int getNrTerritories() {
175+
return NR_TERRITORIES;
176+
}
177+
178+
/**
179+
* Get number of territory records (rectangles per territory).
180+
*
181+
* @return Number of rectangles per territory.
182+
*/
183+
static int getNrTerritoryRecords() {
184+
return NR_TERRITORY_RECORDS;
185+
}
186+
151187
@SuppressWarnings("PointlessArithmeticExpression")
152188
static int getLonMicroDegMin(final int territoryRecord) {
153189
return DATA[((territoryRecord * DATA_FIELDS_PER_REC) + POS_DATA_LON_MICRO_DEG_MIN)];
@@ -183,4 +219,3 @@ static int getDataLastRecord(final int territoryNumber) {
183219
return INDEX[territoryNumber + POS_INDEX_LAST_RECORD] - 1;
184220
}
185221
}
186-

0 commit comments

Comments
 (0)