Skip to content

Commit d3310a1

Browse files
committed
Fixed upper/lowercase
1 parent a094d8d commit d3310a1

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

src/main/java/com/mapcode/Mapcode.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ public Mapcode(
6464
"the regular expression for the mapcode syntax is: " + REGEX_MAPCODE_FORMAT);
6565
}
6666

67-
this.mapcodePrecision2 = mapcode;
68-
if (mapcode.contains("-")) {
69-
this.mapcodePrecision0 = mapcode.substring(0, mapcode.length() - 3);
70-
this.mapcodePrecision1 = mapcode.substring(0, mapcode.length() - 1);
67+
final String mapcodeUppercase = mapcode.toUpperCase();
68+
this.mapcodePrecision2 = mapcodeUppercase;
69+
if (mapcodeUppercase.contains("-")) {
70+
this.mapcodePrecision0 = mapcodeUppercase.substring(0, mapcodeUppercase.length() - 3);
71+
this.mapcodePrecision1 = mapcodeUppercase.substring(0, mapcodeUppercase.length() - 1);
7172
} else {
72-
this.mapcodePrecision0 = mapcode;
73-
this.mapcodePrecision1 = mapcode;
73+
this.mapcodePrecision0 = mapcodeUppercase;
74+
this.mapcodePrecision1 = mapcodeUppercase;
7475
}
7576
this.territory = territory;
7677
}
@@ -223,7 +224,7 @@ public enum MapcodeFormatType {
223224
public static MapcodeFormatType getMapcodeFormatType(@Nonnull final String mapcode) {
224225

225226
// First, decode to ASCII.
226-
final String decodedMapcode = convertToAscii(mapcode);
227+
final String decodedMapcode = convertToAscii(mapcode.toUpperCase());
227228

228229
// Syntax needs to be OK.
229230
if (!PATTERN_MAPCODE_FORMAT.matcher(decodedMapcode).matches()) {
@@ -251,7 +252,7 @@ public static MapcodeFormatType getMapcodeFormatType(@Nonnull final String mapco
251252
* mapcode representing a location on Earth.
252253
*/
253254
public static boolean isValidMapcodeFormat(@Nonnull final String mapcode) {
254-
return getMapcodeFormatType(mapcode) != MapcodeFormatType.MAPCODE_TYPE_INVALID;
255+
return getMapcodeFormatType(mapcode.toUpperCase()) != MapcodeFormatType.MAPCODE_TYPE_INVALID;
255256
}
256257

257258
/**
@@ -262,7 +263,7 @@ public static boolean isValidMapcodeFormat(@Nonnull final String mapcode) {
262263
*/
263264
@Nonnull
264265
public static String convertToAscii(@Nonnull final String mapcode) {
265-
return Decoder.decodeUTF16(mapcode);
266+
return Decoder.decodeUTF16(mapcode.toUpperCase());
266267
}
267268

268269
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public static Mapcode encodeToInternational(
208208
public static Point decode(
209209
@Nonnull final String mapcode) throws UnknownMapcodeException, IllegalArgumentException {
210210
checkNonnull("mapcode", mapcode);
211-
String mapcodeTrimmed = mapcode.trim();
211+
String mapcodeTrimmed = mapcode.trim().toUpperCase();
212212
final int space = mapcodeTrimmed.indexOf(' ');
213213
final Territory territory;
214214
if ((space > 0) && (mapcodeTrimmed.length() > space)) {
@@ -249,7 +249,7 @@ public static Point decode(
249249
@Nonnull final Territory territoryContext) throws UnknownMapcodeException, IllegalArgumentException {
250250
checkNonnull("mapcode", mapcode);
251251
checkNonnull("territoryContext", territoryContext);
252-
final String mapcodeTrimmed = mapcode.trim();
252+
final String mapcodeTrimmed = mapcode.trim().toUpperCase();
253253
if (!Mapcode.isValidMapcodeFormat(mapcodeTrimmed)) {
254254
throw new IllegalArgumentException(mapcode + " is not a correctly formatted mapcode; " +
255255
"the regular expression for the mapcode syntax is: " + Mapcode.REGEX_MAPCODE_FORMAT);

src/site/apt/ReleaseNotes.apt.vm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ Release Notes (Version ${project.version})
99
In any case, never depend on them for your own non-<<<SNAPSHOT>>> releases.
1010
#end
1111

12+
* 1.42.2
13+
14+
* Upper- and lowercase mapcodes always allowed.
15+
16+
[]
17+
1218
* 1.42.1
1319

1420
* Cleaned up source. Removed all pending IntelliJ IDEA inspection warnings and reformatted code

src/test/java/com/mapcode/DecoderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ public void decodeMapcodeWithTerritory() throws Exception {
3434
assertEquals("decode longitude", 4908542, point.getLonMicroDeg());
3535
}
3636

37+
@Test
38+
public void decodeUpperLowercaseMapcode() throws Exception {
39+
LOG.info("decodeUpperLowercaseMapcode");
40+
final Point point1 = MapcodeCodec.decode("XXXXX.1234");
41+
assertEquals("decode latitude", 59596312, point1.getLatMicroDeg());
42+
assertEquals("decode longitude", 155931892, point1.getLonMicroDeg());
43+
44+
final Point point2 = MapcodeCodec.decode("Xxxxx.1234");
45+
assertEquals("decode latitude", 59596312, point2.getLatMicroDeg());
46+
assertEquals("decode longitude", 155931892, point2.getLonMicroDeg());
47+
48+
final Point point3 = MapcodeCodec.decode("xxxxx.1234");
49+
assertEquals("decode latitude", 59596312, point3.getLatMicroDeg());
50+
assertEquals("decode longitude", 155931892, point3.getLonMicroDeg());
51+
}
52+
3753
@Test
3854
public void decodeFullMapcode() throws Exception {
3955
LOG.info("decodeFullMapcode");

0 commit comments

Comments
 (0)