forked from mapcode-foundation/mapcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapcode.java
More file actions
300 lines (271 loc) · 10.5 KB
/
Copy pathMapcode.java
File metadata and controls
300 lines (271 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright (C) 2014 Stichting Mapcode Foundation (http://www.mapcode.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mapcode;
import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This class defines a single mapcode encoding result, including the mapcode itself and the
* territory definition.
*
* Note that the constructor will throw an {@link IllegalArgumentException} if the syntax of the mapcode
* is not correct. It does not throw an {@link com.mapcode.UnknownMapcodeException}, because the mapcode
* is not checked for validity, other than its syntax.
*/
public final class Mapcode {
@Nonnull private final String mapcodePrecision0;
@Nonnull private final String mapcodePrecision1;
@Nonnull private final String mapcodePrecision2;
@Nonnull private final Territory territory;
public Mapcode(
@Nonnull final String mapcode,
@Nonnull final Territory territory) throws IllegalArgumentException {
// Check mapcode format.
if (!isValidMapcodeFormat(mapcode)) {
throw new IllegalArgumentException(mapcode + " is not a correctly formatted mapcode; " +
"the regular expression for the mapcode syntax is: " + REGEX_MAPCODE_FORMAT);
}
this.mapcodePrecision2 = mapcode;
if (mapcode.contains("-")) {
this.mapcodePrecision0 = mapcode.substring(0, mapcode.length() - 3);
this.mapcodePrecision1 = mapcode.substring(0, mapcode.length() - 1);
}
else {
this.mapcodePrecision0 = mapcode;
this.mapcodePrecision1 = mapcode;
}
this.territory = territory;
}
/**
* Get the Mapcode string (without territory information) with standard precision.
* The returned mapcode does not include the '-' separator and additional digits.
*
* The returned precision is approximately 5 meters. The precision is defined as the maximum distance to the
* (latitude, longitude) pair that encoded to this mapcode, which means the mapcode defines an area of
* approximately 10 x 10 meters (100 m2).
*
* @return Mapcode string.
*/
@Nonnull
public String getMapcode() {
return mapcodePrecision0;
}
/**
* Alias for {@link #getMapcode}.
*
* @return Mapcode string.
*/
@Nonnull
public String getMapcodePrecision0() {
return mapcodePrecision0;
}
/**
* Get the medium-precision mapcode string (without territory information).
* The returned mapcode includes the '-' separator and 1 additional digit, if available.
* If a medium precision code is not available, the regular mapcode is returned.
*
* The returned precision is approximately 1 meter. The precision is defined as the maximum distance to the
* (latitude, longitude) pair that encoded to this mapcode, which means the mapcode defines an area of
* approximately 2 x 2 meters (4 m2).
*
* @return Medium precision mapcode string.
*/
@Nonnull
public String getMapcodePrecision1() {
return mapcodePrecision1;
}
/**
* Deprecated alias for {@link #getMapcodePrecision1}.
*/
@Deprecated
@Nonnull
public String getMapcodeMediumPrecision() {
return mapcodePrecision1;
}
/**
* Get the high-precision mapcode string (without territory information).
* The returned mapcode includes the '-' separator and 2 additional digit2, if available.
* If a high precision code is not available, the regular mapcode is returned.
*
* The returned precision is approximately 16 centimeters. The precision is defined as the maximum distance to the
* (latitude, longitude) pair that encoded to this mapcode, which means the mapcode defines an area of
* approximately 32 x 32 centimeters (0.1 m2).
*
* @return High precision mapcode string.
*/
@Nonnull
public String getMapcodePrecision2() {
return mapcodePrecision2;
}
/**
* Deprecated alias for {@see #getMapcodePrecision2}.
*/
@Deprecated
@Nonnull
public String getMapcodeHighPrecision() {
return mapcodePrecision2;
}
/**
* Get the territory information.
*
* @return Territory information.
*/
@Nonnull
public Territory getTerritory() {
return territory;
}
/**
* This enum describes the types of mapcodes available.
*/
public enum MapcodeFormatType {
MAPCODE_TYPE_INVALID,
MAPCODE_TYPE_PRECISION_0,
MAPCODE_TYPE_PRECISION_1,
MAPCODE_TYPE_PRECISION_2,
}
/**
* These patterns and matchers are used internally in this module to match mapcodes. They are
* provided as statics to only compile these patterns once.
*/
@Nonnull static final String REGEX_MAPCODE_FORMAT1 = "^[\\p{Alpha}\\p{Digit}]{2,5}+";
@Nonnull static final String REGEX_MAPCODE_FORMAT2 = "[.][\\p{Alpha}\\p{Digit}]{2,5}+";
@Nonnull static final String REGEX_MAPCODE_PRECISION = "[-][\\p{Alpha}\\p{Digit}&&[^zZ]]{1,2}+";
/**
* This patterns/regular expressions is used for checking mapcode format strings.
* They've been made pulkic to allow others to use the correct regular expressions as well.
*/
@Nonnull public static final String REGEX_MAPCODE_FORMAT =
REGEX_MAPCODE_FORMAT1 + REGEX_MAPCODE_FORMAT2 + '(' + REGEX_MAPCODE_PRECISION + ")?$";
@Nonnull private static final Pattern PATTERN_MAPCODE_FORMAT =
Pattern.compile(REGEX_MAPCODE_FORMAT, Pattern.UNICODE_CHARACTER_CLASS);
@Nonnull private static final Pattern PATTERN_MAPCODE_PRECISION =
Pattern.compile(REGEX_MAPCODE_PRECISION, Pattern.UNICODE_CHARACTER_CLASS);
/**
* This method return the mapcode type, given a mapcode string. If the mapcode string has an invalid
* format, {@link MapcodeFormatType#MAPCODE_TYPE_INVALID} is returned. If another value is returned,
* the precision of the mapcode is given.
*
* Note that this method only checks the syntactic validity of the mapcode, the string format. It does not
* check if the mapcode is really a valid mapcode representing a position on Earth.
*
* @param mapcode Mapcode string.
* @return Type of mapcode format, or {@link MapcodeFormatType#MAPCODE_TYPE_INVALID} if not valid.
*/
@Nonnull
public static MapcodeFormatType getMapcodeFormatType(@Nonnull final String mapcode) {
// First, decode to ASCII.
final String decodedMapcode = convertToAscii(mapcode);
// Syntax needs to be OK.
if (!PATTERN_MAPCODE_FORMAT.matcher(decodedMapcode).matches()) {
return MapcodeFormatType.MAPCODE_TYPE_INVALID;
}
// Precision part should be OK.
final Matcher matcherMapcodePrecision = PATTERN_MAPCODE_PRECISION.matcher(decodedMapcode);
if (!matcherMapcodePrecision.find()) {
return MapcodeFormatType.MAPCODE_TYPE_PRECISION_0;
}
final int length = matcherMapcodePrecision.end() - matcherMapcodePrecision.start();
assert (2 <= length) && (length <= 3);
if (length == 2) {
return MapcodeFormatType.MAPCODE_TYPE_PRECISION_1;
}
return MapcodeFormatType.MAPCODE_TYPE_PRECISION_2;
}
/**
* This method provides a shortcut to checking if a mapcode string is formatted properly or not at all.
*
* @param mapcode Mapcode string.
* @return True if the mapcode format, the syntax, is correct. This does not mean the mapcode is actually a valid
* mapcode representing a location on Earth.
*/
public static boolean isValidMapcodeFormat(@Nonnull final String mapcode) {
return getMapcodeFormatType(mapcode) != MapcodeFormatType.MAPCODE_TYPE_INVALID;
}
/**
* Convert a mapcode which potentially contains Unicode characters, to an ASCII veriant.
*
* @param mapcode Mapcode, with optional Unicode characters.
* @return ASCII, non-Unicode string.
*/
@Nonnull
public static String convertToAscii(@Nonnull final String mapcode) {
return Decoder.decodeUTF16(mapcode);
}
/**
* Return the local mapcode string, potentially ambiguous.
*
* Example:
* 49.4V
*
* @return Local mapcode.
*/
@Nonnull
public String asLocal() {
return mapcodePrecision0;
}
/**
* Return the full international mapcode, including the full name of the territory and the Mapcode itself.
* The format of the code is:
* full-territory-name mapcode
*
* Example:
* Netherlands 49.4V (regular code)
* Netherlands 49.4V-K2 (high precision code)
*
* @return Full international mapcode.
*/
@Nonnull
public String asInternationalFullName() {
return territory.getFullName() + ' ' + mapcodePrecision0;
}
/**
* Return the international mapcode as a shorter version using the ISO territory codes where possible.
* International codes use a territory code "AAA".
* The format of the code is:
* short-territory-name mapcode
*
* Example:
* NLD 49.4V (regular code)
* NLD 49.4V-K2 (high-precision code)
*
* @return Short-hand international mapcode.
*/
@Nonnull
public String asInternationalISO() {
return territory.toString() + ' ' + mapcodePrecision0;
}
@Nonnull
@Override
public String toString() {
return asInternationalISO();
}
@Override
public int hashCode() {
return Arrays.deepHashCode(new Object[]{mapcodePrecision0, territory});
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Mapcode)) {
return false;
}
final Mapcode that = (Mapcode) obj;
return mapcodePrecision0.equals(that.mapcodePrecision0) && (this.territory.equals(that.territory));
}
}