Skip to content

Commit ac0167c

Browse files
committed
Merge pull request #18 from mapcode-foundation/dev
Release 2.0.1
2 parents ff44d32 + c69e912 commit ac0167c

File tree

12 files changed

+287
-240
lines changed

12 files changed

+287
-240
lines changed

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>2.0.1-SNAPSHOT</version>
11+
<version>2.0.1</version>
1212

1313
<name>Mapcode Java Library</name>
1414
<description>
@@ -68,7 +68,7 @@
6868
<properties>
6969
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
7070
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
71-
<jdk.version>1.8</jdk.version>
71+
<jdk.version>1.6</jdk.version>
7272

7373
<gson.version>2.3.1</gson.version>
7474
<jsr305.version>3.0.0</jsr305.version>

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,31 @@ class DataAccess {
4242
final int bufferSize = 100000;
4343
final byte[] readBuffer = new byte[bufferSize];
4444
int total = 0;
45-
try (final InputStream inputStream = DataAccess.class.getResourceAsStream(FILE_NAME)) {
46-
try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
47-
int nrBytes = inputStream.read(readBuffer);
48-
while (nrBytes >= 0) {
49-
total += nrBytes;
50-
outputStream.write(readBuffer, 0, nrBytes);
51-
nrBytes = inputStream.read(readBuffer);
52-
}
53-
54-
// Copy stream as unsigned bytes (ints).
55-
final byte[] bytes = outputStream.toByteArray();
56-
assert total == bytes.length;
57-
FILE_DATA = new int[total];
58-
for (int i = 0; i < total; ++i) {
59-
FILE_DATA[i] = (bytes[i] < 0) ? (bytes[i] + 256) : bytes[i];
60-
45+
try {
46+
final InputStream inputStream = DataAccess.class.getResourceAsStream(FILE_NAME);
47+
try {
48+
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
49+
try {
50+
int nrBytes = inputStream.read(readBuffer);
51+
while (nrBytes >= 0) {
52+
total += nrBytes;
53+
outputStream.write(readBuffer, 0, nrBytes);
54+
nrBytes = inputStream.read(readBuffer);
55+
}
56+
57+
// Copy stream as unsigned bytes (ints).
58+
final byte[] bytes = outputStream.toByteArray();
59+
assert total == bytes.length;
60+
FILE_DATA = new int[total];
61+
for (int i = 0; i < total; ++i) {
62+
FILE_DATA[i] = (bytes[i] < 0) ? (bytes[i] + 256) : bytes[i];
63+
64+
}
65+
} finally {
66+
outputStream.close();
6167
}
68+
} finally {
69+
inputStream.close();
6270
}
6371
} catch (final IOException e) {
6472
throw new ExceptionInInitializerError("Cannot initialize static data structure from: " +

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private static List<Mapcode> encode(final double argLatDeg, final double argLonD
7171

7272
final Point pointToEncode = Point.fromDeg(latDeg, lonDeg);
7373
final List<SubArea> areas = SubArea.getAreasForPoint(pointToEncode);
74-
final List<Mapcode> results = new ArrayList<>();
74+
final List<Mapcode> results = new ArrayList<Mapcode>();
7575

7676
int lastbasesubareaID = -1;
7777

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ public Territory getTerritory() {
246246
* provided as statics to only compile these patterns once.
247247
*/
248248
@Nonnull
249-
static final String REGEX_TERRITORY = "[\\p{Alpha}\\p{Digit}]{2,3}+([-_][\\p{Alpha}\\p{Digit}]{2,3}+)?";
249+
static final String REGEX_TERRITORY = "[\\p{L}\\p{N}]{2,3}+([-_][\\p{L}\\p{N}]{2,3}+)?";
250250
@Nonnull
251-
static final String REGEX_CODE_PART = "[\\p{Alpha}\\p{Digit}]{2,5}+";
251+
static final String REGEX_CODE_PART = "[\\p{L}\\p{N}]{2,5}+";
252252
@Nonnull
253-
static final String REGEX_CODE_PRECISION = "[-][\\p{Alpha}\\p{Digit}&&[^zZ]]{1,2}+";
253+
static final String REGEX_CODE_PRECISION = "[-][\\p{L}\\p{N}&&[^zZ]]{1,2}+";
254254

255255
/**
256256
* This patterns/regular expressions is used for checking mapcode format strings.
@@ -261,14 +261,11 @@ public Territory getTerritory() {
261261
REGEX_CODE_PART + "[.]" + REGEX_CODE_PART + '(' + REGEX_CODE_PRECISION + ")?";
262262

263263
@Nonnull
264-
static final Pattern PATTERN_MAPCODE =
265-
Pattern.compile('^' + REGEX_MAPCODE + '$', Pattern.UNICODE_CHARACTER_CLASS);
264+
static final Pattern PATTERN_MAPCODE = Pattern.compile('^' + REGEX_MAPCODE + '$');
266265
@Nonnull
267-
static final Pattern PATTERN_TERRITORY =
268-
Pattern.compile('^' + REGEX_TERRITORY + ' ', Pattern.UNICODE_CHARACTER_CLASS);
266+
static final Pattern PATTERN_TERRITORY = Pattern.compile('^' + REGEX_TERRITORY + ' ');
269267
@Nonnull
270-
static final Pattern PATTERN_PRECISION =
271-
Pattern.compile(REGEX_CODE_PRECISION + '$', Pattern.UNICODE_CHARACTER_CLASS);
268+
static final Pattern PATTERN_PRECISION = Pattern.compile(REGEX_CODE_PRECISION + '$');
272269

273270
/**
274271
* This enum describes the types of available mapcodes (as returned by {@link #getPrecisionFormat(String)}.

src/main/java/com/mapcode/Range.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ Range<T> constrain(@Nonnull final Range<T> constrainingRange) {
8080
return null;
8181
}
8282

83-
return new Range<>(newMin, newMax);
83+
return new Range<T>(newMin, newMax);
8484
}
8585

8686
@Nullable
8787
ArrayList<Range<T>> constrain(@Nonnull final ArrayList<Range<T>> constrainingRanges) {
88-
final ArrayList<Range<T>> resultRanges = new ArrayList<>();
88+
final ArrayList<Range<T>> resultRanges = new ArrayList<Range<T>>();
8989
for (final Range<T> range : constrainingRanges) {
9090
final Range<T> constrainedRange = constrain(range);
9191
if (constrainedRange != null) {

src/main/java/com/mapcode/SubArea.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ class SubArea {
3636

3737
private static final int SUB_AREAS_INITIAL_CAPACITY = 16250;
3838

39-
private static final List<SubArea> SUB_AREAS = new ArrayList<>(SUB_AREAS_INITIAL_CAPACITY);
40-
private static final TreeMap<Integer, ArrayList<SubArea>> LON_MAP = new TreeMap<>();
41-
private static final TreeMap<Integer, ArrayList<SubArea>> LAT_MAP = new TreeMap<>();
39+
private static final List<SubArea> SUB_AREAS = new ArrayList<SubArea>(SUB_AREAS_INITIAL_CAPACITY);
40+
private static final TreeMap<Integer, ArrayList<SubArea>> LON_MAP = new TreeMap<Integer, ArrayList<SubArea>>();
41+
private static final TreeMap<Integer, ArrayList<SubArea>> LAT_MAP = new TreeMap<Integer, ArrayList<SubArea>>();
4242

43-
private static final Range<Integer> LAT_BOUNDING_RANGE = new Range<>(Point.LAT_MICRODEG_MIN, Point.LAT_MICRODEG_MAX);
44-
private static final Range<Integer> LON_BOUNDING_RANGE = new Range<>(Point.LON_MICRODEG_MIN, Point.LON_MICRODEG_MAX);
43+
private static final Range<Integer> LAT_BOUNDING_RANGE = new Range<Integer>(Point.LAT_MICRODEG_MIN, Point.LAT_MICRODEG_MAX);
44+
private static final Range<Integer> LON_BOUNDING_RANGE = new Range<Integer>(Point.LON_MICRODEG_MIN, Point.LON_MICRODEG_MAX);
4545

4646
static {
4747
LOG.info("SubArea: Initialize sub-areas for {} territories", Territory.values().length);
@@ -115,7 +115,7 @@ static SubArea getArea(final int i) {
115115
@SuppressWarnings("unchecked")
116116
@Nonnull
117117
static List<SubArea> getAreasForPoint(@Nonnull final Point point) {
118-
final ArrayList<ArrayList<SubArea>> areaLists = new ArrayList<>();
118+
final ArrayList<ArrayList<SubArea>> areaLists = new ArrayList<ArrayList<SubArea>>();
119119
ArrayList<SubArea> list;
120120
list = LAT_MAP.get(point.getLatMicroDeg());
121121

@@ -158,7 +158,7 @@ static List<SubArea> getAreasForPoint(@Nonnull final Point point) {
158158
areaLists.add(list);
159159
}
160160

161-
final ArrayList<SubArea> result = new ArrayList<>();
161+
final ArrayList<SubArea> result = new ArrayList<SubArea>();
162162
list = areaLists.get(0);
163163

164164
mainLoop:
@@ -208,8 +208,8 @@ private SubArea(final int i, @Nonnull final Territory territory, @Nullable final
208208
minMaxSetup(i);
209209
parentTerritory = territory;
210210
subAreaID = i;
211-
boundedLonRange = new ArrayList<>();
212-
boundedLatRange = new ArrayList<>();
211+
boundedLonRange = new ArrayList<Range<Integer>>();
212+
boundedLatRange = new ArrayList<Range<Integer>>();
213213

214214
// Mapcode areas are inclusive for the minimum bounds and exclusive for the maximum bounds
215215
// Trim max by 1, to address boundary cases.
@@ -246,7 +246,7 @@ private SubArea(final int i, @Nonnull final Territory territory, @Nullable final
246246
@Nonnull
247247
private static ArrayList<Range<Integer>> normaliseRange(
248248
@Nonnull final Range<Integer> range, @Nonnull final Range<Integer> boundingRange) {
249-
final ArrayList<Range<Integer>> ranges = new ArrayList<>();
249+
final ArrayList<Range<Integer>> ranges = new ArrayList<Range<Integer>>();
250250

251251
Range<Integer> tempRange = range.constrain(boundingRange);
252252
if (tempRange != null) {
@@ -255,7 +255,7 @@ private static ArrayList<Range<Integer>> normaliseRange(
255255

256256
Range<Integer> normalisingRange = range;
257257
while (normalisingRange.getMin() < boundingRange.getMin()) {
258-
normalisingRange = new Range<>((normalisingRange.getMin() + boundingRange.getMax())
258+
normalisingRange = new Range<Integer>((normalisingRange.getMin() + boundingRange.getMax())
259259
- boundingRange.getMin(), (normalisingRange.getMax() + boundingRange.getMax())
260260
- boundingRange.getMin());
261261
tempRange = normalisingRange.constrain(boundingRange);
@@ -266,7 +266,7 @@ private static ArrayList<Range<Integer>> normaliseRange(
266266

267267
normalisingRange = range;
268268
while (normalisingRange.getMax() > boundingRange.getMax()) {
269-
normalisingRange = new Range<>((normalisingRange.getMin() - boundingRange.getMax())
269+
normalisingRange = new Range<Integer>((normalisingRange.getMin() - boundingRange.getMax())
270270
+ boundingRange.getMin(), (normalisingRange.getMax() - boundingRange.getMax())
271271
+ boundingRange.getMin());
272272
tempRange = normalisingRange.constrain(boundingRange);
@@ -293,8 +293,8 @@ boolean containsPoint(@Nonnull final Point point) {
293293
@Nonnull
294294
SubArea extendBounds(final int xExtension, final int yExtension) {
295295
final SubArea result = new SubArea();
296-
result.latRange = new Range<>(this.getMinY() - yExtension, getMaxY() + yExtension);
297-
result.lonRange = new Range<>(this.getMinX() - xExtension, getMaxX() + xExtension);
296+
result.latRange = new Range<Integer>(this.getMinY() - yExtension, getMaxY() + yExtension);
297+
result.lonRange = new Range<Integer>(this.getMinX() - xExtension, getMaxX() + xExtension);
298298
return result;
299299
}
300300

@@ -327,11 +327,11 @@ private void minMaxSetup(final int arg) {
327327
i += 4;
328328
final int maxY = DataAccess.asLong(i);
329329

330-
latRange = new Range<>(minY, maxY);
331-
lonRange = new Range<>(minX, maxX);
330+
latRange = new Range<Integer>(minY, maxY);
331+
lonRange = new Range<Integer>(minX, maxX);
332332
}
333333

334334
private static Range<Integer> trimRange(final Range<Integer> range) {
335-
return new Range<>(range.getMin(), range.getMax() - 1);
335+
return new Range<Integer>(range.getMin(), range.getMax() - 1);
336336
}
337337
}

src/main/java/com/mapcode/Territory.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ static Territory fromNumber(final int number) throws UnknownTerritoryException {
646646
}
647647

648648
/**
649-
* Get a territory from a mapcode territory abbreviation. Note that the provided abbreviation is NOT an
649+
* Get a territory from a mapcode territory abbreviation (or a territory name). Note that the provided abbreviation is NOT an
650650
* ISO code: it's a mapcode prefix. As local mapcodes for subdivisions have been optimized to prefer to use 2-character
651651
* subdivisions codes in local codes, subdivisions are preferred over countries in this case.
652652
*
@@ -658,7 +658,7 @@ static Territory fromNumber(final int number) throws UnknownTerritoryException {
658658
*
659659
* Brazilian mapcodes, on the other hand, would be specified as "BRA BDHP.JK39-1D", using the ISO 3 letter code.
660660
*
661-
* @param alphaCode Territory, alphanumeric code.
661+
* @param alphaCode Territory name or alphanumeric code.
662662
* @return Territory.
663663
* @throws UnknownTerritoryException Thrown if incorrect numeric or alphanumeric code.
664664
*/
@@ -822,13 +822,13 @@ private Territory(
822822
*/
823823
static {
824824
final String errorPrefix = "Initializing error: ";
825-
codeList = new ArrayList<>();
826-
nameMap = new HashMap<>();
827-
parentList = new ArrayList<>();
825+
codeList = new ArrayList<Territory>();
826+
nameMap = new HashMap<String, List<Territory>>();
827+
parentList = new ArrayList<Territory>();
828828
int min = Integer.MAX_VALUE;
829829
int max = Integer.MIN_VALUE;
830-
final Set<Integer> territoryCodes = new HashSet<>();
831-
final Set<String> aliasesSet = new HashSet<>();
830+
final Set<Integer> territoryCodes = new HashSet<Integer>();
831+
final Set<String> namesSet = new HashSet<String>();
832832

833833
for (final Territory territory : Territory.values()) {
834834
final int territoryNumber = territory.getNumber();
@@ -849,14 +849,33 @@ private Territory(
849849
if ((territory.parentTerritory != null) && !parentList.contains(territory.parentTerritory)) {
850850
parentList.add(territory.parentTerritory);
851851
}
852+
853+
// Add territory codes and alias codes.
854+
if (namesSet.contains(territory.toString())) {
855+
throw new ExceptionInInitializerError(errorPrefix + "non-unique territory: " + territory.toString());
856+
}
857+
namesSet.add(territory.toString());
852858
addNameWithParentVariants(territory.toString(), territory);
853859
for (final String alias : territory.aliases) {
854-
if (aliasesSet.contains(alias)) {
860+
if (namesSet.contains(alias)) {
855861
throw new ExceptionInInitializerError(errorPrefix + "non-unique alias: " + alias);
856862
}
857-
aliasesSet.add(alias);
863+
namesSet.add(alias);
858864
addNameWithParentVariants(alias, territory);
859865
}
866+
867+
// Add territory fullnames and aliases as well. Skip special case: territory name == territory code (e.g. USA).
868+
if (namesSet.contains(territory.fullName.toUpperCase()) && !territory.toString().equals(territory.fullName.toUpperCase())) {
869+
throw new ExceptionInInitializerError(errorPrefix + "non-unique fullName: " + territory.fullName.toUpperCase());
870+
}
871+
addNameWithParentVariants(territory.fullName.toUpperCase(), territory);
872+
for (final String fullNameAlias : territory.fullNameAliases) {
873+
if (namesSet.contains(fullNameAlias.toUpperCase())) {
874+
throw new ExceptionInInitializerError(errorPrefix + "non-unique fullName alias: " + fullNameAlias);
875+
}
876+
namesSet.add(fullNameAlias.toUpperCase());
877+
addNameWithParentVariants(fullNameAlias.toUpperCase(), territory);
878+
}
860879
min = Math.min(min, territory.number);
861880
max = Math.max(max, territory.number);
862881
}
@@ -986,7 +1005,7 @@ private static void addName(@Nonnull final String name, @Nonnull final Territory
9861005
}
9871006
territories.add(territory);
9881007
} else {
989-
final ArrayList<Territory> arrayList = new ArrayList<>();
1008+
final ArrayList<Territory> arrayList = new ArrayList<Territory>();
9901009
arrayList.add(territory);
9911010
nameMap.put(name, arrayList);
9921011
}

src/site/apt/ReleaseNotes.apt.vm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ Release Notes (Version ${project.version})
1111

1212
* 2.0.1
1313

14+
* Reverted Java JDK level to 1.6 (Java 6) from 1.8 (Java 8), so the library can be used on
15+
Android platforms operating at Java 6 as well.
16+
1417
* Use multi-threading for long running test to speed them up (uses all CPU cores now).
1518

19+
* Added the ability to use a country name for <<<Territory.fromString()>>>.
20+
1621
* 2.0.0
1722

1823
* Fixes to the data rectangles (primarily intended for ISO proposal).

0 commit comments

Comments
 (0)