Skip to content

Commit 113a6b4

Browse files
author
Cameron Mace
authored
Adds annotations and test for polylineUtils (#687)
* Adds annotations and test for polylineUtils * sortened string length to less than 100
1 parent 207f9d4 commit 113a6b4

2 files changed

Lines changed: 48 additions & 15 deletions

File tree

services-geojson/src/main/java/com/mapbox/geojson/utils/PolylineUtils.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mapbox.geojson.utils;
22

3+
import android.support.annotation.NonNull;
34
import com.mapbox.geojson.Point;
45

56
import java.util.ArrayList;
@@ -34,7 +35,8 @@ private PolylineUtils() {
3435
* @see <a href="https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java">Part of algorithm came from this source.</a>
3536
* @since 1.0.0
3637
*/
37-
public static List<Point> decode(final String encodedPath, int precision) {
38+
@NonNull
39+
public static List<Point> decode(@NonNull final String encodedPath, int precision) {
3840
int len = encodedPath.length();
3941

4042
// OSRM uses precision=6, the default Polyline spec divides by 1E5, capping at precision=5
@@ -83,7 +85,8 @@ public static List<Point> decode(final String encodedPath, int precision) {
8385
* @return a String representing a path string
8486
* @since 1.0.0
8587
*/
86-
public static String encode(final List<Point> path, int precision) {
88+
@NonNull
89+
public static String encode(@NonNull final List<Point> path, int precision) {
8790
long lastLat = 0;
8891
long lastLng = 0;
8992

@@ -131,7 +134,8 @@ private static void encode(long variable, StringBuilder result) {
131134
* @see <a href="http://mourner.github.io/simplify-js/">JavaScript implementation</a>
132135
* @since 1.2.0
133136
*/
134-
public static List<Point> simplify(List<Point> points) {
137+
@NonNull
138+
public static List<Point> simplify(@NonNull List<Point> points) {
135139
return simplify(points, SIMPLIFY_DEFAULT_TOLERANCE, SIMPLIFY_DEFAULT_HIGHEST_QUALITY);
136140
}
137141

@@ -146,7 +150,8 @@ public static List<Point> simplify(List<Point> points) {
146150
* @see <a href="http://mourner.github.io/simplify-js/">JavaScript implementation</a>
147151
* @since 1.2.0
148152
*/
149-
public static List<Point> simplify(List<Point> points, double tolerance) {
153+
@NonNull
154+
public static List<Point> simplify(@NonNull List<Point> points, double tolerance) {
150155
return simplify(points, tolerance, SIMPLIFY_DEFAULT_HIGHEST_QUALITY);
151156
}
152157

@@ -161,7 +166,8 @@ public static List<Point> simplify(List<Point> points, double tolerance) {
161166
* @see <a href="http://mourner.github.io/simplify-js/">JavaScript implementation</a>
162167
* @since 1.2.0
163168
*/
164-
public static List<Point> simplify(List<Point> points, boolean highestQuality) {
169+
@NonNull
170+
public static List<Point> simplify(@NonNull List<Point> points, boolean highestQuality) {
165171
return simplify(points, SIMPLIFY_DEFAULT_TOLERANCE, highestQuality);
166172
}
167173

@@ -178,7 +184,9 @@ public static List<Point> simplify(List<Point> points, boolean highestQuality) {
178184
* @see <a href="http://mourner.github.io/simplify-js/">JavaScript implementation</a>
179185
* @since 1.2.0
180186
*/
181-
public static List<Point> simplify(List<Point> points, double tolerance, boolean highestQuality) {
187+
@NonNull
188+
public static List<Point> simplify(@NonNull List<Point> points, double tolerance,
189+
boolean highestQuality) {
182190
if (points.size() <= 2) {
183191
return points;
184192
}
Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
package com.mapbox.geojson.utils;
22

3-
import com.mapbox.core.constants.Constants;
3+
import static com.mapbox.geojson.utils.PolylineUtils.decode;
4+
import static com.mapbox.geojson.utils.PolylineUtils.encode;
5+
import static com.mapbox.geojson.utils.PolylineUtils.simplify;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNotNull;
8+
49
import com.mapbox.core.TestUtils;
10+
import com.mapbox.core.constants.Constants;
511
import com.mapbox.geojson.Point;
6-
7-
import org.junit.Assert;
812
import org.junit.Test;
913

14+
import java.util.ArrayList;
1015
import java.util.List;
1116

1217
public class PolylineUtilsTest extends TestUtils {
1318

14-
private static final String TEST_LINE = "_cqeFf~cjVf@p@fA}AtAoB`ArAx@hA`GbIvDiFv@gAh@t@X\\|@z@`@Z\\Xf@Vf@VpA\\tATJ@NBBkC";
19+
private static final String TEST_LINE
20+
= "_cqeFf~cjVf@p@fA}AtAoB`ArAx@hA`GbIvDiFv@gAh@t@X\\|@z@`@Z\\Xf@Vf@VpA\\tATJ@NBBkC";
1521

1622
@Test
1723
public void testDecodePath() {
18-
List<Point> latLngs = PolylineUtils.decode(TEST_LINE, Constants.PRECISION_5);
24+
List<Point> latLngs = decode(TEST_LINE, Constants.PRECISION_5);
1925

2026
int expectedLength = 21;
21-
Assert.assertEquals("Wrong length.", expectedLength, latLngs.size());
27+
assertEquals("Wrong length.", expectedLength, latLngs.size());
2228

2329
Point lastPoint = latLngs.get(expectedLength - 1);
2430
expectNearNumber(37.76953, lastPoint.latitude(), 1e-6);
@@ -27,8 +33,27 @@ public void testDecodePath() {
2733

2834
@Test
2935
public void testEncodePath() {
30-
List<Point> path = PolylineUtils.decode(TEST_LINE, Constants.PRECISION_5);
31-
String encoded = PolylineUtils.encode(path, Constants.PRECISION_5);
32-
Assert.assertEquals(TEST_LINE, encoded);
36+
List<Point> path = decode(TEST_LINE, Constants.PRECISION_5);
37+
String encoded = encode(path, Constants.PRECISION_5);
38+
assertEquals(TEST_LINE, encoded);
39+
}
40+
41+
@Test
42+
public void decode_neverReturnsNullButRatherAnEmptyList() throws Exception {
43+
List<Point> path = decode("", Constants.PRECISION_5);
44+
assertNotNull(path);
45+
assertEquals(0, path.size());
46+
}
47+
48+
@Test
49+
public void encode_neverReturnsNull() throws Exception {
50+
String encodedString = encode(new ArrayList<Point>(), Constants.PRECISION_6);
51+
assertNotNull(encodedString);
52+
}
53+
54+
@Test
55+
public void simplify_neverReturnsNullButRatherAnEmptyList() throws Exception {
56+
List<Point> simplifiedPath = simplify(new ArrayList<Point>(), Constants.PRECISION_6);
57+
assertNotNull(simplifiedPath);
3358
}
3459
}

0 commit comments

Comments
 (0)