Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsEqual.equalTo;
Expand All @@ -11,6 +12,7 @@
import com.mapbox.api.geocoding.v5.GeocodingTestUtils;
import com.mapbox.api.geocoding.v5.MapboxGeocoding;
import com.mapbox.core.TestUtils;
import com.mapbox.geojson.CoordinateContainer;
import com.mapbox.geojson.Point;
import org.junit.Test;
import retrofit2.Response;
Expand Down Expand Up @@ -81,8 +83,8 @@ public void fromJson_handlesConversionCorrectly() throws Exception {
assertThat(feature.type(), equalTo("Feature"));
assertEquals(5, feature.context().size());
assertThat(feature.geometry().type(), equalTo("Point"));
assertThat(feature.geometry().coordinates().toString(), equalTo("[-77.036543, "
+ "38.897702]"));
assertThat(((CoordinateContainer) feature.geometry()).coordinates().toString(),
equalTo("[-77.036543, 38.897702]"));
assertThat(feature.address(), equalTo("1600"));
assertThat(feature.id(), equalTo("address.3982178573139850"));
assertEquals(1, feature.placeType().size());
Expand Down Expand Up @@ -132,8 +134,8 @@ public void ForwardGeocode_withValidChineseResponse() throws Exception {

assertEquals(3, feature.context().size());
assertThat(feature.geometry().type(), equalTo("Point"));
assertThat(feature.geometry().coordinates().toString(), equalTo("[106.820552, "
+ "39.458115]"));
assertThat(((CoordinateContainer) feature.geometry()).coordinates().toString(),
equalTo("[106.820552, 39.458115]"));
assertThat(feature.id(), equalTo("place.10514057239276310"));
assertThat(feature.relevance(), equalTo(0.99));
assertThat(feature.placeName(), equalTo("中国内蒙古乌海市海南区"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mapbox.geojson;

/**
* Each of the s geometries which make up GeoJson implement this interface and consume a varying
* dimension of {@link Point} list. Since this is varying, each geometry object fulfills the
* contract by replacing the generic with a well defined list of Points.
*
* @param <T> a generic allowing varying dimensions for each GeoJson geometry
* @since 3.0.0
*/
public interface CoordinateContainer<T> extends Geometry {

/**
* the coordinates which define the geometry. Typically a list of points but for some geometry
* such as polygon this can be a list of a list of points, thus the return is generic here.
*
* @return the {@link Point}s which make up the coordinates defining the geometry
* @since 3.0.0
*/
T coordinates();
}
15 changes: 2 additions & 13 deletions services-geojson/src/main/java/com/mapbox/geojson/Geometry.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
package com.mapbox.geojson;

/**
* Each of the seven geometries which make up GeoJson implement this interface and consume a varying
* dimension of {@link Point} list. Since this is varying, each geometry object fulfills the
* contract by replacing the generic with a well defined list of Points.
* Each of the six geometries and {@link GeometryCollection} which make up GeoJson implement this interface.
*
* @param <T> a generic allowing varying dimensions for each GeoJson geometry
* @since 1.0.0
*/
public interface Geometry<T> extends GeoJson {
public interface Geometry extends GeoJson {

/**
* the coordinates which define the geometry. Typically a list of points but for some geometry
* such as polygon this can be a list of a list of points, thus the return is generic here.
*
* @return the {@link Point}s which make up the coordinates defining the geometry
* @since 1.0.0
*/
T coordinates();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.auto.value.AutoValue;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -61,7 +62,7 @@
* @since 1.0.0
*/
@AutoValue
public abstract class GeometryCollection implements GeoJson, Serializable {
public abstract class GeometryCollection implements Geometry, Serializable {

private static final String TYPE = "GeometryCollection";

Expand All @@ -72,7 +73,7 @@ public abstract class GeometryCollection implements GeoJson, Serializable {
*
* @param json a formatted valid JSON string defining a GeoJson Geometry Collection
* @return a new instance of this class defined by the values passed inside this static factory
* method
* method
* @since 1.0.0
*/
public static GeometryCollection fromJson(String json) {
Expand All @@ -89,7 +90,7 @@ public static GeometryCollection fromJson(String json) {
*
* @param geometries a non-null list of geometry which makes up this collection
* @return a new instance of this class defined by the values passed inside this static factory
* method
* method
* @since 1.0.0
*/
public static GeometryCollection fromGeometries(@NonNull List<Geometry> geometries) {
Expand All @@ -101,7 +102,7 @@ public static GeometryCollection fromGeometries(@NonNull List<Geometry> geometri
*
* @param geometry a non-null object of type geometry which makes up this collection
* @return a new instance of this class defined by the values passed inside this static factory
* method
* method
* @since 3.0.0
*/
public static GeometryCollection fromGeometry(@NonNull Geometry geometry) {
Expand All @@ -115,7 +116,7 @@ public static GeometryCollection fromGeometry(@NonNull Geometry geometry) {
* @param geometries a non-null list of geometry which makes up this collection
* @param bbox optionally include a bbox definition as a double array
* @return a new instance of this class defined by the values passed inside this static factory
* method
* method
* @since 1.0.0
*/
public static GeometryCollection fromGeometries(@NonNull List<Geometry> geometries,
Expand All @@ -129,7 +130,7 @@ public static GeometryCollection fromGeometries(@NonNull List<Geometry> geometri
* @param geometry a non-null object of type geometry which makes up this collection
* @param bbox optionally include a bbox definition as a double array
* @return a new instance of this class defined by the values passed inside this static factory
* method
* method
* @since 3.0.0
*/
public static GeometryCollection fromGeometry(@NonNull Geometry geometry,
Expand All @@ -143,7 +144,7 @@ public static GeometryCollection fromGeometry(@NonNull Geometry geometry,
* {@link GeometryCollection}.
*
* @return a String which describes the TYPE of geometry, for this object it will always return
* {@code GeometryCollection}
* {@code GeometryCollection}
* @since 1.0.0
*/
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* @since 1.0.0
*/
@AutoValue
public abstract class LineString implements Geometry<List<Point>>, Serializable {
public abstract class LineString implements CoordinateContainer<List<Point>>, Serializable {

private static final String TYPE = "LineString";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* @since 1.0.0
*/
@AutoValue
public abstract class MultiLineString implements Geometry<List<List<Point>>>, Serializable {
public abstract class MultiLineString implements CoordinateContainer<List<List<Point>>>, Serializable {

private static final String TYPE = "MultiLineString";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @since 1.0.0
*/
@AutoValue
public abstract class MultiPoint implements Geometry<List<Point>>, Serializable {
public abstract class MultiPoint implements CoordinateContainer<List<Point>>, Serializable {

private static final String TYPE = "MultiPoint";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
* @since 1.0.0
*/
@AutoValue
public abstract class MultiPolygon implements Geometry<List<List<List<Point>>>>, Serializable {
public abstract class MultiPolygon implements CoordinateContainer<List<List<List<Point>>>>, Serializable {

private static final String TYPE = "MultiPolygon";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* @since 1.0.0
*/
@AutoValue
public abstract class Point implements Geometry<List<Double>>, Serializable {
public abstract class Point implements CoordinateContainer<List<Double>>, Serializable {

private static final String TYPE = "Point";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* @since 1.0.0
*/
@AutoValue
public abstract class Polygon implements Geometry<List<List<Point>>>, Serializable {
public abstract class Polygon implements CoordinateContainer<List<List<Point>>>, Serializable {

private static final String TYPE = "Polygon";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.mapbox.geojson.CoordinateContainer;
import com.mapbox.geojson.Geometry;

import java.io.IOException;
Expand All @@ -16,7 +17,9 @@ public void write(JsonWriter out, Geometry value) throws IOException {
if (value.bbox() != null) {
out.name("bbox").jsonValue(value.bbox().toJson());
}
out.name("coordinates").jsonValue(value.coordinates().toString());
if (value instanceof CoordinateContainer) {
out.name("coordinates").jsonValue(((CoordinateContainer) value).coordinates().toString());
}
out.endObject();
}

Expand Down