Skip to content

Commit b14bcf8

Browse files
committed
Allows use of territory names as well as codes
1 parent 74f3be4 commit b14bcf8

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ static Territory fromNumber(final int number) throws UnknownTerritoryException {
646646
}
647647

648648
/**
649-
* Get a territory from a mapcode territory abbreviation. Note that the provided abbreviation is NOT an
649+
* Get a territory from a mapcode territory abbreviation (or a territory name). Note that the provided abbreviation is NOT an
650650
* ISO code: it's a mapcode prefix. As local mapcodes for subdivisions have been optimized to prefer to use 2-character
651651
* subdivisions codes in local codes, subdivisions are preferred over countries in this case.
652652
*
@@ -658,7 +658,7 @@ static Territory fromNumber(final int number) throws UnknownTerritoryException {
658658
*
659659
* Brazilian mapcodes, on the other hand, would be specified as "BRA BDHP.JK39-1D", using the ISO 3 letter code.
660660
*
661-
* @param alphaCode Territory, alphanumeric code.
661+
* @param alphaCode Territory name or alphanumeric code.
662662
* @return Territory.
663663
* @throws UnknownTerritoryException Thrown if incorrect numeric or alphanumeric code.
664664
*/
@@ -828,7 +828,7 @@ private Territory(
828828
int min = Integer.MAX_VALUE;
829829
int max = Integer.MIN_VALUE;
830830
final Set<Integer> territoryCodes = new HashSet<Integer>();
831-
final Set<String> aliasesSet = new HashSet<String>();
831+
final Set<String> namesSet = new HashSet<String>();
832832

833833
for (final Territory territory : Territory.values()) {
834834
final int territoryNumber = territory.getNumber();
@@ -849,14 +849,33 @@ private Territory(
849849
if ((territory.parentTerritory != null) && !parentList.contains(territory.parentTerritory)) {
850850
parentList.add(territory.parentTerritory);
851851
}
852+
853+
// Add territory codes and alias codes.
854+
if (namesSet.contains(territory.toString())) {
855+
throw new ExceptionInInitializerError(errorPrefix + "non-unique territory: " + territory.toString());
856+
}
857+
namesSet.add(territory.toString());
852858
addNameWithParentVariants(territory.toString(), territory);
853859
for (final String alias : territory.aliases) {
854-
if (aliasesSet.contains(alias)) {
860+
if (namesSet.contains(alias)) {
855861
throw new ExceptionInInitializerError(errorPrefix + "non-unique alias: " + alias);
856862
}
857-
aliasesSet.add(alias);
863+
namesSet.add(alias);
858864
addNameWithParentVariants(alias, territory);
859865
}
866+
867+
// Add territory fullnames and aliases as well. Skip special case: territory name == territory code (e.g. USA).
868+
if (namesSet.contains(territory.fullName.toUpperCase()) && !territory.toString().equals(territory.fullName.toUpperCase())) {
869+
throw new ExceptionInInitializerError(errorPrefix + "non-unique fullName: " + territory.fullName.toUpperCase());
870+
}
871+
addNameWithParentVariants(territory.fullName.toUpperCase(), territory);
872+
for (final String fullNameAlias : territory.fullNameAliases) {
873+
if (namesSet.contains(fullNameAlias.toUpperCase())) {
874+
throw new ExceptionInInitializerError(errorPrefix + "non-unique fullName alias: " + fullNameAlias);
875+
}
876+
namesSet.add(fullNameAlias.toUpperCase());
877+
addNameWithParentVariants(fullNameAlias.toUpperCase(), territory);
878+
}
860879
min = Math.min(min, territory.number);
861880
max = Math.max(max, territory.number);
862881
}

src/site/apt/ReleaseNotes.apt.vm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Release Notes (Version ${project.version})
1616

1717
* Use multi-threading for long running test to speed them up (uses all CPU cores now).
1818

19+
* Added the ability to use a country name for <<<Territory.fromString()>>>.
20+
1921
* 2.0.0
2022

2123
* Fixes to the data rectangles (primarily intended for ISO proposal).

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ public void emptyTerritoryCodeTest() throws Exception {
3434
Territory.fromString("");
3535
}
3636

37+
@Test
38+
public void checkFullName() throws Exception {
39+
LOG.info("checkFullName");
40+
assertEquals(Territory.AAA, Territory.fromString("International"));
41+
assertEquals(Territory.AAA, Territory.fromString("Worldwide"));
42+
assertEquals(Territory.AAA, Territory.fromString("Earth"));
43+
assertEquals(Territory.NLD, Territory.fromString("Netherlands"));
44+
assertEquals(Territory.CN_XZ, Territory.fromString("Xizang"));
45+
assertEquals(Territory.CN_XZ, Territory.fromString("Tibet"));
46+
}
47+
3748
@Test
3849
public void checkDash() throws Exception {
3950
LOG.info("checkDash");
@@ -126,10 +137,4 @@ public void testFromStringError3() {
126137
LOG.info("testFromStringError3");
127138
Territory.fromString("999");
128139
}
129-
130-
@Test(expected = IllegalArgumentException.class)
131-
public void testFromStringError4() {
132-
LOG.info("testFromStringError4");
133-
Territory.fromString("Netherlands");
134-
}
135140
}

0 commit comments

Comments
 (0)