Skip to content

Commit

Permalink
add check and test for pointonline list less than 2 (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Mace authored Dec 13, 2017
1 parent 98c842b commit a8b22cb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mapbox.core.internal;

import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.support.annotation.RestrictTo;

@RestrictTo(LIBRARY_GROUP)
public final class Preconditions {
public static void checkNotNull(Object value, String message) {
if (value == null) {
throw new NullPointerException(message);
}
}

private Preconditions() {
throw new AssertionError("No instances.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public final class TurfMeasurement {

private TurfMeasurement() {
// Private constructor preventing initialization of this class
throw new AssertionError("No Instances.");
}

/**
Expand Down
20 changes: 16 additions & 4 deletions services-turf/src/main/java/com/mapbox/turf/TurfMisc.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.mapbox.turf;

import android.support.annotation.NonNull;
import static com.mapbox.core.internal.Preconditions.checkNotNull;

import com.mapbox.turf.models.LineIntersectsResult;
import android.support.annotation.NonNull;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.Point;
import com.mapbox.turf.models.LineIntersectsResult;

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

private TurfMisc() {
// Private constructor preventing initialization of this class
throw new AssertionError("No Instances.");
}

/**
Expand All @@ -36,7 +37,10 @@ private TurfMisc() {
* @see <a href="http://turfjs.org/docs/#lineslice">Turf Line slice documentation</a>
* @since 1.2.0
*/
public static LineString lineSlice(Point startPt, Point stopPt, Feature line) {
@NonNull
public static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt,
@NonNull Feature line) {
checkNotNull(line.geometry(), "Feature.geometry() == null");
if (!line.geometry().type().equals("LineString")) {
throw new TurfException("input must be a LineString Feature or Geometry");
}
Expand All @@ -54,6 +58,7 @@ public static LineString lineSlice(Point startPt, Point stopPt, Feature line) {
* @see <a href="http://turfjs.org/docs/#lineslice">Turf Line slice documentation</a>
* @since 1.2.0
*/
@NonNull
public static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt,
@NonNull LineString line) {

Expand Down Expand Up @@ -96,7 +101,14 @@ public static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt
* @return closest point on the line to point
* @since 1.3.0
*/
@NonNull
public static Feature pointOnLine(@NonNull Point pt, @NonNull List<Point> coords) {

if (coords.size() < 2) {
throw new TurfException("Turf pointOnLine requires a List of Points made up of at least 2 "
+ "coordinates.");
}

Feature closestPt = Feature.fromGeometry(
Point.fromLngLat(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));
closestPt.addNumberProperty("dist", Double.POSITIVE_INFINITY);
Expand Down
21 changes: 21 additions & 0 deletions services-turf/src/test/java/com/mapbox/turf/TurfMiscTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.Point;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -57,6 +58,15 @@ public void lineSlice_throwLineMustContainTwoOrMorePoints() throws Exception {
TurfMisc.lineSlice(point, point, lineString);
}

@Test
public void lineSlice_returnsEmptyLineStringRatherThanNull() throws Exception {
List<Point> coords = new ArrayList<>();
coords.add(Point.fromLngLat(1.0, 1.0));
coords.add(Point.fromLngLat(2.0, 2.0));
LineString lineString = LineString.fromLngLats(coords);
assertNotNull(TurfMisc.lineSlice(coords.get(0), coords.get(1), lineString));
}

@Test
public void testTurfLineSliceLine1() throws IOException, TurfException {
Point start = Point.fromLngLat(-97.79617309570312, 22.254624939561698);
Expand Down Expand Up @@ -136,6 +146,17 @@ public void testTurfLineSliceVertical() throws IOException, TurfException {
* Point on line test
*/

@Test
public void pointOnLine_throwLineMustContainTwoOrMorePoints() throws Exception {
thrown.expect(TurfException.class);
thrown.expectMessage(startsWith("Turf pointOnLine requires a List of Points made up of at least"
+ " 2 coordinates."));

List<Point> line = new ArrayList<>();
line.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638));
TurfMisc.pointOnLine(line.get(0), line);
}

@Test
public void testTurfPointOnLineFirstPoint() throws TurfException {
List<Point> line = new ArrayList<>();
Expand Down

0 comments on commit a8b22cb

Please sign in to comment.