Skip to content

Commit 8af1a87

Browse files
committed
Fixed port and regenerated test case for 1.50
1 parent d774934 commit 8af1a87

38 files changed

+43276
-76344
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<artifactId>mapcode</artifactId>
99

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

1313
<name>Mapcode Java Library</name>
1414
<description>

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

100644100755
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class DataAccess {
3434

3535
// Read data only once in static initializer.
3636
static {
37-
boolean initialized = false;
3837
final InputStream inputStream = DataAccess.class.getResourceAsStream(FILE_NAME);
3938
try {
4039
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@@ -45,9 +44,9 @@ class DataAccess {
4544
FILE_DATA = outputStream.toByteArray();
4645
inputStream.close();
4746
outputStream.close();
48-
initialized = true;
4947
} catch (final IOException e) {
50-
throw new ExceptionInInitializerError(e);
48+
throw new ExceptionInInitializerError("Cannot initialize static data structure from: " + FILE_NAME +
49+
", exception=" + e);
5150
} finally {
5251
try {
5352
if (inputStream != null) {
@@ -57,9 +56,6 @@ class DataAccess {
5756
// Ignore.
5857
}
5958
}
60-
if (!initialized) {
61-
throw new IllegalArgumentException("Cannot initialize static data structure from: " + FILE_NAME);
62-
}
6359
}
6460

6561
private DataAccess() {
@@ -143,8 +139,7 @@ static int smartDiv(final int i) {
143139
15477, 15493, 15530, 15547, 15571, 15594, 15611, 15631, 15657, 15683,
144140
15702, 15719, 15772, 15813, 15835, 15862, 15882, 15903, 15921, 15951,
145141
15977, 15997, 16018, 16036, 16054, 16073, 16089, 16105, 16133, 16159,
146-
16166, 16168, 16169, 16171, 16172, 16174, 16176, 16178, 16180, 16182,
147-
16183, 16184, 16216};
142+
16166, 16168, 16169, 16201};
148143

149144
// / low-level routines for data access
150145
static int dataFirstRecord(final int ccode) {

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

100644100755
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.slf4j.LoggerFactory;
2121

2222
import javax.annotation.Nonnull;
23+
import javax.annotation.Nullable;
2324

2425
class Decoder {
2526
private static final Logger LOG = LoggerFactory.getLogger(Decoder.class);
@@ -189,7 +190,7 @@ private static class Unicode2Ascii {
189190
public final int max;
190191
public final String convert;
191192

192-
public Unicode2Ascii(final int min, final int max, final String convert) {
193+
public Unicode2Ascii(final int min, final int max, @Nullable final String convert) {
193194
this.min = min;
194195
this.max = max;
195196
this.convert = convert;
@@ -511,7 +512,19 @@ private static String aeuUnpack(final String argStr) {
511512
}
512513
// less than 2 letters after dot
513514

514-
if (str.charAt(0) == 'A') {
515+
if (str.charAt(0) == 'A') { // v1.50
516+
int v1 = decode_chars[(int) str.charAt(lastpos)];
517+
if (v1 < 0) {
518+
v1 = 31;
519+
}
520+
int v2 = decode_chars[(int) str.charAt(lastpos - 1)];
521+
if (v2 < 0) {
522+
v2 = 31;
523+
}
524+
String s = String.valueOf(1000 + v1 + (32 * v2));
525+
str = s.charAt(1) + str.substring(1, lastpos - 1) + s.charAt(2) + s.charAt(3);
526+
voweled = true;
527+
} else if (str.charAt(0) == 'U') { // v.1.50 debug decoding of U+alldigitmapcode
515528
voweled = true;
516529
str = str.substring(1);
517530
dotpos--;

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

100644100755
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static List<Mapcode> encode(
5454
// ----------------------------------------------------------------------
5555

5656
private final static char[] encode_chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'B', 'C', 'D', 'F',
57-
'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'};
57+
'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z', 'A', 'E', 'U'};
5858

5959
@Nonnull
6060
private static List<Mapcode> encode(final double argLatDeg, final double argLonDeg,
@@ -134,7 +134,7 @@ private static List<Mapcode> encode(final double argLatDeg, final double argLonD
134134
}
135135

136136
if (mapcode.length() > 4) {
137-
mapcode = aeuPack(mapcode);
137+
mapcode = aeuPack(mapcode, false);
138138

139139
Territory encodeTerritory = currentEncodeTerritory;
140140
if (stateOverride != null) {
@@ -446,7 +446,7 @@ private static String encodeNameless(final Point pointToEncode, final Data mapco
446446
return "";
447447
}
448448

449-
private static String aeuPack(final String argStr) {
449+
private static String aeuPack(final String argStr, final boolean argShort) {
450450
String str = argStr;
451451
int dotpos = -9;
452452
int rlen = str.length();
@@ -471,11 +471,13 @@ private static String aeuPack(final String argStr) {
471471
if ((rlen - 2) > dotpos) {
472472
// does r have a dot, AND at least 2 chars
473473
// after the dot?
474-
final int v = (((((int) str.charAt(rlen - 2)) - 48) * 10) + ((int) str.charAt(rlen - 1))) - 48;
475-
final int last = v % 34;
476-
final char[] vowels = {'A', 'E', 'U'};
477-
str =
478-
str.substring(0, rlen - 2) + vowels[v / 34] + (last < 31 ? encode_chars[last] : vowels[last - 31]);
474+
if (argShort) {
475+
final int v = ((((int) str.charAt(0)) - 48) * 100) + ((((int) str.charAt(rlen - 2)) - 48) * 10) + (((int) str.charAt(rlen - 1)) - 48);
476+
return 'A' + str.substring(1, rlen - 2) + encode_chars[v / 32] + encode_chars[v % 32] + rest;
477+
} else {
478+
final int v = (((((int) str.charAt(rlen - 2)) - 48) * 10) + ((int) str.charAt(rlen - 1))) - 48;
479+
str = str.substring(0, rlen - 2) + encode_chars[31 + (v / 34)] + encode_chars[v % 34];
480+
}
479481
}
480482
return str + rest;
481483
}

src/main/java/com/mapcode/ParentTerritory.java

100644100755
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public enum ParentTerritory {
3333
MEX(Territory.MEX),
3434
CAN(Territory.CAN),
3535
RUS(Territory.RUS),
36-
CHN(Territory.CHN),
37-
ATA(Territory.ATA);
36+
CHN(Territory.CHN);
3837

3938
@Nonnull
4039
public Territory getTerritory() {

src/main/java/com/mapcode/Territory.java

100644100755
Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818

1919
import javax.annotation.Nonnull;
2020
import javax.annotation.Nullable;
21-
import java.util.ArrayList;
22-
import java.util.HashMap;
23-
import java.util.List;
24-
import java.util.Map;
21+
import java.util.*;
2522

2623
import static com.mapcode.CheckArgs.checkNonnull;
2724
import static com.mapcode.CheckArgs.checkRange;
@@ -42,7 +39,7 @@ public enum Territory {
4239
BRA(409, "Brazil", null, new String[]{"BR"}),
4340
RUS(496, "Russia", null, new String[]{"RU"}),
4441
CHN(528, "China", null, new String[]{"CN"}),
45-
ATA(540, "Antarctica"),
42+
ATA(531, "Antarctica"),
4643

4744
VAT(0, "Vatican City", null, null, new String[]{"Holy See)"}),
4845
MCO(1, "Monaco"),
@@ -52,10 +49,10 @@ public enum Territory {
5249
BLM(5, "Saint-Barthelemy"),
5350
NRU(6, "Nauru"),
5451
TUV(7, "Tuvalu"),
55-
MAC(8, "Macau", null, new String[]{"CN-92", "CHN-92", "CN-MC", "CHN-MC"}),
52+
MAC(8, "Macau", null, new String[]{"CN-92", "CHN-92", "CN-MC", "CHN-MC"}, new String[]{"Aomen"}),
5653
SXM(9, "Sint Maarten"),
5754
MAF(10, "Saint-Martin"),
58-
NFK(11, "Norfolk and Philip Island", null, new String[]{"AU-NI", "AUS-NI", "AU-NF", "AUS-NF"},
55+
NFK(11, "Norfolk and Philip Island", null, new String[]{"AU-NF", "AUS-NF"},
5956
new String[]{"Philip Island"}),
6057
PCN(12, "Pitcairn Islands"),
6158
BVT(13, "Bouvet Island"),
@@ -86,7 +83,7 @@ public enum Territory {
8683
GRD(37, "Grenada"),
8784
VIR(38, "US Virgin Islands", null, new String[]{"US-VI", "USA-VI"}, new String[]{"Virgin Islands, US"}),
8885
MYT(39, "Mayotte"),
89-
SJM(40, "Svalbard and Jan Mayen", null, null, new String[]{"Jan Mayen"}),
86+
SJM(40, "Svalbard and Jan Mayen", null, null, new String[]{"Jan Mayen", "Spitsbergen"}),
9087
VCT(41, "Saint Vincent and the Grenadines", null, null, new String[]{"Grenadines"}),
9188
HMD(42, "Heard Island and McDonald Islands", null, new String[]{"AU-HM", "AUS-HM"},
9289
new String[]{"McDonald Islands"}),
@@ -108,7 +105,7 @@ public enum Territory {
108105
KIR(58, "Kiribati"),
109106
TCA(59, "Turks and Caicos Islands", null, null, new String[]{"Caicos Islands"}),
110107
STP(60, "Sao Tome and Principe", null, null, new String[]{"Principe"}),
111-
HKG(61, "Hong Kong", null, new String[]{"CN-91", "CHN-91", "CN-HK", "CHN-HK"}),
108+
HKG(61, "Hong Kong", null, new String[]{"CN-91", "CHN-91", "CN-HK", "CHN-HK"}, new String[]{"Xianggang"}),
112109
MTQ(62, "Martinique"),
113110
FRO(63, "Faroe Islands"),
114111
GLP(64, "Guadeloupe"),
@@ -264,7 +261,7 @@ public enum Territory {
264261
NAM(213, "Namibia"),
265262
VEN(214, "Venezuela"),
266263
NGA(215, "Nigeria"),
267-
TZA(216, "Tanzania", null, new String[]{"EAZ"}),
264+
TZA(216, "Tanzania"),
268265
EGY(217, "Egypt"),
269266
MRT(218, "Mauritania"),
270267
BOL(219, "Bolivia"),
@@ -330,7 +327,7 @@ public enum Territory {
330327
IN_MN(279, "Manipur", ParentTerritory.IND),
331328
IN_TR(280, "Tripura", ParentTerritory.IND),
332329
IN_MZ(281, "Mizoram", ParentTerritory.IND),
333-
IN_SK(282, "Sikkim", ParentTerritory.IND, new String[]{"IN-SKM"}),
330+
IN_SK(282, "Sikkim", ParentTerritory.IND),
334331
IN_PB(283, "Punjab", ParentTerritory.IND),
335332
IN_HR(284, "Haryana", ParentTerritory.IND),
336333
IN_AR(285, "Arunachal Pradesh", ParentTerritory.IND),
@@ -339,7 +336,7 @@ public enum Territory {
339336
IN_UT(288, "Uttarakhand", ParentTerritory.IND, new String[]{"IN-UK"}),
340337
IN_GA(289, "Goa", ParentTerritory.IND),
341338
IN_KL(290, "Kerala", ParentTerritory.IND),
342-
IN_TN(291, "Tamil Nuda", ParentTerritory.IND),
339+
IN_TN(291, "Tamil Nadu", ParentTerritory.IND),
343340
IN_HP(292, "Himachal Pradesh", ParentTerritory.IND),
344341
IN_JK(293, "Jammu and Kashmir", ParentTerritory.IND),
345342
IN_CT(294, "Chhattisgarh", ParentTerritory.IND, new String[]{"IN-CG"}),
@@ -357,13 +354,13 @@ public enum Territory {
357354
IN_PY(306, "Puducherry", ParentTerritory.IND),
358355
AU_NSW(307, "New South Wales", ParentTerritory.AUS),
359356
AU_ACT(308, "Australian Capital Territory", ParentTerritory.AUS),
360-
AU_JBT(309, "Jervis Bay Territory", ParentTerritory.AUS, new String[]{"AU-JB"}),
357+
AU_JBT(309, "Jervis Bay Territory", ParentTerritory.AUS),
361358
AU_NT(310, "Northern Territory", ParentTerritory.AUS),
362359
AU_SA(311, "South Australia", ParentTerritory.AUS),
363-
AU_TAS(312, "Tasmania", ParentTerritory.AUS, new String[]{"AU-TS"}),
360+
AU_TAS(312, "Tasmania", ParentTerritory.AUS),
364361
AU_VIC(313, "Victoria", ParentTerritory.AUS),
365362
AU_WA(314, "Western Australia", ParentTerritory.AUS),
366-
AU_QLD(315, "Queensland", ParentTerritory.AUS, new String[]{"AU-QL"}),
363+
AU_QLD(315, "Queensland", ParentTerritory.AUS),
367364
BR_DF(316, "Distrito Federal", ParentTerritory.BRA),
368365
BR_SE(317, "Sergipe", ParentTerritory.BRA),
369366
BR_AL(318, "Alagoas", ParentTerritory.BRA),
@@ -571,16 +568,7 @@ public enum Territory {
571568
CN_XJ(527, "Xinjiang Uyghur", ParentTerritory.CHN, new String[]{"CN-65"}),
572569
UMI(529, "United States Minor Outlying Islands", null, new String[]{"US-UM", "USA-UM", "JTN"}),
573570
CPT(530, "Clipperton Island"),
574-
AT0(531, "Macquarie Island", ParentTerritory.ATA),
575-
AT1(532, "Ross Dependency", ParentTerritory.ATA),
576-
AT2(533, "Adelie Land", ParentTerritory.ATA),
577-
AT3(534, "Australian Antarctic Territory", ParentTerritory.ATA),
578-
AT4(535, "Queen Maud Land", ParentTerritory.ATA),
579-
AT5(536, "British Antarctic Territory", ParentTerritory.ATA),
580-
AT6(537, "Chile Antartica", ParentTerritory.ATA),
581-
AT7(538, "Argentine Antarctica", ParentTerritory.ATA),
582-
AT8(539, "Peter 1 Island", ParentTerritory.ATA),
583-
AAA(541, "International", null, null, new String[]{"Worldwide", "Earth"});
571+
AAA(532, "International", null, null, new String[]{"Worldwide", "Earth"});
584572

585573
private final int territoryCode;
586574
@Nonnull
@@ -802,15 +790,22 @@ private Territory(
802790
* Static initialization of the static data structures.
803791
*/
804792
static {
793+
final String errorPrefix = "Initializing error: ";
805794
codeList = new ArrayList<>();
806795
nameMap = new HashMap<>();
807796
parentList = new ArrayList<>();
808797
final ArrayList<Integer> territoryCodeList = new ArrayList<>();
798+
final Set<String> aliasesSet = new HashSet<>();
809799

810800
for (final Territory territory : Territory.values()) {
811801
final int territoryCode = territory.getTerritoryCode();
812-
assert (0 <= territoryCode) && (territoryCode < Territory.values().length) : territoryCode;
813-
assert !territoryCodeList.contains(territoryCode) : territoryCode;
802+
if ((territoryCode < 0) || (territoryCode >= Territory.values().length)) {
803+
throw new ExceptionInInitializerError(errorPrefix + "territory code out of range: " + territoryCode);
804+
805+
}
806+
if (territoryCodeList.contains(territoryCode)) {
807+
throw new ExceptionInInitializerError(errorPrefix + "non-unique territory code: " + territoryCode);
808+
}
814809
territoryCodeList.add(territory.getTerritoryCode());
815810

816811
final int initialCodeListSize = codeList.size();
@@ -823,6 +818,10 @@ private Territory(
823818
}
824819
addNameWithParentVariants(territory.toString(), territory);
825820
for (final String alias : territory.aliases) {
821+
if (aliasesSet.contains(alias)) {
822+
throw new ExceptionInInitializerError(errorPrefix + "non-unique alias: " + alias);
823+
}
824+
aliasesSet.add(alias);
826825
addNameWithParentVariants(alias, territory);
827826
}
828827
}
@@ -932,7 +931,7 @@ private static void addName(@Nonnull final String name, @Nonnull final Territory
932931
for (int i = 0; i < territories.size(); i++) {
933932

934933
final Territory existingTerritoryParent = territories.get(i).getParentTerritory();
935-
if (existingTerritoryParent == null && territories.get(i).toString().contentEquals(name)) {
934+
if ((existingTerritoryParent == null) && territories.get(i).toString().contentEquals(name)) {
936935
// A primary identifier always takes priority.
937936
return;
938937
}

src/main/resources/com/mapcode/mminfo.dat

100644100755
-300 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)