Skip to content

Commit a8b22cb

Browse files
author
Cameron Mace
authored
add check and test for pointonline list less than 2 (#689)
1 parent 98c842b commit a8b22cb

4 files changed

Lines changed: 56 additions & 5 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mapbox.core.internal;
2+
3+
import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
4+
5+
import android.support.annotation.RestrictTo;
6+
7+
@RestrictTo(LIBRARY_GROUP)
8+
public final class Preconditions {
9+
public static void checkNotNull(Object value, String message) {
10+
if (value == null) {
11+
throw new NullPointerException(message);
12+
}
13+
}
14+
15+
private Preconditions() {
16+
throw new AssertionError("No instances.");
17+
}
18+
}

services-turf/src/main/java/com/mapbox/turf/TurfMeasurement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
public final class TurfMeasurement {
2323

2424
private TurfMeasurement() {
25-
// Private constructor preventing initialization of this class
25+
throw new AssertionError("No Instances.");
2626
}
2727

2828
/**

services-turf/src/main/java/com/mapbox/turf/TurfMisc.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.mapbox.turf;
22

3-
import android.support.annotation.NonNull;
3+
import static com.mapbox.core.internal.Preconditions.checkNotNull;
44

5-
import com.mapbox.turf.models.LineIntersectsResult;
5+
import android.support.annotation.NonNull;
66
import com.mapbox.geojson.Feature;
77
import com.mapbox.geojson.LineString;
88
import com.mapbox.geojson.Point;
9+
import com.mapbox.turf.models.LineIntersectsResult;
910

1011
import java.util.ArrayList;
1112
import java.util.List;
@@ -21,7 +22,7 @@ public final class TurfMisc {
2122
private static final String INDEX_KEY = "index";
2223

2324
private TurfMisc() {
24-
// Private constructor preventing initialization of this class
25+
throw new AssertionError("No Instances.");
2526
}
2627

2728
/**
@@ -36,7 +37,10 @@ private TurfMisc() {
3637
* @see <a href="http://turfjs.org/docs/#lineslice">Turf Line slice documentation</a>
3738
* @since 1.2.0
3839
*/
39-
public static LineString lineSlice(Point startPt, Point stopPt, Feature line) {
40+
@NonNull
41+
public static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt,
42+
@NonNull Feature line) {
43+
checkNotNull(line.geometry(), "Feature.geometry() == null");
4044
if (!line.geometry().type().equals("LineString")) {
4145
throw new TurfException("input must be a LineString Feature or Geometry");
4246
}
@@ -54,6 +58,7 @@ public static LineString lineSlice(Point startPt, Point stopPt, Feature line) {
5458
* @see <a href="http://turfjs.org/docs/#lineslice">Turf Line slice documentation</a>
5559
* @since 1.2.0
5660
*/
61+
@NonNull
5762
public static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt,
5863
@NonNull LineString line) {
5964

@@ -96,7 +101,14 @@ public static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt
96101
* @return closest point on the line to point
97102
* @since 1.3.0
98103
*/
104+
@NonNull
99105
public static Feature pointOnLine(@NonNull Point pt, @NonNull List<Point> coords) {
106+
107+
if (coords.size() < 2) {
108+
throw new TurfException("Turf pointOnLine requires a List of Points made up of at least 2 "
109+
+ "coordinates.");
110+
}
111+
100112
Feature closestPt = Feature.fromGeometry(
101113
Point.fromLngLat(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));
102114
closestPt.addNumberProperty("dist", Double.POSITIVE_INFINITY);

services-turf/src/test/java/com/mapbox/turf/TurfMiscTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.mapbox.geojson.LineString;
66
import com.mapbox.geojson.Point;
77

8+
import org.junit.Assert;
89
import org.junit.Rule;
910
import org.junit.Test;
1011
import org.junit.rules.ExpectedException;
@@ -57,6 +58,15 @@ public void lineSlice_throwLineMustContainTwoOrMorePoints() throws Exception {
5758
TurfMisc.lineSlice(point, point, lineString);
5859
}
5960

61+
@Test
62+
public void lineSlice_returnsEmptyLineStringRatherThanNull() throws Exception {
63+
List<Point> coords = new ArrayList<>();
64+
coords.add(Point.fromLngLat(1.0, 1.0));
65+
coords.add(Point.fromLngLat(2.0, 2.0));
66+
LineString lineString = LineString.fromLngLats(coords);
67+
assertNotNull(TurfMisc.lineSlice(coords.get(0), coords.get(1), lineString));
68+
}
69+
6070
@Test
6171
public void testTurfLineSliceLine1() throws IOException, TurfException {
6272
Point start = Point.fromLngLat(-97.79617309570312, 22.254624939561698);
@@ -136,6 +146,17 @@ public void testTurfLineSliceVertical() throws IOException, TurfException {
136146
* Point on line test
137147
*/
138148

149+
@Test
150+
public void pointOnLine_throwLineMustContainTwoOrMorePoints() throws Exception {
151+
thrown.expect(TurfException.class);
152+
thrown.expectMessage(startsWith("Turf pointOnLine requires a List of Points made up of at least"
153+
+ " 2 coordinates."));
154+
155+
List<Point> line = new ArrayList<>();
156+
line.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638));
157+
TurfMisc.pointOnLine(line.get(0), line);
158+
}
159+
139160
@Test
140161
public void testTurfPointOnLineFirstPoint() throws TurfException {
141162
List<Point> line = new ArrayList<>();

0 commit comments

Comments
 (0)