Skip to content

Commit d8cc5db

Browse files
committed
Returns ISO3 countries also when restricted to ISO2
1 parent 6554a3c commit d8cc5db

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,23 @@ public static List<Mapcode> encodeRestrictToCountryISO2(final double latDeg, fin
130130
@Nonnull final String countryISO2)
131131
throws IllegalArgumentException {
132132
checkNonnull("countryISO2", countryISO2);
133+
final String countryISO3 = Territory.fromCountryISO2(countryISO2).toString();
133134
final String prefix = countryISO2.toUpperCase() + '-';
134135
final List<Mapcode> mapcodes = encode(latDeg, lonDeg);
135136
final List<Mapcode> filtered = new ArrayList<Mapcode>();
136137
for (final Mapcode mapcode : mapcodes) {
138+
137139
if (mapcode.getTerritory().toString().startsWith(prefix)) {
140+
// If the mapcode starts with the ISO 2 code, it's OK.
138141
filtered.add(mapcode);
142+
143+
} else if (mapcode.getTerritory().toString().equals(countryISO3)) {
144+
145+
// Otherwise, if it's the correct country ISO 3 code, it's also OK.
146+
filtered.add(mapcode);
147+
} else {
148+
149+
// Nothing.
139150
}
140151
}
141152
return filtered;

src/test/java/com/mapcode/EncoderTest.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public void legalArguments() {
263263
}
264264

265265
private static final Point CIUDAD_JUAREZ = Point.fromDeg(31.7, -106.5);
266+
private static final Point VAALS = Point.fromDeg(50.8, 6.0);
266267

267268
@Test
268269
public void testEncodeCiudadJuarez() {
@@ -273,28 +274,51 @@ public void testEncodeCiudadJuarez() {
273274
}
274275

275276
@Test
276-
public void testEncodeRestrictToCountryISO2() {
277-
LOG.info("testEncodeRestrictToCountryISO2");
278-
final Point ciudadJuarez = Point.fromDeg(31.7, -106.5);
279-
final List<Mapcode> mapcodesMX = MapcodeCodec.encodeRestrictToCountryISO2(ciudadJuarez, "MX");
280-
assertEquals(5, mapcodesMX.size());
277+
public void testEncodeRestrictToCountryISO2CountryWithSubdivision() {
278+
LOG.info("testEncodeRestrictToCountryISO2CountryWithSubdivision");
279+
final List<Mapcode> mapcodesMX = MapcodeCodec.encodeRestrictToCountryISO2(CIUDAD_JUAREZ, "MX");
280+
assertEquals(7, mapcodesMX.size());
281281
assertEquals("MX-CHH 5S.0G", mapcodesMX.get(0).toString());
282282

283-
final List<Mapcode> mapcodesUS = MapcodeCodec.encodeRestrictToCountryISO2(ciudadJuarez, "us");
284-
assertEquals(4, mapcodesUS.size());
283+
final List<Mapcode> mapcodesUS = MapcodeCodec.encodeRestrictToCountryISO2(CIUDAD_JUAREZ, "us");
284+
assertEquals(5, mapcodesUS.size());
285285
assertEquals("US-NM T1DZ.338", mapcodesUS.get(0).toString());
286286
}
287287

288288
@Test
289-
public void testEncodeRestrictToCountryISO3() {
290-
LOG.info("testEncodeRestrictToCountryISO3");
289+
public void testEncodeRestrictToCountryISO2CountryWithoutSubdivision() {
290+
LOG.info("testEncodeRestrictToCountryISO2CountryWithoutSubdivision");
291+
final List<Mapcode> mapcodesNL = MapcodeCodec.encodeRestrictToCountryISO2(VAALS, "NL");
292+
assertEquals(2, mapcodesNL.size());
293+
assertEquals("NLD ZNV.W78", mapcodesNL.get(0).toString());
294+
295+
final List<Mapcode> mapcodesBE = MapcodeCodec.encodeRestrictToCountryISO2(VAALS, "be");
296+
assertEquals(2, mapcodesBE.size());
297+
assertEquals("BEL DRQ.PNK", mapcodesBE.get(0).toString());
298+
}
299+
300+
@Test
301+
public void testEncodeRestrictToCountryISO3WithSubdivision() {
302+
LOG.info("testEncodeRestrictToCountryISO3WithSubdivision");
291303
final Point ciudadJuarez = Point.fromDeg(31.7, -106.5);
292-
final List<Mapcode> mapcodesMEX = MapcodeCodec.encodeRestrictToCountryISO3(ciudadJuarez, "MEX");
304+
final List<Mapcode> mapcodesMEX = MapcodeCodec.encodeRestrictToCountryISO3(CIUDAD_JUAREZ, "MEX");
293305
assertEquals(2, mapcodesMEX.size());
294306
assertEquals("MEX 4CMT.DLX", mapcodesMEX.get(0).toString());
295307

296-
final List<Mapcode> mapcodesUSA = MapcodeCodec.encodeRestrictToCountryISO3(ciudadJuarez, "usa");
308+
final List<Mapcode> mapcodesUSA = MapcodeCodec.encodeRestrictToCountryISO3(CIUDAD_JUAREZ, "usa");
297309
assertEquals(1, mapcodesUSA.size());
298310
assertEquals("USA NG4F.K745", mapcodesUSA.get(0).toString());
299311
}
312+
313+
@Test
314+
public void testEncodeRestrictToCountryISO3CountryWithoutSubdivision() {
315+
LOG.info("testEncodeRestrictToCountryISO3CountryWithoutSubdivision");
316+
final List<Mapcode> mapcodesNLD = MapcodeCodec.encodeRestrictToCountryISO3(VAALS, "NLD");
317+
assertEquals(2, mapcodesNLD.size());
318+
assertEquals("NLD ZNV.W78", mapcodesNLD.get(0).toString());
319+
320+
final List<Mapcode> mapcodesBEL = MapcodeCodec.encodeRestrictToCountryISO3(VAALS, "bel");
321+
assertEquals(2, mapcodesBEL.size());
322+
assertEquals("BEL DRQ.PNK", mapcodesBEL.get(0).toString());
323+
}
300324
}

0 commit comments

Comments
 (0)