Skip to content

Commit 0cbdb45

Browse files
committed
Merge pull request #11 from mapcode-foundation/dev-1.50
Move dev-1.50 to master
2 parents d774934 + 6f69c4a commit 0cbdb45

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+44594
-76946
lines changed

NOTICE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ MAPCODE JAVA LIBRARY
33
-------------------------------------------------------------------------------
44

55
Original C library created by Pieter Geelen. Work on Java version
6-
of the Mapcode library by Rijn Buve and Matthew Lowden.
6+
of the Mapcode library by Rijn Buve (original port by Matthew Lowden).
77

88
Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ and to decode mapcodes back to latitude/longitude pairs.
1313

1414
**An example of how to use this library can be found at: https://github.com/mapcode-foundation/mapcode-java-example**
1515

16+
If you wish to use mapcodes in your own application landscape, consider using running an instance of the
17+
Mapcode REST API, which can be found on: **https://github.com/mapcode-foundation/mapcode-rest-service**
18+
1619
# License
1720

1821
Licensed under the Apache License, Version 2.0 (the "License");

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<artifactId>mapcode</artifactId>
99

1010
<packaging>jar</packaging>
11-
<version>1.42.3-SNAPSHOT</version>
11+
<version>1.50.0-SNAPSHOT</version>
1212

1313
<name>Mapcode Java Library</name>
1414
<description>
@@ -45,7 +45,7 @@
4545
<name>Matthew Lowden</name>
4646
<organization>Mapcode</organization>
4747
<roles>
48-
<role>Developer</role>
48+
<role>Original Port</role>
4949
</roles>
5050
</developer>
5151
</developers>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mapcode;
18+
19+
import javax.annotation.Nonnull;
20+
21+
import static com.mapcode.CheckArgs.checkNonnull;
22+
23+
/**
24+
* This enum defines all alphabets supported for mapcodes. Mapcodes can be safely converted between
25+
* alphabets and fed to the mapcode decoder in the regular ASCII Roman alphabet or any other.
26+
*/
27+
public enum Alphabet {
28+
ROMAN(0),
29+
GREEK(1),
30+
CYRILLIC(2),
31+
HEBREW(3),
32+
HINDI(4),
33+
MALAY(5),
34+
GEORGIAN(6),
35+
KATAKANA(7),
36+
THAI(8),
37+
LAO(9),
38+
ARMENIAN(10),
39+
BENGALI(11),
40+
GURMUKHI(12),
41+
TIBETAN(13);
42+
43+
/**
44+
* The numeric code is synonym for the alphanumeric code. It can be used in the decoder
45+
* to define a territory as well.
46+
*/
47+
private final int code;
48+
49+
private Alphabet(final int code) {
50+
this.code = code;
51+
}
52+
53+
public int getCode() {
54+
return code;
55+
}
56+
57+
/**
58+
* Get an alphabet from a numeric code.
59+
*
60+
* @param code Numeric code.
61+
* @return Alphabet.
62+
* @throws UnknownAlphabetException Thrown if code out of range.
63+
*/
64+
@Nonnull
65+
public static Alphabet fromCode(final int code) throws UnknownAlphabetException {
66+
if ((code >= 0) && (code < Alphabet.values().length)) {
67+
return Alphabet.values()[code];
68+
}
69+
throw new UnknownAlphabetException(code);
70+
}
71+
72+
/**
73+
* Return alphabet from a string, which can be a numeric or alpha code.
74+
*
75+
* @param numericOrAlpha Alphabet. May be a numeric or alphanumeric code.
76+
* @return Alphabet.
77+
* @throws UnknownAlphabetException Thrown if incorrect numeric or alphanumeric code.
78+
*/
79+
@Nonnull
80+
public static Alphabet fromString(@Nonnull final String numericOrAlpha) throws UnknownAlphabetException {
81+
checkNonnull("name", numericOrAlpha);
82+
final String trimmed = numericOrAlpha.trim().toUpperCase();
83+
try {
84+
return fromCode(Integer.valueOf(numericOrAlpha));
85+
} catch (final IllegalArgumentException ignored) {
86+
// Ignore. Re-try as alpha code.
87+
}
88+
try {
89+
return valueOf(trimmed);
90+
} catch (final IllegalArgumentException ignored) {
91+
throw new UnknownAlphabetException(trimmed);
92+
}
93+
}
94+
95+
/**
96+
* Static checking of the static data structures.
97+
*/
98+
static {
99+
int i = 0;
100+
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);
103+
}
104+
++i;
105+
}
106+
}
107+
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import javax.annotation.Nonnull;
2020
import javax.annotation.Nullable;
2121

22+
import static com.mapcode.Mapcode.isValidMapcodeFormat;
23+
2224
/**
23-
* Package private helper methods.
25+
* Package private helper methods to check arguments for validity.
2426
*/
2527
class CheckArgs {
2628

@@ -42,4 +44,13 @@ static void checkNonnull(@Nonnull final String param, @Nullable final Object obj
4244
throw new IllegalArgumentException("Parameter " + param + " should not be null");
4345
}
4446
}
47+
48+
static void checkMapcodeCode(@Nonnull final String param, @Nullable final String code)
49+
throws IllegalArgumentException {
50+
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+
}
55+
}
4556
}

src/main/java/com/mapcode/DataAccess.java

100644100755
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class DataAccess {
3434

3535
// Read data only once in static initializer.
3636
static {
37-
boolean initialized = false;
3837
final InputStream inputStream = DataAccess.class.getResourceAsStream(FILE_NAME);
3938
try {
4039
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@@ -45,9 +44,9 @@ class DataAccess {
4544
FILE_DATA = outputStream.toByteArray();
4645
inputStream.close();
4746
outputStream.close();
48-
initialized = true;
4947
} catch (final IOException e) {
50-
throw new ExceptionInInitializerError(e);
48+
throw new ExceptionInInitializerError("Cannot initialize static data structure from: " + FILE_NAME +
49+
", exception=" + e);
5150
} finally {
5251
try {
5352
if (inputStream != null) {
@@ -57,9 +56,6 @@ class DataAccess {
5756
// Ignore.
5857
}
5958
}
60-
if (!initialized) {
61-
throw new IllegalArgumentException("Cannot initialize static data structure from: " + FILE_NAME);
62-
}
6359
}
6460

6561
private DataAccess() {
@@ -143,8 +139,7 @@ static int smartDiv(final int i) {
143139
15477, 15493, 15530, 15547, 15571, 15594, 15611, 15631, 15657, 15683,
144140
15702, 15719, 15772, 15813, 15835, 15862, 15882, 15903, 15921, 15951,
145141
15977, 15997, 16018, 16036, 16054, 16073, 16089, 16105, 16133, 16159,
146-
16166, 16168, 16169, 16171, 16172, 16174, 16176, 16178, 16180, 16182,
147-
16183, 16184, 16216};
142+
16166, 16168, 16169, 16201};
148143

149144
// / low-level routines for data access
150145
static int dataFirstRecord(final int ccode) {

0 commit comments

Comments
 (0)