Skip to content

Commit 3d386f6

Browse files
committed
Removed numeric territory and alphabet codes
1 parent bd16378 commit 3d386f6

File tree

7 files changed

+52
-84
lines changed

7 files changed

+52
-84
lines changed

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

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,45 +46,35 @@ public enum Alphabet {
4646
*/
4747
private final int number;
4848

49+
/**
50+
* Private constructor.
51+
*
52+
* @param number Alphabet number, for internal use only.
53+
*/
4954
private Alphabet(final int number) {
5055
this.number = number;
5156
}
5257

53-
public int getNumber() {
54-
return number;
55-
}
56-
5758
/**
58-
* Get an alphabet from a numeric code.
59+
* Get alphabet number. Package private, for internal use only.
5960
*
60-
* @param number Numeric code.
61-
* @return Alphabet.
62-
* @throws UnknownAlphabetException Thrown if code out of range.
61+
* @return Numeric code.
6362
*/
64-
@Nonnull
65-
public static Alphabet fromNumber(final int number) throws UnknownAlphabetException {
66-
if ((number >= 0) && (number < Alphabet.values().length)) {
67-
return Alphabet.values()[number];
68-
}
69-
throw new UnknownAlphabetException(number);
63+
int getNumber() {
64+
return number;
7065
}
7166

7267
/**
7368
* Return alphabet from a string, which can be a numeric or alpha code.
7469
*
75-
* @param numberOrString Alphabet. May be a numeric or alphanumeric code.
70+
* @param alphaCode Alphabet, alphanumeric code.
7671
* @return Alphabet.
7772
* @throws UnknownAlphabetException Thrown if incorrect numeric or alphanumeric code.
7873
*/
7974
@Nonnull
80-
public static Alphabet fromString(@Nonnull final String numberOrString) throws UnknownAlphabetException {
81-
checkNonnull("numberOrString", numberOrString);
82-
final String trimmed = numberOrString.trim().toUpperCase();
83-
try {
84-
return fromNumber(Integer.valueOf(numberOrString));
85-
} catch (final IllegalArgumentException ignored) {
86-
// Ignore. Re-try as alpha code.
87-
}
75+
public static Alphabet fromString(@Nonnull final String alphaCode) throws UnknownAlphabetException {
76+
checkNonnull("alphaCode", alphaCode);
77+
final String trimmed = alphaCode.trim().toUpperCase();
8878
try {
8979
return valueOf(trimmed);
9080
} catch (final IllegalArgumentException ignored) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ private static String aeuUnpack(final String argStr) {
547547
if (v2 < 0) {
548548
v2 = 31;
549549
}
550-
String s = String.valueOf(1000 + v1 + (32 * v2));
550+
final String s = String.valueOf(1000 + v1 + (32 * v2));
551551
str = s.charAt(1) + str.substring(1, lastpos - 1) + s.charAt(2) + s.charAt(3);
552552
voweled = true;
553553
} else if (str.charAt(0) == 'U') { // v.1.50 debug decoding of U+alldigitmapcode
@@ -651,7 +651,7 @@ static String decodeUTF16(final String mapcode) {
651651
return result;
652652
}
653653

654-
static String encodeUTF16(final String mapcode, int alphabetCode) throws IllegalArgumentException {
654+
static String encodeUTF16(final String mapcode, final int alphabetCode) throws IllegalArgumentException {
655655
final String mapcodeToEncode;
656656
if (ASCII2LANGUAGE[alphabetCode][4] == MISSCODE) {
657657

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ private static List<Mapcode> encode(final double argLatDeg, final double argLonD
6565
argLatDeg, argLonDeg, (territory == null) ? null : territory.name(), isRecursive, limitToOneResult,
6666
allowWorld);
6767

68-
double latDeg = Point.mapToLat(argLatDeg);
69-
double lonDeg = Point.mapToLon(argLonDeg);
68+
final double latDeg = Point.mapToLat(argLatDeg);
69+
final double lonDeg = Point.mapToLon(argLonDeg);
7070
Territory stateOverride = argStateOverride;
7171

7272
final Point pointToEncode = Point.fromDeg(latDeg, lonDeg);

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,12 @@ public enum Territory {
580580
private final String[] fullNameAliases;
581581

582582
/**
583-
* Return the numeric territory code for a territory.
583+
* Return the numeric territory code for a territory. Package private, because territory numbers are no longer
584+
* exposed publicly.
584585
*
585586
* @return Integer territory code.
586587
*/
587-
public int getNumber() {
588+
int getNumber() {
588589
return number;
589590
}
590591

@@ -629,14 +630,15 @@ public String[] getFullNameAliases() {
629630
}
630631

631632
/**
632-
* Return the territory for a specific code.
633+
* Return the territory for a specific code. Package private, because territory numbers are no longer exposed
634+
* publicly.
633635
*
634636
* @param number Numeric territory code.
635637
* @return Territory.
636638
* @throws UnknownTerritoryException Thrown if incorrect numeric or alphanumeric code.
637639
*/
638640
@Nonnull
639-
public static Territory fromNumber(final int number) throws UnknownTerritoryException {
641+
static Territory fromNumber(final int number) throws UnknownTerritoryException {
640642
if ((number < 0) || (number >= codeList.size())) {
641643
throw new UnknownTerritoryException(number);
642644
}
@@ -656,32 +658,32 @@ public static Territory fromNumber(final int number) throws UnknownTerritoryExce
656658
*
657659
* Brazilian mapcodes, on the other hand, would be specified as "BRA BDHP.JK39-1D", using the ISO 3 letter code.
658660
*
659-
* @param numericOrAlpha Territory, may be numeric or alphanumeric code.
661+
* @param alphaCode Territory, alphanumeric code.
660662
* @return Territory.
661663
* @throws UnknownTerritoryException Thrown if incorrect numeric or alphanumeric code.
662664
*/
663665
@Nonnull
664-
public static Territory fromString(@Nonnull final String numericOrAlpha) throws UnknownTerritoryException {
665-
checkNonnull("numericOrAlpha", numericOrAlpha);
666-
return createFromString(numericOrAlpha, null);
666+
public static Territory fromString(@Nonnull final String alphaCode) throws UnknownTerritoryException {
667+
checkNonnull("alphaCode", alphaCode);
668+
return createFromString(alphaCode, null);
667669
}
668670

669671
/**
670672
* Get a territory from a name, specifying a parent territory for disambiguation.
671673
*
672-
* @param numericOrAlpha Territory, may be numeric or alphanumeric code. See {@link #fromString(String)}
674+
* @param alphaCode Territory, alphanumeric code. See {@link #fromString(String)}
673675
* for an explanation of the format for this name. (This is NOT strictly an ISO code!)
674676
* @param parentTerritory Parent territory.
675677
* @return Territory.
676678
* @throws UnknownTerritoryException Thrown if the territory is not found given the parentTerritory.
677679
*/
678680
@Nonnull
679681
public static Territory fromString(
680-
@Nonnull final String numericOrAlpha,
682+
@Nonnull final String alphaCode,
681683
@Nonnull final ParentTerritory parentTerritory) throws UnknownTerritoryException {
682-
checkNonnull("numericOrAlpha", numericOrAlpha);
684+
checkNonnull("alphaCode", alphaCode);
683685
checkNonnull("parentTerritory", parentTerritory);
684-
return createFromString(numericOrAlpha, parentTerritory);
686+
return createFromString(alphaCode, parentTerritory);
685687
}
686688

687689
/**
@@ -772,7 +774,7 @@ public boolean hasSubdivisions() {
772774
}
773775

774776
/**
775-
* Local constructors to create a territory code.
777+
* Private constructors to create a territory code.
776778
*/
777779
private Territory(
778780
final int number,
@@ -869,26 +871,18 @@ private Territory(
869871
/**
870872
* Get a territory from a name, specifying a parent territory for disambiguation.
871873
*
872-
* @param numericOrAlpha Territory name.
874+
* @param alphaCode Territory, alphanumeric code.
873875
* @param parentTerritory Parent territory.
874876
* @return Territory.
875877
* @throws UnknownTerritoryException Thrown if the territory is not found.
876878
*/
877879
@Nonnull
878880
private static Territory createFromString(
879-
@Nonnull final String numericOrAlpha,
881+
@Nonnull final String alphaCode,
880882
@Nullable final ParentTerritory parentTerritory) throws UnknownTerritoryException {
881-
final String trimmed = Mapcode.convertStringToPlainAscii(numericOrAlpha.trim().replace('_', '-')).toUpperCase();
882-
883-
// First, try as numeric code.
884-
try {
885-
final Integer territoryCode = Integer.valueOf(trimmed);
886-
return fromNumber(territoryCode);
887-
} catch (final NumberFormatException ignored) {
888-
// Re-try as alpha code.
889-
}
883+
final String trimmed = Mapcode.convertStringToPlainAscii(alphaCode.trim().replace('_', '-')).toUpperCase();
890884

891-
// Now, try as alpha code.
885+
// Try as alpha code.
892886
final List<Territory> territories = nameMap.get(trimmed);
893887
if (territories != null) {
894888
if (parentTerritory == null) {

src/site/apt/ReleaseNotes.apt.vm

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

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

16+
* Removed functionality to use numeric territory codes; only alpha codes are accepted.
17+
1618
* Note that this release only allows high-precision mapcodes up to 2 additional suffix characters.
1719
A future release will be scheduled to allow up to 8 suffix characters (nanometer accuracy).
1820

src/test/java/com/mapcode/AlphabetTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ public void testConvertToAlphabet() throws Exception {
4747
public void testFromString() throws Exception {
4848
LOG.info("testFromString");
4949
assertEquals(Alphabet.ROMAN, Alphabet.fromString("ROMAN"));
50-
assertEquals(Alphabet.ROMAN, Alphabet.fromString("0"));
5150
assertEquals(Alphabet.ROMAN, Alphabet.fromString("roman"));
5251

5352
for (final Alphabet alphabet : Alphabet.values()) {
5453
assertEquals(alphabet, Alphabet.fromString(alphabet.toString()));
55-
assertEquals(alphabet, Alphabet.fromString(String.valueOf(alphabet.getNumber())));
5654
}
5755
}
5856

57+
@Test(expected = UnknownAlphabetException.class)
58+
public void testFromStringNumeric() throws Exception {
59+
LOG.info("testFromStringNumeric");
60+
assertEquals(Alphabet.ROMAN, Alphabet.fromString("0"));
61+
}
62+
5963
@Test(expected = IllegalArgumentException.class)
6064
public void testFromStringError1() {
6165
LOG.info("testFromStringError1");

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

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

37-
@Test
38-
public void checkAlphabetCode() throws Exception {
39-
LOG.info("checkAlphabetCode");
40-
int i = 0;
41-
for (final Alphabet alphabet : Alphabet.values()) {
42-
assertEquals(alphabet, Alphabet.fromNumber(i));
43-
++i;
44-
}
45-
}
46-
47-
@Test(expected = UnknownAlphabetException.class)
48-
public void checkAlphabetCodeError1() throws Exception {
49-
LOG.info("checkAlphabetCodeError1");
50-
Alphabet.fromNumber(-1);
51-
}
52-
53-
@Test(expected = UnknownAlphabetException.class)
54-
public void checkAlphabetCodeError2() throws Exception {
55-
LOG.info("checkAlphabetCodeError2");
56-
Alphabet.fromNumber(Alphabet.values().length);
57-
}
58-
5937
@Test
6038
public void checkDash() throws Exception {
6139
LOG.info("checkDash");
@@ -84,8 +62,8 @@ public void disambiguateMNTest2() throws Exception {
8462
}
8563

8664
@Test
87-
public void territoryFromStringTest() throws Exception {
88-
LOG.info("territoryFromStringTest");
65+
public void testTerritoryFromString() throws Exception {
66+
LOG.info("testTerritoryFromString");
8967

9068
// Accept ISO-style codes.
9169
assertEquals(Territory.NLD, Territory.fromString("NLD"));
@@ -106,17 +84,17 @@ public void territoryFromStringTest() throws Exception {
10684
assertEquals(Territory.USA, Territory.fromString("USA"));
10785
assertEquals(Territory.USA, Territory.fromString("US"));
10886

109-
// Accept numeric codes as well.
110-
assertEquals(Territory.VAT, Territory.fromString("0"));
111-
assertEquals(Territory.AAA, Territory.fromString("532"));
112-
assertEquals(Territory.NLD, Territory.fromString("112"));
113-
11487
for (final Territory territory : Territory.values()) {
11588
assertEquals(territory, Territory.fromString(territory.toString()));
116-
assertEquals(territory, Territory.fromString(String.valueOf(territory.getNumber())));
11789
}
11890
}
11991

92+
@Test(expected = UnknownTerritoryException.class)
93+
public void testTerritoryFromStringNumeric() throws Exception {
94+
LOG.info("testTerritoryFromStringNumeric");
95+
assertEquals(Territory.VAT, Territory.fromString("0"));
96+
}
97+
12098
@Test
12199
public void checkAlphabet() throws Exception {
122100
LOG.info("checkAlphabet");

0 commit comments

Comments
 (0)