Skip to content

Commit b1cd072

Browse files
committed
Added support for ISO2/3 at the same time.
1 parent 4f734ac commit b1cd072

File tree

5 files changed

+141
-10
lines changed

5 files changed

+141
-10
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>2.4.6</version>
11+
<version>2.4.7A-SNAPSHOT</version>
1212

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

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,39 @@ public static List<Mapcode> encodeRestrictToCountryISO3(@Nonnull final Point poi
186186
return encodeRestrictToCountryISO3(point.getLatDeg(), point.getLonDeg(), countryISO3);
187187
}
188188

189+
/**
190+
* Encode a lat/lon pair to a list of mapcodes, like {@link #encode(double, double)}.
191+
* The result list is limited to those mapcodes that belong to the provided ISO 3166 country code, 2 or 3
192+
* characters.
193+
*
194+
* @param latDeg Latitude, accepted range: -90..90.
195+
* @param lonDeg Longitude, accepted range: -180..180.
196+
* @param countryISO ISO 3166 country code, 2 or 3 characters.
197+
* @return Possibly empty, ordered list of mapcode information records, see {@link Mapcode}.
198+
* @throws IllegalArgumentException Thrown if latitude or longitude are out of range, or if the ISO code is invalid.
199+
*/
200+
@Nonnull
201+
public static List<Mapcode> encodeRestrictToCountryISO(final double latDeg, final double lonDeg,
202+
@Nonnull final String countryISO)
203+
throws IllegalArgumentException {
204+
checkNonnull("countryISO", countryISO);
205+
List<Mapcode> mapcodes;
206+
try {
207+
mapcodes = encodeRestrictToCountryISO2(latDeg, lonDeg, countryISO);
208+
} catch (final IllegalArgumentException ignored) {
209+
mapcodes = encodeRestrictToCountryISO3(latDeg, lonDeg, countryISO);
210+
}
211+
return mapcodes;
212+
}
213+
214+
@Nonnull
215+
public static List<Mapcode> encodeRestrictToCountryISO(@Nonnull final Point point,
216+
@Nonnull final String countryISO)
217+
throws IllegalArgumentException {
218+
checkNonnull("point", point);
219+
return encodeRestrictToCountryISO(point.getLatDeg(), point.getLonDeg(), countryISO);
220+
}
221+
189222
/**
190223
* Encode a lat/lon pair to its shortest mapcode with territory information.
191224
*

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

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,37 @@ public static Territory fromCountryISO2(@Nonnull final String countryISO2) {
716716
return fromString(getCountryISO3FromISO2(countryISO2));
717717
}
718718

719+
/**
720+
* Create a Territory object from a valid ISO 3166 country code, 3 characters.
721+
*
722+
* @param countryISO3 ISO 3166 country code, 3 characters.
723+
* @return Territory object for the country.
724+
* @throws IllegalArgumentException Thrown if the country code is not a valid ISO 3166 code, 3 characters.
725+
*/
726+
@Nonnull
727+
public static Territory fromCountryISO3(@Nonnull final String countryISO3) {
728+
if (!MAP_ISO3_TO_ISO2.containsKey(countryISO3.toUpperCase())) {
729+
throw new IllegalArgumentException("Parameter " + countryISO3 + " must be a valid ISO 3166 country code, 3 characters");
730+
}
731+
return fromString(countryISO3);
732+
}
733+
734+
/**
735+
* Create a Territory object from a valid ISO 3166 country code, 2 or 3 characters.
736+
*
737+
* @param countryISO ISO 3166 country code, 2 or 3 characters.
738+
* @return Territory object for the country.
739+
* @throws IllegalArgumentException Thrown if the country code is not a valid ISO 3166 code, 2 or 3 characters.
740+
*/
741+
@Nonnull
742+
public static Territory fromCountryISO(@Nonnull final String countryISO) {
743+
try {
744+
return fromCountryISO2(countryISO);
745+
} catch (final IllegalArgumentException ignored) {
746+
return fromCountryISO3(countryISO);
747+
}
748+
}
749+
719750
/**
720751
* Return the ISO 3166 2 character country code for a ISO 3166 3 character code.
721752
*
@@ -754,18 +785,23 @@ public static String getCountryISO3FromISO2(@Nonnull final String countryISO2) {
754785
}
755786

756787
/**
757-
* Create a Territory object from a valid ISO 3166 country code, 3 characters.
788+
* Returns all ISO 3166 country codes, 2 characters.
758789
*
759-
* @param countryISO3 ISO 3166 country code, 3 characters.
760-
* @return Territory object for the country.
761-
* @throws IllegalArgumentException Thrown if the country code is not a valid ISO 3166 code, 3 characters.
790+
* @return ISO 3166 country codes, 2 characters.
762791
*/
763792
@Nonnull
764-
public static Territory fromCountryISO3(@Nonnull final String countryISO3) {
765-
if (!MAP_ISO3_TO_ISO2.containsKey(countryISO3.toUpperCase())) {
766-
throw new IllegalArgumentException("Parameter " + countryISO3 + " must be a valid ISO 3166 country code, 3 characters");
767-
}
768-
return fromString(countryISO3);
793+
public static Set<String> allCountryISO2Codes() {
794+
return COUNTRY_ISO2_CODES;
795+
}
796+
797+
/**
798+
* Returns all ISO 3166 country codes, 3 characters.
799+
*
800+
* @return ISO 3166 country codes, 3 characters.
801+
*/
802+
@Nonnull
803+
public static Set<String> allCountryISO3Codes() {
804+
return COUNTRY_ISO3_CODES;
769805
}
770806

771807
/**
@@ -903,6 +939,10 @@ public boolean hasSubdivisions() {
903939
@Nonnull
904940
private static final Map<String, String> MAP_ISO3_TO_ISO2;
905941
@Nonnull
942+
private static final Set<String> COUNTRY_ISO2_CODES;
943+
@Nonnull
944+
private static final Set<String> COUNTRY_ISO3_CODES;
945+
@Nonnull
906946
private static final List<Territory> CODE_LIST;
907947
@Nonnull
908948
private static final Map<String, List<Territory>> NAME_MAP;
@@ -919,6 +959,10 @@ public boolean hasSubdivisions() {
919959

920960
// Add Clipperton Island (not recognized by Java).
921961
MAP_ISO3_TO_ISO2.put("CPT", "CP");
962+
963+
// Create fixed sets of the keys and values.
964+
COUNTRY_ISO2_CODES = new HashSet<String>(MAP_ISO3_TO_ISO2.values());
965+
COUNTRY_ISO3_CODES = new HashSet<String>(MAP_ISO3_TO_ISO2.keySet());
922966
}
923967

924968
/**

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,36 +276,60 @@ public void testEncodeCiudadJuarez() {
276276
public void testEncodeRestrictToCountryISOCountryWithSubdivision() {
277277
LOG.info("testEncodeRestrictToCountryISOCountryWithSubdivision");
278278
final List<Mapcode> mapcodesMX = MapcodeCodec.encodeRestrictToCountryISO2(CIUDAD_JUAREZ, "MX");
279+
final List<Mapcode> mapcodesMX2 = MapcodeCodec.encodeRestrictToCountryISO(CIUDAD_JUAREZ, "MX");
279280
final List<Mapcode> mapcodesMEX = MapcodeCodec.encodeRestrictToCountryISO3(CIUDAD_JUAREZ, "MEX");
281+
final List<Mapcode> mapcodesMEX2 = MapcodeCodec.encodeRestrictToCountryISO(CIUDAD_JUAREZ, "MEX");
280282
assertEquals(7, mapcodesMX.size());
283+
assertEquals(7, mapcodesMX2.size());
281284
assertEquals(7, mapcodesMEX.size());
285+
assertEquals(7, mapcodesMEX2.size());
282286
assertEquals("MX-CHH 5S.0G", mapcodesMX.get(0).toString());
287+
assertEquals("MX-CHH 5S.0G", mapcodesMX2.get(0).toString());
283288
assertEquals("MX-CHH 5S.0G", mapcodesMEX.get(0).toString());
289+
assertEquals("MX-CHH 5S.0G", mapcodesMEX2.get(0).toString());
284290

285291
final List<Mapcode> mapcodesUS = MapcodeCodec.encodeRestrictToCountryISO2(CIUDAD_JUAREZ, "us");
292+
final List<Mapcode> mapcodesUS2 = MapcodeCodec.encodeRestrictToCountryISO(CIUDAD_JUAREZ, "us");
286293
final List<Mapcode> mapcodesUSA = MapcodeCodec.encodeRestrictToCountryISO3(CIUDAD_JUAREZ, "usa");
294+
final List<Mapcode> mapcodesUSA2 = MapcodeCodec.encodeRestrictToCountryISO(CIUDAD_JUAREZ, "usa");
287295
assertEquals(5, mapcodesUS.size());
296+
assertEquals(5, mapcodesUS2.size());
288297
assertEquals(5, mapcodesUSA.size());
298+
assertEquals(5, mapcodesUSA2.size());
289299
assertEquals("US-NM T1DZ.338", mapcodesUS.get(0).toString());
300+
assertEquals("US-NM T1DZ.338", mapcodesUS2.get(0).toString());
290301
assertEquals("US-NM T1DZ.338", mapcodesUSA.get(0).toString());
302+
assertEquals("US-NM T1DZ.338", mapcodesUSA2.get(0).toString());
291303
}
292304

293305
@Test
294306
public void testEncodeRestrictToCountryISOCountryWithoutSubdivision() {
295307
LOG.info("testEncodeRestrictToCountryISO2CountryWithoutSubdivision");
296308
final List<Mapcode> mapcodesNL = MapcodeCodec.encodeRestrictToCountryISO2(VAALS, "NL");
309+
final List<Mapcode> mapcodesNL2 = MapcodeCodec.encodeRestrictToCountryISO(VAALS, "NL");
297310
final List<Mapcode> mapcodesNLD = MapcodeCodec.encodeRestrictToCountryISO3(VAALS, "NLD");
311+
final List<Mapcode> mapcodesNLD2 = MapcodeCodec.encodeRestrictToCountryISO(VAALS, "NLD");
298312
assertEquals(2, mapcodesNL.size());
313+
assertEquals(2, mapcodesNL2.size());
299314
assertEquals(2, mapcodesNLD.size());
315+
assertEquals(2, mapcodesNLD2.size());
300316
assertEquals("NLD ZNV.W78", mapcodesNL.get(0).toString());
317+
assertEquals("NLD ZNV.W78", mapcodesNL2.get(0).toString());
301318
assertEquals("NLD ZNV.W78", mapcodesNLD.get(0).toString());
319+
assertEquals("NLD ZNV.W78", mapcodesNLD2.get(0).toString());
302320

303321
final List<Mapcode> mapcodesBE = MapcodeCodec.encodeRestrictToCountryISO2(VAALS, "be");
322+
final List<Mapcode> mapcodesBE2 = MapcodeCodec.encodeRestrictToCountryISO(VAALS, "be");
304323
final List<Mapcode> mapcodesBEL = MapcodeCodec.encodeRestrictToCountryISO3(VAALS, "bel");
324+
final List<Mapcode> mapcodesBEL2 = MapcodeCodec.encodeRestrictToCountryISO(VAALS, "bel");
305325
assertEquals(2, mapcodesBE.size());
326+
assertEquals(2, mapcodesBE2.size());
306327
assertEquals(2, mapcodesBEL.size());
328+
assertEquals(2, mapcodesBEL2.size());
307329
assertEquals("BEL DRQ.PNK", mapcodesBE.get(0).toString());
330+
assertEquals("BEL DRQ.PNK", mapcodesBE2.get(0).toString());
308331
assertEquals("BEL DRQ.PNK", mapcodesBEL.get(0).toString());
332+
assertEquals("BEL DRQ.PNK", mapcodesBEL2.get(0).toString());
309333
}
310334

311335
@Test

src/test/java/com/mapcode/TerritoryTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323

24+
import java.util.Set;
25+
2426
import static org.junit.Assert.assertEquals;
2527
import static org.junit.Assert.assertNotEquals;
2628

@@ -253,6 +255,17 @@ public void testFromStringCountryISO3OK() {
253255
assertEquals("USA", Territory.fromCountryISO3("Usa").toString());
254256
}
255257

258+
@Test
259+
public void testFromStringCountryISOOK() {
260+
LOG.info("testFromStringCountryISOOK");
261+
assertEquals("NLD", Territory.fromCountryISO2("NL").toString());
262+
assertEquals("BRA", Territory.fromCountryISO2("br").toString());
263+
assertEquals("USA", Territory.fromCountryISO2("Us").toString());
264+
assertEquals("NLD", Territory.fromCountryISO("NLD").toString());
265+
assertEquals("BRA", Territory.fromCountryISO("bra").toString());
266+
assertEquals("USA", Territory.fromCountryISO("Usa").toString());
267+
}
268+
256269
@Test
257270
public void testGetCountryISO3FromISO2() {
258271
LOG.info("testGetCountryISO3FromISO2");
@@ -281,4 +294,21 @@ public void testGetCountryISO2FromISO3Error() {
281294
Territory.getCountryISO2FromISO3("NL");
282295
}
283296

297+
@Test
298+
public void testGetCountryISO2Codes() {
299+
LOG.info("testGetCountryISO2Codes");
300+
final Set<String> countryISO2Codes = Territory.allCountryISO2Codes();
301+
LOG.info("set={}", countryISO2Codes);
302+
assertEquals(251, countryISO2Codes.size());
303+
assertEquals(2, countryISO2Codes.iterator().next().length());
304+
}
305+
306+
@Test
307+
public void testGetCountryISO3Codes() {
308+
LOG.info("testGetCountryISO3Codes");
309+
final Set<String> countryISO3Codes = Territory.allCountryISO3Codes();
310+
LOG.info("set={}", countryISO3Codes);
311+
assertEquals(251, countryISO3Codes.size());
312+
assertEquals(3, countryISO3Codes.iterator().next().length());
313+
}
284314
}

0 commit comments

Comments
 (0)