Skip to content

Commit cb3a7b4

Browse files
committed
Merge pull request #14 from mapcode-foundation/dev
Release 1.50.3
2 parents 0fad1c5 + 110a25d commit cb3a7b4

File tree

15 files changed

+281
-340
lines changed

15 files changed

+281
-340
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>1.50.2</version>
11+
<version>1.50.3-SNAPSHOT</version>
1212

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

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,44 @@ public enum Alphabet {
4444
* The numeric code is synonym for the alphanumeric code. It can be used in the decoder
4545
* to define a territory as well.
4646
*/
47-
private final int code;
47+
private final int number;
4848

49-
private Alphabet(final int code) {
50-
this.code = code;
49+
private Alphabet(final int number) {
50+
this.number = number;
5151
}
5252

53-
public int getCode() {
54-
return code;
53+
public int getNumber() {
54+
return number;
5555
}
5656

5757
/**
5858
* Get an alphabet from a numeric code.
5959
*
60-
* @param code Numeric code.
60+
* @param number Numeric code.
6161
* @return Alphabet.
6262
* @throws UnknownAlphabetException Thrown if code out of range.
6363
*/
6464
@Nonnull
65-
public static Alphabet fromCode(final int code) throws UnknownAlphabetException {
66-
if ((code >= 0) && (code < Alphabet.values().length)) {
67-
return Alphabet.values()[code];
65+
public static Alphabet fromNumber(final int number) throws UnknownAlphabetException {
66+
if ((number >= 0) && (number < Alphabet.values().length)) {
67+
return Alphabet.values()[number];
6868
}
69-
throw new UnknownAlphabetException(code);
69+
throw new UnknownAlphabetException(number);
7070
}
7171

7272
/**
7373
* Return alphabet from a string, which can be a numeric or alpha code.
7474
*
75-
* @param numericOrAlpha Alphabet. May be a numeric or alphanumeric code.
75+
* @param numberOrString Alphabet. May be a numeric or alphanumeric code.
7676
* @return Alphabet.
7777
* @throws UnknownAlphabetException Thrown if incorrect numeric or alphanumeric code.
7878
*/
7979
@Nonnull
80-
public static Alphabet fromString(@Nonnull final String numericOrAlpha) throws UnknownAlphabetException {
81-
checkNonnull("name", numericOrAlpha);
82-
final String trimmed = numericOrAlpha.trim().toUpperCase();
80+
public static Alphabet fromString(@Nonnull final String numberOrString) throws UnknownAlphabetException {
81+
checkNonnull("numberOrString", numberOrString);
82+
final String trimmed = numberOrString.trim().toUpperCase();
8383
try {
84-
return fromCode(Integer.valueOf(numericOrAlpha));
84+
return fromNumber(Integer.valueOf(numberOrString));
8585
} catch (final IllegalArgumentException ignored) {
8686
// Ignore. Re-try as alpha code.
8787
}
@@ -98,10 +98,10 @@ public static Alphabet fromString(@Nonnull final String numericOrAlpha) throws U
9898
static {
9999
int i = 0;
100100
for (final Alphabet alphabet : Alphabet.values()) {
101-
if (Alphabet.values()[i].code != i) {
102-
throw new ExceptionInInitializerError("Incorrect alphabet code: " + alphabet + ".code should be " + i);
101+
if (Alphabet.values()[i].number != i) {
102+
throw new ExceptionInInitializerError("Incorrect alphabet number: " + alphabet + ".number should be " + i);
103103
}
104104
++i;
105105
}
106106
}
107-
}
107+
}

src/main/java/com/mapcode/CheckArgs.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import javax.annotation.Nonnull;
2020
import javax.annotation.Nullable;
2121

22-
import static com.mapcode.Mapcode.isValidMapcodeFormat;
22+
import static com.mapcode.Mapcode.getPrecisionFormat;
2323

2424
/**
2525
* Package private helper methods to check arguments for validity.
@@ -48,9 +48,8 @@ static void checkNonnull(@Nonnull final String param, @Nullable final Object obj
4848
static void checkMapcodeCode(@Nonnull final String param, @Nullable final String code)
4949
throws IllegalArgumentException {
5050
checkNonnull(param, code);
51-
if (!isValidMapcodeFormat(code)) {
52-
throw new IllegalArgumentException(code + " is not a correctly formatted mapcode code; " +
53-
"the regular expression for the mapcode code syntax is: " + Mapcode.REGEX_MAPCODE);
54-
}
51+
52+
// Throws an exception if the format is incorrect.
53+
getPrecisionFormat(code);
5554
}
5655
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static Point decode(@Nonnull final String argMapcode,
7979
}
8080
}
8181

82-
final int ccode = territory.getCode();
82+
final int ccode = territory.getNumber();
8383

8484
final int from = DataAccess.dataFirstRecord(ccode);
8585
if (DataAccess.dataFlags(from) == 0) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ private static List<Mapcode> encode(final double argLatDeg, final double argLonD
8787
continue;
8888
}
8989

90-
final int from = DataAccess.dataFirstRecord(currentEncodeTerritory.getCode());
90+
final int from = DataAccess.dataFirstRecord(currentEncodeTerritory.getNumber());
9191
final Data mapcoderData = new Data(from);
9292
if (mapcoderData.getFlags() == 0) {
9393
continue;
9494
}
95-
final int upto = DataAccess.dataLastRecord(currentEncodeTerritory.getCode());
95+
final int upto = DataAccess.dataLastRecord(currentEncodeTerritory.getNumber());
9696

9797

9898
final int i = subArea.getSubAreaID();

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

Lines changed: 29 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -271,75 +271,80 @@ public Territory getTerritory() {
271271
Pattern.compile(REGEX_CODE_PRECISION + '$', Pattern.UNICODE_CHARACTER_CLASS);
272272

273273
/**
274-
* This enum describes the types of available mapcodes (as returned by {@link #getMapcodeFormatType(String)}.
274+
* This enum describes the types of available mapcodes (as returned by {@link #getPrecisionFormat(String)}.
275275
*/
276-
public enum FormatType {
276+
public enum PrecisionFormat {
277277
PRECISION_0,
278278
PRECISION_1,
279-
PRECISION_2,
280-
INVALID;
279+
PRECISION_2;
281280

282-
public static FormatType fromPrecision(final int precision) {
283-
switch (precision) {
281+
public static PrecisionFormat fromNumber(final int number) {
282+
switch (number) {
284283
case 0:
285284
return PRECISION_0;
286285
case 1:
287286
return PRECISION_1;
288287
case 2:
289288
return PRECISION_2;
290289
default:
291-
return INVALID;
290+
throw new UnknownPrecisionFormatException("Precision must be in [0, 2], is: " + number, number);
292291
}
293292
}
294293
}
295294

296295
/**
297296
* This method return the mapcode type, given a mapcode string. If the mapcode string has an invalid
298-
* format, {@link FormatType#INVALID} is returned. If another value is returned,
299-
* the precision of the mapcode is given.
297+
* format, an exception is thrown.
300298
*
301299
* Note that this method only checks the syntactic validity of the mapcode, the string format. It does not
302300
* check if the mapcode is really a valid mapcode representing a position on Earth.
303301
*
304302
* @param mapcode Mapcode (optionally with a territory).
305-
* @return Type of mapcode code format, or {@link FormatType#INVALID} if not valid.
306-
* @throws IllegalArgumentException If mapcode has incorrect syntax.
303+
* @return Type of mapcode code format.
304+
* @throws UnknownPrecisionFormatException If precision format is incorrect.
307305
*/
308306
@Nonnull
309-
public static FormatType getMapcodeFormatType(@Nonnull final String mapcode) throws IllegalArgumentException {
307+
public static PrecisionFormat getPrecisionFormat(@Nonnull final String mapcode) throws UnknownPrecisionFormatException {
310308

311309
// First, decode to ASCII.
312310
final String decodedMapcode = convertStringToPlainAscii(mapcode).toUpperCase();
313311

314312
// Syntax needs to be OK.
315313
if (!PATTERN_MAPCODE.matcher(decodedMapcode).matches()) {
316-
return FormatType.INVALID;
314+
throw new UnknownPrecisionFormatException(decodedMapcode + " is not a correctly formatted mapcode code; " +
315+
"the regular expression for the mapcode code syntax is: " + REGEX_MAPCODE);
317316
}
318317

319318
// Precision part should be OK.
320319
final Matcher matcherPrecision = PATTERN_PRECISION.matcher(decodedMapcode);
321320
if (!matcherPrecision.find()) {
322-
return FormatType.PRECISION_0;
321+
return PrecisionFormat.PRECISION_0;
323322
}
324323
final int length = matcherPrecision.end() - matcherPrecision.start();
325324
assert (2 <= length) && (length <= 3);
326325
if (length == 2) {
327-
return FormatType.PRECISION_1;
326+
return PrecisionFormat.PRECISION_1;
328327
}
329-
return FormatType.PRECISION_2;
328+
return PrecisionFormat.PRECISION_2;
330329
}
331330

332331
/**
333332
* This method provides a shortcut to checking if a mapcode string is formatted properly or not at all.
334333
*
335-
* @param mapcode Mapcode (optionally with a territory_).
334+
* @param mapcode Mapcode (optionally with a territory).
336335
* @return True if the mapcode format, the syntax, is correct. This does not mean the mapcode code is
337336
* actually a valid mapcode representing a location on Earth.
338337
* @throws IllegalArgumentException If mapcode is null.
339338
*/
340-
public static boolean isValidMapcodeFormat(@Nonnull final String mapcode) throws IllegalArgumentException {
339+
public static boolean isValidPrecisionFormat(@Nonnull final String mapcode) throws IllegalArgumentException {
341340
checkNonnull("mapcode", mapcode);
342-
return getMapcodeFormatType(mapcode.toUpperCase()) != FormatType.INVALID;
341+
try {
342+
// Throws an exception if the format is incorrect.
343+
getPrecisionFormat(mapcode.toUpperCase());
344+
return true;
345+
} catch (final UnknownPrecisionFormatException ignored) {
346+
return false;
347+
}
343348
}
344349

345350
/**
@@ -362,9 +367,12 @@ public static boolean containsTerritory(@Nonnull final String mapcode) throws Il
362367

363368
/**
364369
* Get a safe maximum for the distance between a decoded mapcode and its original
365-
* location used for encoding the mapcode. The actual accuracy (resolution) of mapcodes is slightly
370+
* location used for encoding the mapcode. The actual accuracy (resolution) of mapcodes is
366371
* better than this, but these are safe values to use under normal circumstances.
367372
*
373+
* Do not make any other assumptions on these numbers than that mapcodes are never more off
374+
* by this distance.
375+
*
368376
* @param precision Precision of mapcode.
369377
* @return Maximum offset in meters.
370378
*/
@@ -397,7 +405,7 @@ static String convertStringToPlainAscii(@Nonnull final String string) {
397405
*/
398406
@Nonnull
399407
static String convertStringToAlphabet(@Nonnull final String string, @Nullable final Alphabet alphabet) throws IllegalArgumentException {
400-
return (alphabet != null) ? Decoder.encodeUTF16(string.toUpperCase(), alphabet.getCode()) : string.toUpperCase();
408+
return (alphabet != null) ? Decoder.encodeUTF16(string.toUpperCase(), alphabet.getNumber()) : string.toUpperCase();
401409
}
402410

403411
/**
@@ -431,124 +439,4 @@ public boolean equals(@Nullable final Object obj) {
431439
codePrecision2.equals(that.codePrecision2) &&
432440
(this.territory.equals(that.territory));
433441
}
434-
435-
/**
436-
* ----------------------------------------------------------------------
437-
* Deprecated methods.
438-
* ----------------------------------------------------------------------
439-
*
440-
* Important: these methods will potentially be removed from the interface in later releases.
441-
* It is advised to migrate to the newer variants.
442-
*/
443-
444-
/**
445-
* Deprecated. Replaced with {@link #getCode}.
446-
*
447-
* @return Deprecated.
448-
*/
449-
@Deprecated
450-
@Nonnull
451-
public String getMapcode() {
452-
return getCode();
453-
}
454-
455-
/**
456-
* Deprecated. Replaced with {@link #getCode}.
457-
*
458-
* @param precision Deprecated.
459-
* @return Deprecated.
460-
*/
461-
@Deprecated
462-
@Nonnull
463-
public String getMapcodePrecision(final int precision) {
464-
return getCode(precision);
465-
}
466-
467-
/**
468-
* Deprecated. Replaced with {@link #getCode(int)}.
469-
*
470-
* @return Deprecated.
471-
*/
472-
@Deprecated
473-
@Nonnull
474-
public String getMapcodePrecision0() {
475-
return codePrecision0;
476-
}
477-
478-
/**
479-
* Deprecated. Replaced with {@link #getCode(int)}.
480-
*
481-
* @return Deprecated.
482-
*/
483-
@Deprecated
484-
@Nonnull
485-
public String getMapcodePrecision1() {
486-
return codePrecision1;
487-
}
488-
489-
/**
490-
* Deprecated. Replaced with {@link #getCode(int)}.
491-
*
492-
* @return Deprecated.
493-
*/
494-
@Deprecated
495-
@Nonnull
496-
public String getMapcodeMediumPrecision() {
497-
return codePrecision1;
498-
}
499-
500-
/**
501-
* Deprecated. Replaced with {@link #getCode(int)}.
502-
*
503-
* @return Deprecated.
504-
*/
505-
@Deprecated
506-
@Nonnull
507-
public String getMapcodePrecision2() {
508-
return codePrecision2;
509-
}
510-
511-
/**
512-
* Deprecated. Replaced with {@link #getCode(int)}.
513-
*
514-
* @return Deprecated.
515-
*/
516-
@Deprecated
517-
@Nonnull
518-
public String getMapcodeHighPrecision() {
519-
return codePrecision2;
520-
}
521-
522-
/**
523-
* Deprecated. Replaced with {@link #getCode()}.
524-
*
525-
* @return Deprecated.
526-
*/
527-
@Deprecated
528-
@Nonnull
529-
public String asLocal() {
530-
return getCode();
531-
}
532-
533-
/**
534-
* Deprecated. Replaced with {@link #getCodeWithTerritoryFullname()}.
535-
*
536-
* @return Deprecated.
537-
*/
538-
@Deprecated
539-
@Nonnull
540-
public String asInternationalFullName() {
541-
return getCodeWithTerritoryFullname();
542-
}
543-
544-
/**
545-
* Deprecated. Replaced with {@link #getCodeWithTerritory()}.
546-
*
547-
* @return Deprecated.
548-
*/
549-
@Deprecated
550-
@Nonnull
551-
public String asInternationalISO() {
552-
return getCodeWithTerritory();
553-
}
554442
}

0 commit comments

Comments
 (0)