Skip to content

Commit 705d09f

Browse files
committed
Fixed code style, Java 8 and deprecated number of redundant toString variants of Mapcode
1 parent 7d2e9e9 commit 705d09f

17 files changed

+124
-70
lines changed

src/main/java/com/mapcode/Common.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* ----------------------------------------------------------------------------------------------
2121
* Package private implementation class. For internal use within the Mapcode implementation only.
2222
* ----------------------------------------------------------------------------------------------
23-
*
23+
* <p/>
2424
* This class contains common data structures and methods used by the Mapcode implementation.
2525
*/
2626
class Common {

src/main/java/com/mapcode/Data.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* ----------------------------------------------------------------------------------------------
2424
* Package private implementation class. For internal use within the Mapcode implementation only.
2525
* ----------------------------------------------------------------------------------------------
26-
*
26+
* <p/>
2727
* This class the data class for Mapcode codex items.
2828
*/
2929
class Data {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* ----------------------------------------------------------------------------------------------
2525
* Package private implementation class. For internal use within the Mapcode implementation only.
2626
* ----------------------------------------------------------------------------------------------
27-
*
27+
* <p/>
2828
* This class contains the module that reads the Mapcode areas into memory and processes them.
2929
*/
3030
class DataAccess {

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@
2424
import java.util.ArrayList;
2525
import java.util.List;
2626

27-
import static com.mapcode.Common.countCityCoordinatesForCountry;
28-
import static com.mapcode.Common.getFirstNamelessRecord;
29-
import static com.mapcode.Common.nc;
30-
import static com.mapcode.Common.xDivider;
31-
import static com.mapcode.Common.xSide;
32-
import static com.mapcode.Common.ySide;
27+
import static com.mapcode.Common.*;
3328

3429
class Encoder {
3530
private static final Logger LOG = LoggerFactory.getLogger(Encoder.class);

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

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
/**
2525
* This class defines a single mapcode encoding result, including the mapcode itself and the
2626
* territory definition.
27-
*
27+
* <p/>
2828
* Note that the constructor will throw an {@link IllegalArgumentException} if the syntax of the mapcode
29-
* is not correct. It does not throw an {@link UnknownMapcodeException}, because the mapcode
29+
* is not correct. It does not throw an {@link com.mapcode.UnknownMapcodeException}, because the mapcode
3030
* is not checked for validity, other than its syntax.
3131
*/
3232
public final class Mapcode {
@@ -63,7 +63,7 @@ public Mapcode(
6363
/**
6464
* Get the Mapcode string (without territory information) with standard precision.
6565
* The returned mapcode does not include the '-' separator and additional digits.
66-
*
66+
* <p/>
6767
* The returned precision is approximately 5 meters. The precision is defined as the maximum distance to the
6868
* (latitude, longitude) pair that encoded to this mapcode, which means the mapcode defines an area of
6969
* approximately 10 x 10 meters (100 m2).
@@ -75,6 +75,24 @@ public String getMapcode() {
7575
return mapcodePrecision0;
7676
}
7777

78+
/**
79+
* Get the Mapcode string (without territory information) with a specified precision.
80+
* The returned mapcode includes a '-' separator and additional digits for precisions 1 and 2.
81+
*
82+
* The precision defines the size of a geographical area a single mapcode covers. This means It also defines
83+
* the maximum distance to the location, a (latitude, longitude) pair, that encoded to this mapcode.
84+
*
85+
* Precision 0 uses an area of approx 10 x 10 meters, which means the original location and the location
86+
* as obtained by decoding the encoded mapcode are no further than 5 meters apart.
87+
*
88+
* Precision 1 uses an area of approx 10 x 10 meters, which means the original location and the location
89+
* as obtained by decoding the encoded mapcode are no further than 5 meters apart.
90+
*
91+
* Precision 2 uses an area of approx 10 x 10 meters, which means the original location and the location
92+
* as obtained by decoding the encoded mapcode are no further than 5 meters apart.
93+
*
94+
* @return Mapcode string.
95+
*/
7896
/**
7997
* Alias for {@link #getMapcode}.
8098
*
@@ -100,6 +118,7 @@ public String getMapcodePrecision(final int precision) {
100118
*
101119
* @return Mapcode string.
102120
*/
121+
@Deprecated
103122
@Nonnull
104123
public String getMapcodePrecision0() {
105124
return mapcodePrecision0;
@@ -109,7 +128,7 @@ public String getMapcodePrecision0() {
109128
* Get the medium-precision mapcode string (without territory information).
110129
* The returned mapcode includes the '-' separator and 1 additional digit, if available.
111130
* If a medium precision code is not available, the regular mapcode is returned.
112-
*
131+
* <p/>
113132
* The returned precision is approximately 1 meter. The precision is defined as the maximum distance to the
114133
* (latitude, longitude) pair that encoded to this mapcode, which means the mapcode defines an area of
115134
* approximately 2 x 2 meters (4 m2).
@@ -134,7 +153,7 @@ public String getMapcodeMediumPrecision() {
134153
* Get the high-precision mapcode string (without territory information).
135154
* The returned mapcode includes the '-' separator and 2 additional digit2, if available.
136155
* If a high precision code is not available, the regular mapcode is returned.
137-
*
156+
* <p/>
138157
* The returned precision is approximately 16 centimeters. The precision is defined as the maximum distance to the
139158
* (latitude, longitude) pair that encoded to this mapcode, which means the mapcode defines an area of
140159
* approximately 32 x 32 centimeters (0.1 m2).
@@ -203,7 +222,7 @@ public enum MapcodeFormatType {
203222
* This method return the mapcode type, given a mapcode string. If the mapcode string has an invalid
204223
* format, {@link MapcodeFormatType#MAPCODE_TYPE_INVALID} is returned. If another value is returned,
205224
* the precision of the mapcode is given.
206-
*
225+
* <p/>
207226
* Note that this method only checks the syntactic validity of the mapcode, the string format. It does not
208227
* check if the mapcode is really a valid mapcode representing a position on Earth.
209228
*
@@ -258,7 +277,7 @@ public static String convertToAscii(@Nonnull final String mapcode) {
258277

259278
/**
260279
* Return the local mapcode string, potentially ambiguous.
261-
*
280+
* <p/>
262281
* Example:
263282
* 49.4V
264283
*
@@ -273,13 +292,19 @@ public String asLocal() {
273292
* Return the full international mapcode, including the full name of the territory and the Mapcode itself.
274293
* The format of the code is:
275294
* full-territory-name mapcode
276-
*
295+
* <p/>
277296
* Example:
278297
* Netherlands 49.4V (regular code)
279298
* Netherlands 49.4V-K2 (high precision code)
280299
*
300+
* @param precision Precision specifier. Range: [0, 2].
281301
* @return Full international mapcode.
282302
*/
303+
@Nonnull
304+
public String asInternationalFullName(final int precision) {
305+
return territory.getFullName() + ' ' + getMapcodePrecision(precision);
306+
}
307+
283308
@Nonnull
284309
public String asInternationalFullName() {
285310
return territory.getFullName() + ' ' + mapcodePrecision0;
@@ -290,18 +315,25 @@ public String asInternationalFullName() {
290315
* International codes use a territory code "AAA".
291316
* The format of the code is:
292317
* short-territory-name mapcode
293-
*
318+
* <p/>
294319
* Example:
295320
* NLD 49.4V (regular code)
296321
* NLD 49.4V-K2 (high-precision code)
297322
*
323+
* @param precision Precision specifier. Range: [0, 2].
298324
* @return Short-hand international mapcode.
299325
*/
326+
@Nonnull
327+
public String asInternationalISO(final int precision) {
328+
return territory.toString() + ' ' + getMapcodePrecision(precision);
329+
}
330+
300331
@Nonnull
301332
public String asInternationalISO() {
302333
return territory.toString() + ' ' + mapcodePrecision0;
303334
}
304335

336+
305337
@Nonnull
306338
@Override
307339
public String toString() {
@@ -310,7 +342,7 @@ public String toString() {
310342

311343
@Override
312344
public int hashCode() {
313-
return Arrays.deepHashCode(new Object[]{mapcodePrecision0, territory});
345+
return Arrays.deepHashCode(new Object[]{mapcodePrecision0, mapcodePrecision1, mapcodePrecision2, territory});
314346
}
315347

316348
@Override
@@ -322,6 +354,9 @@ public boolean equals(final Object obj) {
322354
return false;
323355
}
324356
final Mapcode that = (Mapcode) obj;
325-
return mapcodePrecision0.equals(that.mapcodePrecision0) && (this.territory.equals(that.territory));
357+
return mapcodePrecision0.equals(that.mapcodePrecision0) &&
358+
mapcodePrecision1.equals(that.mapcodePrecision1) &&
359+
mapcodePrecision2.equals(that.mapcodePrecision2) &&
360+
(this.territory.equals(that.territory));
326361
}
327362
}

src/main/java/com/mapcode/MapcodeCodec.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* ----------------------------------------------------------------------------------------------
2727
* Mapcode public interface.
2828
* ----------------------------------------------------------------------------------------------
29-
*
29+
* <p/>
3030
* This class is the external Java interface for encoding and decoding mapcodes.
3131
*/
3232
public final class MapcodeCodec {
@@ -44,13 +44,13 @@ private MapcodeCodec() {
4444
/**
4545
* Encode a lat/lon pair to a mapcode with territory information. This produces a non-empty list of mapcode,
4646
* with at the very least 1 mapcodes for the lat/lon, which is the "International" mapcode.
47-
*
47+
* <p/>
4848
* The returned result list will always contain at least 1 mapcode, because every lat/lon pair can be encoded.
49-
*
49+
* <p/>
5050
* The list is ordered in such a way that the first result contains the shortest mapcode (which is usually a
5151
* local mapcode). The last result contains the "International" or world-wide mapcode, which is always
5252
* unambiguous, even when used without a territory specification.
53-
*
53+
* <p/>
5454
* The international code can be obtained from the list by using: "results.get(results.size() - 1)".
5555
*
5656
* @param latDeg Latitude, accepted range: -90..90.
@@ -75,9 +75,9 @@ public static List<Mapcode> encode(
7575
/**
7676
* Encode a lat/lon pair to a mapcode with territory information, for a specific territory. This produces a
7777
* potentially empty list of mapcodes (empty if the lat/lon does not fall within the territory for mapcodes).
78-
*
78+
* <p/>
7979
* The returned result list will always contain at least 1 mapcode, because every lat/lon pair can be encoded.
80-
*
80+
* <p/>
8181
* The list is ordered in such a way that the first result contains the shortest mapcode (which is usually a
8282
* local mapcode).
8383
*
@@ -191,7 +191,7 @@ public static Mapcode encodeToInternational(
191191
/**
192192
* Decode a mapcode to a Point. The decoding process may fail for local mapcodes,
193193
* because no territory context is supplied (world-wide).
194-
*
194+
* <p/>
195195
* The accepted format is:
196196
* {mapcode}
197197
* {territory-code} {mapcode}
@@ -231,7 +231,7 @@ public static Point decode(
231231

232232
/**
233233
* Decode a mapcode to a Point. A reference territory is supplied for disambiguation (only used if applicable).
234-
*
234+
* <p/>
235235
* The accepted format is:
236236
* {mapcode} (note that a territory code is not allowed here)
237237
*

src/main/java/com/mapcode/ParentTerritory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* ----------------------------------------------------------------------------------------------
2323
* Mapcode public interface.
2424
* ----------------------------------------------------------------------------------------------
25-
*
25+
* <p/>
2626
* This class defines "parent territories" for territories that have multiple territory codes.
2727
*/
2828
public enum ParentTerritory {

src/main/java/com/mapcode/Point.java

Lines changed: 29 additions & 7 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 java.util.Arrays;
2021
import java.util.Random;
2122

2223
import static com.mapcode.CheckArgs.checkNonnull;
@@ -25,7 +26,7 @@
2526
* ----------------------------------------------------------------------------------------------
2627
* Package private implementation class. For internal use within the mapcode implementation only.
2728
* ----------------------------------------------------------------------------------------------
28-
*
29+
* <p/>
2930
* This class defines a class for lat/lon points.
3031
*/
3132
public class Point {
@@ -119,12 +120,6 @@ public double getLonDeg() {
119120
return lonDeg;
120121
}
121122

122-
@Override
123-
@Nonnull
124-
public String toString() {
125-
return "(" + latDeg + ", " + lonDeg + ')';
126-
}
127-
128123
public static int degToMicroDeg(final double deg) {
129124
//noinspection NumericCastThatLosesPrecision
130125
return (int) Math.round(deg * MICRODEG_TO_DEG_FACTOR);
@@ -292,4 +287,31 @@ void setUndefined() {
292287
boolean isDefined() {
293288
return defined;
294289
}
290+
291+
@Nonnull
292+
@Override
293+
public String toString() {
294+
return defined ? ("(" + latDeg + ", " + lonDeg + ')') : "undefined";
295+
}
296+
297+
@SuppressWarnings("NonFinalFieldReferencedInHashCode")
298+
@Override
299+
public int hashCode() {
300+
return Arrays.hashCode(new Object[]{latDeg, lonDeg, defined});
301+
}
302+
303+
@SuppressWarnings("NonFinalFieldReferenceInEquals")
304+
@Override
305+
public boolean equals(final Object obj) {
306+
if (this == obj) {
307+
return true;
308+
}
309+
if (!(obj instanceof Point)) {
310+
return false;
311+
}
312+
final Point that = (Point) obj;
313+
return (Double.compare(this.latDeg, that.latDeg) == 0) &&
314+
(Double.compare(this.lonDeg, that.lonDeg) == 0) &&
315+
(this.defined == that.defined);
316+
}
295317
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* ----------------------------------------------------------------------------------------------
2525
* Package private implementation class. For internal use within the Mapcode implementation only.
2626
* ----------------------------------------------------------------------------------------------
27-
*
27+
* <p/>
2828
* This class contains a class for dealing with ranges of comparable items.
2929
*/
3030
class Range<T extends Comparable<T>> {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@
2121

2222
import javax.annotation.Nonnull;
2323
import javax.annotation.Nullable;
24-
import java.util.ArrayList;
25-
import java.util.Collections;
26-
import java.util.List;
24+
import java.util.*;
2725
import java.util.Map.Entry;
28-
import java.util.SortedMap;
29-
import java.util.TreeMap;
3026

3127
/**
3228
* ----------------------------------------------------------------------------------------------
3329
* Package private implementation class. For internal use within the mapcode implementation only.
3430
* ----------------------------------------------------------------------------------------------
35-
*
31+
* <p/>
3632
* This class contains a class that defines an area for local mapcodes.
3733
*/
3834
class SubArea {

0 commit comments

Comments
 (0)