Skip to content

Commit

Permalink
removed dependency of Turf library on services-core
Browse files Browse the repository at this point in the history
  • Loading branch information
“osana” committed Mar 20, 2019
1 parent 82837f3 commit fea0fc1
Show file tree
Hide file tree
Showing 21 changed files with 296 additions and 79 deletions.
4 changes: 0 additions & 4 deletions services-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ dependencies {
// OkHttp
api dependenciesList.okhttp3Logging

// AutoValue
compileOnly dependenciesList.autoValue
compileOnly dependenciesList.autoValueGson

// Test Dependencies
testOutput sourceSets.test.output
}
1 change: 0 additions & 1 deletion services-core/src/test/java/com/mapbox/core/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Scanner;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public static Builder builder() {
public abstract String name();

/**
* Any road designations associated with the road or path leading from this step's
* maneuver to the next step's maneuver. Optionally included, if data is available.
* Any road designations associated with the road or path leading from this step's
* maneuver to the next step's maneuver. Optionally included, if data is available.
* If multiple road designations are associated with the road, they are separated by semicolons.
* A road designation typically consists of an alphabetic network code (identifying the road type
* or numbering system), a space or hyphen, and a route number. You should not assume that
Expand Down
5 changes: 0 additions & 5 deletions services-geojson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ dependencies {
// Annotations
compileOnly dependenciesList.supportAnnotation

// AutoValue
compileOnly dependenciesList.autoValue
compileOnly dependenciesList.autoValueGson

// Test Dependencies
testImplementation dependenciesList.okhttp3Mockwebserver
testImplementation project(path: ':services-core', configuration: 'testOutput')
testImplementation project(":services-turf")
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import com.mapbox.core.TestUtils;
import com.mapbox.core.constants.Constants;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand All @@ -20,6 +17,9 @@ public class LineStringTest extends TestUtils {

private static final String SAMPLE_LINESTRING_FIXTURE = "sample-linestring.json";

private static final int PRECISION_6 = 6;
private static final int PRECISION_5 = 5;

@Rule
public ExpectedException thrown = ExpectedException.none();

Expand All @@ -40,7 +40,7 @@ public void fromLngLats_generatedFromMultipoint() throws Exception {
points.add(Point.fromLngLat(4.0,8.0));
MultiPoint multiPoint = MultiPoint.fromLngLats(points);
LineString lineString = LineString.fromLngLats(multiPoint);
assertEquals("_gayB_c`|@_wemJ_kbvD", lineString.toPolyline(Constants.PRECISION_6));
assertEquals("_gayB_c`|@_wemJ_kbvD", lineString.toPolyline(PRECISION_6));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import com.mapbox.core.TestUtils;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand All @@ -19,6 +17,9 @@ public class MultiLineStringTest extends TestUtils {

private static final String SAMPLE_MULTILINESTRING = "sample-multilinestring.json";

private static final int PRECISION_6 = 6;
private static final int PRECISION_5 = 5;

@Rule
public ExpectedException thrown = ExpectedException.none();

Expand Down
61 changes: 61 additions & 0 deletions services-geojson/src/test/java/com/mapbox/geojson/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.mapbox.geojson;

import com.google.gson.JsonParser;

import org.hamcrest.Matchers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class TestUtils {

public static final double DELTA = 1E-10;
public static final String ACCESS_TOKEN = "pk.XXX";

public void compareJson(String expectedJson, String actualJson) {
JsonParser parser = new JsonParser();
assertThat(parser.parse(actualJson), Matchers.equalTo(parser.parse(expectedJson)));
}

protected String loadJsonFixture(String filename) throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(filename);
Scanner scanner = new Scanner(inputStream, UTF_8.name()).useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
}

public static <T extends Serializable> byte[] serialize(T obj)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.close();
return baos.toByteArray();
}

public static <T extends Serializable> T deserialize(byte[] bytes, Class<T> cl)
throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
Object object = ois.readObject();
return cl.cast(object);
}

/**
* Comes from Google Utils Test Case
*/
public static void expectNearNumber(double expected, double actual, double epsilon) {
assertTrue(String.format("Expected %f to be near %f", actual, expected),
Math.abs(expected - actual) <= epsilon);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.mapbox.core.TestUtils;
import com.mapbox.core.constants.Constants;
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.TestUtils;

import org.junit.Test;

import java.io.IOException;
Expand All @@ -20,6 +20,9 @@

public class PolylineUtilsTest extends TestUtils {

private static final int PRECISION_6 = 6;
private static final int PRECISION_5 = 5;

// Delta for Coordinates comparison
private static final double DELTA = 0.000001;

Expand All @@ -34,7 +37,7 @@ public class PolylineUtilsTest extends TestUtils {

@Test
public void testDecodePath() {
List<Point> latLngs = decode(TEST_LINE, Constants.PRECISION_5);
List<Point> latLngs = decode(TEST_LINE, PRECISION_5);

int expectedLength = 21;
assertEquals("Wrong length.", expectedLength, latLngs.size());
Expand All @@ -46,15 +49,15 @@ public void testDecodePath() {

@Test
public void testEncodePath5() {
List<Point> path = decode(TEST_LINE, Constants.PRECISION_5);
String encoded = encode(path, Constants.PRECISION_5);
List<Point> path = decode(TEST_LINE, PRECISION_5);
String encoded = encode(path, PRECISION_5);
assertEquals(TEST_LINE, encoded);
}

@Test
public void testDecodeEncodePath6() {
List<Point> path = decode(TEST_LINE6, Constants.PRECISION_6);
String encoded = encode(path, Constants.PRECISION_6);
List<Point> path = decode(TEST_LINE6, PRECISION_6);
String encoded = encode(path, PRECISION_6);
assertEquals(TEST_LINE6, encoded);
}

Expand All @@ -65,8 +68,8 @@ public void testFromPolyline6() {
Point.fromLngLat(2.2862036, 48.8267868),
Point.fromLngLat(2.4, 48.9)
);
String encoded = encode(originalPath, Constants.PRECISION_6);
List<Point> path = LineString.fromPolyline(encoded, Constants.PRECISION_6).coordinates();
String encoded = encode(originalPath, PRECISION_6);
List<Point> path = LineString.fromPolyline(encoded, PRECISION_6).coordinates();

assertEquals(originalPath.size(), path.size());
for (int i = 0; i < originalPath.size(); i++) {
Expand All @@ -78,8 +81,8 @@ public void testFromPolyline6() {
@Test
public void testFromPolylineAndDecode() {

List<Point> path1 = LineString.fromPolyline(TEST_LINE6, Constants.PRECISION_6).coordinates();
List<Point> path2 = decode(TEST_LINE6, Constants.PRECISION_6);
List<Point> path1 = LineString.fromPolyline(TEST_LINE6, PRECISION_6).coordinates();
List<Point> path2 = decode(TEST_LINE6, PRECISION_6);

assertEquals(path1.size(), path2.size());
for (int i = 0; i < path1.size(); i++) {
Expand All @@ -95,8 +98,8 @@ public void testEncodeDecodePath6() {
Point.fromLngLat(2.4, 48.9)
);

String encoded = encode(originalPath, Constants.PRECISION_6);
List<Point> path = decode(encoded, Constants.PRECISION_6);
String encoded = encode(originalPath, PRECISION_6);
List<Point> path = decode(encoded, PRECISION_6);
assertEquals(originalPath.size(), path.size());

for (int i = 0; i < originalPath.size(); i++) {
Expand All @@ -108,20 +111,20 @@ public void testEncodeDecodePath6() {

@Test
public void decode_neverReturnsNullButRatherAnEmptyList() throws Exception {
List<Point> path = decode("", Constants.PRECISION_5);
List<Point> path = decode("", PRECISION_5);
assertNotNull(path);
assertEquals(0, path.size());
}

@Test
public void encode_neverReturnsNull() throws Exception {
String encodedString = encode(new ArrayList<Point>(), Constants.PRECISION_6);
String encodedString = encode(new ArrayList<Point>(), PRECISION_6);
assertNotNull(encodedString);
}

@Test
public void simplify_neverReturnsNullButRatherAnEmptyList() throws Exception {
List<Point> simplifiedPath = simplify(new ArrayList<Point>(), Constants.PRECISION_6);
List<Point> simplifiedPath = simplify(new ArrayList<Point>(), PRECISION_6);
assertNotNull(simplifiedPath);
}

Expand All @@ -130,14 +133,14 @@ public void simplify_returnSameListWhenListSizeIsLessThanOrEqualToTwo(){
final List<Point> path = new ArrayList<>();
path.add(Point.fromLngLat(0, 0));
path.add(Point.fromLngLat(10, 0));
List<Point> simplifiedPath = simplify(path, Constants.PRECISION_6, true);
List<Point> simplifiedPath = simplify(path, PRECISION_6, true);
assertTrue("Returned list is different from input list", path == simplifiedPath);
}

@Test
public void simplify_withHighestQuality() throws IOException{
List<Point> path = createPointListFromResourceFile(SIMPLIFICATION_INPUT);
List<Point> simplifiedPath = simplify(path, Constants.PRECISION_5, true);
List<Point> simplifiedPath = simplify(path, PRECISION_5, true);
List<Point> expectedSimplifiedPath = createPointListFromResourceFile(SIMPLIFICATION_EXPECTED_OUTPUT);
assertTrue("Wrong number of points retained",simplifiedPath.size() == expectedSimplifiedPath.size());
int counter = 0;
Expand Down
8 changes: 0 additions & 8 deletions services-turf/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@ apply plugin: 'java-library'
apply from: "../gradle/dependencies.gradle"

dependencies {
api project(":services-core")
api project(":services-geojson")

// Annotations
compileOnly dependenciesList.supportAnnotation

// AutoValue
compileOnly dependenciesList.autoValue
compileOnly dependenciesList.autoValueGson

// Test Dependencies
testImplementation project(path: ':services-core', configuration: 'testOutput')
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.GeoJson;
import com.mapbox.core.utils.TextUtils;
import com.mapbox.geojson.Point;

/**
Expand Down Expand Up @@ -43,7 +42,7 @@ public static Point getCoord(Feature obj) {
* @since 1.2.0
*/
public static void geojsonType(GeoJson value, String type, String name) {
if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) {
if (type == null || type.length() == 0 || name == null || name.length() == 0) {
throw new TurfException("Type and name required");
}
if (value == null || !value.type().equals(type)) {
Expand All @@ -63,7 +62,7 @@ public static void geojsonType(GeoJson value, String type, String name) {
* @since 1.2.0
*/
public static void featureOf(Feature feature, String type, String name) {
if (TextUtils.isEmpty(name)) {
if (name == null || name.length() == 0) {
throw new TurfException(".featureOf() requires a name");
}
if (feature == null || !feature.type().equals("Feature") || feature.geometry() == null) {
Expand All @@ -87,7 +86,7 @@ public static void featureOf(Feature feature, String type, String name) {
* @since 1.2.0
*/
public static void collectionOf(FeatureCollection featureCollection, String type, String name) {
if (TextUtils.isEmpty(name)) {
if (name == null || name.length() == 0) {
throw new TurfException("collectionOf() requires a name");
}
if (featureCollection == null || !featureCollection.type().equals("FeatureCollection")
Expand Down
11 changes: 6 additions & 5 deletions services-turf/src/main/java/com/mapbox/turf/TurfMisc.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.mapbox.turf;

import static com.mapbox.core.internal.Preconditions.checkNotNull;

import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import com.mapbox.geojson.Feature;
Expand Down Expand Up @@ -41,7 +39,9 @@ private TurfMisc() {
@NonNull
public static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt,
@NonNull Feature line) {
checkNotNull(line.geometry(), "Feature.geometry() == null");
if (line.geometry() == null) {
throw new NullPointerException("Feature.geometry() == null");
}
if (!line.geometry().type().equals("LineString")) {
throw new TurfException("input must be a LineString Feature or Geometry");
}
Expand Down Expand Up @@ -115,8 +115,9 @@ public static LineString lineSliceAlong(@NonNull Feature line,
@FloatRange(from = 0) double startDist,
@FloatRange(from = 0) double stopDist,
@NonNull @TurfConstants.TurfUnitCriteria String units) {

checkNotNull(line.geometry(), "Feature.geometry() == null");
if (line.geometry() == null) {
throw new NullPointerException("Feature.geometry() == null");
}
if (!line.geometry().type().equals("LineString")) {
throw new TurfException("input must be a LineString Feature or Geometry");
}
Expand Down
Loading

0 comments on commit fea0fc1

Please sign in to comment.