Skip to content

Commit 24d07c9

Browse files
committed
Changed mapcode interface for Alphabet
1 parent 1540af5 commit 24d07c9

File tree

1 file changed

+85
-40
lines changed

1 file changed

+85
-40
lines changed

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

Lines changed: 85 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mapcode;
1818

1919
import javax.annotation.Nonnull;
20+
import javax.annotation.Nullable;
2021
import java.util.Arrays;
2122
import java.util.regex.Matcher;
2223
import java.util.regex.Pattern;
@@ -82,11 +83,18 @@ public Mapcode(
8283
* (latitude, longitude) pair that encoded to this mapcode, which means the mapcode defines an area of
8384
* approximately 10 x 10 meters (100 m2).
8485
*
86+
* @param alphabet Alphabet.
8587
* @return Mapcode string.
8688
*/
8789
@Nonnull
90+
public String getMapcode(@Nullable final Alphabet alphabet) {
91+
return convertToAlphabet(mapcodePrecision0, alphabet);
92+
}
93+
94+
// Convencience method.
95+
@Nonnull
8896
public String getMapcode() {
89-
return mapcodePrecision0;
97+
return convertToAlphabet(mapcodePrecision0, null);
9098
}
9199

92100
/**
@@ -104,28 +112,29 @@ public String getMapcode() {
104112
*
105113
* The accuracy is slightly better than the figures above, but these figures are safe assumptions.
106114
*
107-
* @return Mapcode string.
108-
*/
109-
/**
110-
* Alias for {@link #getMapcode}.
111-
*
112115
* @param precision Precision. Range: 0..2.
113116
* @return Mapcode string.
114117
*/
115118
@Nonnull
116-
public String getMapcodePrecision(final int precision) {
119+
public String getMapcodePrecision(final int precision, @Nullable final Alphabet alphabet) {
117120
switch (precision) {
118121
case 0:
119-
return mapcodePrecision0;
122+
return convertToAlphabet(mapcodePrecision0, alphabet);
120123
case 1:
121-
return mapcodePrecision1;
124+
return convertToAlphabet(mapcodePrecision1, alphabet);
122125
case 2:
123-
return mapcodePrecision2;
126+
return convertToAlphabet(mapcodePrecision2, alphabet);
124127
default:
125128
throw new IllegalArgumentException("getMapcodePrecision: precision must be in [0..2]");
126129
}
127130
}
128131

132+
// Convencience method.
133+
@Nonnull
134+
public String getMapcodePrecision(final int precision) {
135+
return getMapcodePrecision(precision, null);
136+
}
137+
129138
// Deprecated alias for getMapcodePrecision().
130139
@Deprecated
131140
@Nonnull
@@ -254,42 +263,24 @@ public static boolean isValidMapcodeFormat(@Nonnull final String mapcode) {
254263
return getMapcodeFormatType(mapcode.toUpperCase()) != MapcodeFormatType.MAPCODE_TYPE_INVALID;
255264
}
256265

257-
/**
258-
* Convert a mapcode which potentially contains Unicode characters, to an ASCII variant.
259-
*
260-
* @param mapcode Mapcode, with optional Unicode characters.
261-
* @return ASCII, non-Unicode string.
262-
*/
263-
@Nonnull
264-
public static String convertToAscii(@Nonnull final String mapcode) {
265-
// Cannot call: checkMapcode() - recursive.
266-
return Decoder.decodeUTF16(mapcode.toUpperCase());
267-
}
268-
269-
/**
270-
* Convert a mapcode into the same mapcode using a different (or the same) alphabet.
271-
*
272-
* @param mapcode Mapcode to be converted.
273-
* @param alphabet Alphabet to convert to, may contain Unicode characters.
274-
* @return Converted mapcode.
275-
*/
276-
@Nonnull
277-
public static String convertToAlphabet(@Nonnull final String mapcode, @Nonnull final Alphabet alphabet) {
278-
checkMapcode("mapcode", mapcode);
279-
return Decoder.encodeToAlphabetCode(mapcode.toUpperCase(), alphabet.code);
280-
}
281-
282266
/**
283267
* Return the local mapcode string, potentially ambiguous.
284268
*
285269
* Example:
286270
* 49.4V
287271
*
272+
* @param alphabet Alphabet.
288273
* @return Local mapcode.
289274
*/
290275
@Nonnull
276+
public String asLocal(@Nullable final Alphabet alphabet) {
277+
return getMapcode(alphabet);
278+
}
279+
280+
// Convenience method.
281+
@Nonnull
291282
public String asLocal() {
292-
return mapcodePrecision0;
283+
return asLocal(null);
293284
}
294285

295286
/**
@@ -302,16 +293,30 @@ public String asLocal() {
302293
* Netherlands 49.4V-K2 (high precision code)
303294
*
304295
* @param precision Precision specifier. Range: [0, 2].
296+
* @param alphabet Alphabet.
305297
* @return Full international mapcode.
306298
*/
307299
@Nonnull
300+
public String asInternationalFullName(final int precision, @Nullable final Alphabet alphabet) {
301+
return territory.getFullName() + ' ' + getMapcodePrecision(precision, alphabet);
302+
}
303+
304+
// Convenience method.
305+
@Nonnull
308306
public String asInternationalFullName(final int precision) {
309-
return territory.getFullName() + ' ' + getMapcodePrecision(precision);
307+
return asInternationalFullName(precision, null);
310308
}
311309

310+
// Convenience method.
311+
@Nonnull
312+
public String asInternationalFullName(@Nonnull final Alphabet alphabet) {
313+
return asInternationalFullName(0, alphabet);
314+
}
315+
316+
// Convenience method.
312317
@Nonnull
313318
public String asInternationalFullName() {
314-
return territory.getFullName() + ' ' + mapcodePrecision0;
319+
return asInternationalFullName(0, null);
315320
}
316321

317322
/**
@@ -325,18 +330,58 @@ public String asInternationalFullName() {
325330
* NLD 49.4V-K2 (high-precision code)
326331
*
327332
* @param precision Precision specifier. Range: [0, 2].
333+
* @param alphabet Alphabet.
328334
* @return Short-hand international mapcode.
329335
*/
330336
@Nonnull
337+
public String asInternationalISO(final int precision, @Nullable final Alphabet alphabet) {
338+
return territory.toString() + ' ' + getMapcodePrecision(precision, alphabet);
339+
}
340+
341+
// Convenience method.
342+
@Nonnull
331343
public String asInternationalISO(final int precision) {
332-
return territory.toString() + ' ' + getMapcodePrecision(precision);
344+
return asInternationalISO(precision, null);
345+
}
346+
347+
// Convenience method.
348+
@Nonnull
349+
public String asInternationalISO(@Nonnull final Alphabet alphabet) {
350+
return asInternationalISO(0, alphabet);
333351
}
334352

353+
// Convenience method.
335354
@Nonnull
336355
public String asInternationalISO() {
337-
return territory.toString() + ' ' + mapcodePrecision0;
356+
return asInternationalISO(0, null);
357+
}
358+
359+
// TODO !! Is this still needed?
360+
361+
/**
362+
* Convert a mapcode which potentially contains Unicode characters, to an ASCII variant.
363+
*
364+
* @param mapcode Mapcode, with optional Unicode characters.
365+
* @return ASCII, non-Unicode string.
366+
*/
367+
@Nonnull
368+
private static String convertToAscii(@Nonnull final String mapcode) {
369+
// Cannot call: checkMapcode() - recursive.
370+
return Decoder.decodeUTF16(mapcode.toUpperCase());
338371
}
339372

373+
/**
374+
* Convert a mapcode into the same mapcode using a different (or the same) alphabet.
375+
*
376+
* @param mapcode Mapcode to be converted.
377+
* @param alphabet Alphabet to convert to, may contain Unicode characters.
378+
* @return Converted mapcode.
379+
*/
380+
@Nonnull
381+
private static String convertToAlphabet(@Nonnull final String mapcode, @Nullable final Alphabet alphabet) {
382+
checkMapcode("mapcode", mapcode);
383+
return (alphabet != null) ? Decoder.encodeToAlphabetCode(mapcode.toUpperCase(), alphabet.code) : mapcode.toUpperCase();
384+
}
340385

341386
@Nonnull
342387
@Override

0 commit comments

Comments
 (0)