@@ -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