From 2d7683646b0c60d6b3fc30f793abd97c1a7d9b49 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Wed, 21 Nov 2018 18:25:38 +0100 Subject: [PATCH] [annotation] - generic geometry --- plugin-annotation/scripts/annotation.java.ejs | 63 +++---------------- .../scripts/annotation_options.java.ejs | 6 +- .../plugins/annotation/Annotation.java | 10 +-- .../mapboxsdk/plugins/annotation/Circle.java | 9 ++- .../plugins/annotation/CircleOptions.java | 4 +- .../mapboxsdk/plugins/annotation/Fill.java | 8 +-- .../plugins/annotation/FillOptions.java | 4 +- .../mapboxsdk/plugins/annotation/Line.java | 10 +-- .../plugins/annotation/LineOptions.java | 4 +- .../mapboxsdk/plugins/annotation/Symbol.java | 9 ++- .../plugins/annotation/SymbolOptions.java | 4 +- 11 files changed, 41 insertions(+), 90 deletions(-) diff --git a/plugin-annotation/scripts/annotation.java.ejs b/plugin-annotation/scripts/annotation.java.ejs index 5dad386bd..33a5a7582 100644 --- a/plugin-annotation/scripts/annotation.java.ejs +++ b/plugin-annotation/scripts/annotation.java.ejs @@ -29,7 +29,7 @@ import static com.mapbox.mapboxsdk.constants.GeometryConstants.MAX_MERCATOR_LATI import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE; @UiThread -public class <%- camelize(type) %> extends Annotation { +public class <%- camelize(type) %> extends Annotation<<%- geometryType(type) %>> { private final AnnotationManager, ?, ?, ?, ?> annotationManager; <% if (type === "symbol") { -%> @@ -44,7 +44,7 @@ public class <%- camelize(type) %> extends Annotation { * @param jsonObject the features of the annotation * @param geometry the geometry of the annotation */ - <%- camelize(type) %>(long id, AnnotationManager, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, Geometry geometry) { + <%- camelize(type) %>(long id, AnnotationManager, ?, ?, ?, ?> annotationManager, JsonObject jsonObject, <%- geometryType(type) %> geometry) { super(id, jsonObject, geometry); this.annotationManager = annotationManager; } @@ -85,8 +85,7 @@ public class <%- camelize(type) %> extends Annotation { */ @NonNull public LatLng getLatLng() { - Point point = (Point) geometry; - return new LatLng(point.latitude(), point.longitude()); + return new LatLng(geometry.latitude(), geometry.longitude()); } <% } else if (type === "line") { -%> @@ -164,7 +163,6 @@ public class <%- camelize(type) %> extends Annotation { return latLngs; } <% } -%> -<% if (type === "circle" || type === "symbol") { -%> /** * Set the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map @@ -174,7 +172,7 @@ public class <%- camelize(type) %> extends Annotation { * * @param geometry the geometry of the <%- type %> */ - public void setGeometry(Point geometry) { + public void setGeometry(<%- geometryType(type) %> geometry) { this.geometry = geometry; } @@ -183,54 +181,9 @@ public class <%- camelize(type) %> extends Annotation { * * @return the geometry of the <%- type %> */ - public Point getGeometry() { - return ((Point) geometry); - } -<% } else if (type === "line") { -%> - - /** - * Set the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map - *

- * To update the <%- type %> on the map use {@link <%- camelize(type) %>Manager#update(Annotation)}. - *

- * - * @param geometry the geometry of the <%- type %> - */ - public void setGeometry(LineString geometry) { - this.geometry = geometry; - } - - /** - * Get the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map - * - * @return geometry the geometry of the <%- type %> - */ - public LineString getGeometry() { - return ((LineString) geometry); + public <%- geometryType(type) %> getGeometry() { + return geometry; } -<% } else { -%> - - /** - * Set the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map - *

- * To update the <%- type %> on the map use {@link <%- camelize(type) %>Manager#update(Annotation)}. - *

- * - * @param geometry the geometry of the <%- type %> - */ - public void setGeometry(Polygon geometry) { - this.geometry = geometry; - } - - /** - * Get the Geometry of the <%- type %>, which represents the location of the <%- type %> on the map - * - * @return the geometry of the <%- type %> - */ - public Polygon getGeometry() { - return ((Polygon) geometry); - } -<% } -%> <% if (type === "symbol") { -%> /** @@ -390,7 +343,7 @@ public class <%- camelize(type) %> extends Annotation { @Nullable Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject, float touchAreaShiftX, float touchAreaShiftY) { - List originalPoints = ((LineString) getGeometry()).coordinates(); + List originalPoints = geometry.coordinates(); List resultingPoints = new ArrayList<>(originalPoints.size()); for (Point jsonPoint : originalPoints) { PointF pointF = projection.toScreenLocation(new LatLng(jsonPoint.latitude(), jsonPoint.longitude())); @@ -412,7 +365,7 @@ public class <%- camelize(type) %> extends Annotation { @Nullable Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject, float touchAreaShiftX, float touchAreaShiftY) { - List> originalPoints = ((Polygon) getGeometry()).coordinates(); + List> originalPoints = geometry.coordinates(); if (originalPoints != null) { List> resultingPoints = new ArrayList<>(originalPoints.size()); for (List points : originalPoints) { diff --git a/plugin-annotation/scripts/annotation_options.java.ejs b/plugin-annotation/scripts/annotation_options.java.ejs index de5c43b1d..b04d00b6e 100644 --- a/plugin-annotation/scripts/annotation_options.java.ejs +++ b/plugin-annotation/scripts/annotation_options.java.ejs @@ -30,7 +30,7 @@ import static com.mapbox.mapboxsdk.plugins.annotation.ConvertUtils.toStringArray public class <%- camelize(type) %>Options extends Options<<%- camelize(type) %>> { private boolean isDraggable; - private Geometry geometry; + private <%- geometryType(type) %> geometry; <% for (const property of properties) { -%> <% if (supportsPropertyFunction(property)) { -%> private <%- propertyType(property) %> <%- camelizeWithLeadingLowercase(property.name) %>; @@ -82,7 +82,7 @@ public class <%- camelize(type) %>Options extends Options<<%- camelize(type) %>> * @param geometry the location of the <%- type %> * @return this */ - public <%- camelize(type) %>Options withGeometry(Point geometry) { + public <%- camelize(type) %>Options withGeometry(<%- geometryType(type) %> geometry) { this.geometry = geometry; return this; } @@ -248,7 +248,7 @@ public class <%- camelize(type) %>Options extends Options<<%- camelize(type) %>> } <%- camelize(type) %>Options options = new <%- camelize(type) %>Options(); - options.geometry = feature.geometry(); + options.geometry = (<%- geometryType(type) %>) feature.geometry(); <% for (const property of properties) { -%> <% if (supportsPropertyFunction(property)) { -%> <% if (propertyType(property).endsWith("[]")) { -%> diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Annotation.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Annotation.java index 1cb549075..c9a901af7 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Annotation.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Annotation.java @@ -8,24 +8,24 @@ import com.mapbox.geojson.Geometry; import com.mapbox.mapboxsdk.maps.Projection; -public abstract class Annotation { +public abstract class Annotation { static final String ID_KEY = "id"; protected JsonObject jsonObject; - protected Geometry geometry; + protected T geometry; private boolean isDraggable; - Annotation(long id, JsonObject jsonObject, Geometry geometry) { + Annotation(long id, JsonObject jsonObject, T geometry) { this.jsonObject = jsonObject; this.jsonObject.addProperty(ID_KEY, id); this.geometry = geometry; } - void setGeometry(Geometry geometry) { + void setGeometry(T geometry) { this.geometry = geometry; } - Geometry getGeometry() { + T getGeometry() { if (geometry == null) { throw new IllegalStateException(); } diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Circle.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Circle.java index 7f89e52c6..dfa41a697 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Circle.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Circle.java @@ -24,7 +24,7 @@ import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE; @UiThread -public class Circle extends Annotation { +public class Circle extends Annotation { private final AnnotationManager annotationManager; @@ -35,7 +35,7 @@ public class Circle extends Annotation { * @param jsonObject the features of the annotation * @param geometry the geometry of the annotation */ - Circle(long id, AnnotationManager annotationManager, JsonObject jsonObject, Geometry geometry) { + Circle(long id, AnnotationManager annotationManager, JsonObject jsonObject, Point geometry) { super(id, jsonObject, geometry); this.annotationManager = annotationManager; } @@ -84,8 +84,7 @@ public void setLatLng(LatLng latLng) { */ @NonNull public LatLng getLatLng() { - Point point = (Point) geometry; - return new LatLng(point.latitude(), point.longitude()); + return new LatLng(geometry.latitude(), geometry.longitude()); } /** @@ -106,7 +105,7 @@ public void setGeometry(Point geometry) { * @return the geometry of the circle */ public Point getGeometry() { - return ((Point) geometry); + return geometry; } // Property accessors diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/CircleOptions.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/CircleOptions.java index e4a3b9780..32d9236d4 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/CircleOptions.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/CircleOptions.java @@ -25,7 +25,7 @@ public class CircleOptions extends Options { private boolean isDraggable; - private Geometry geometry; + private Point geometry; private Float circleRadius; private String circleColor; private Float circleBlur; @@ -262,7 +262,7 @@ static CircleOptions fromFeature(@NonNull Feature feature) { } CircleOptions options = new CircleOptions(); - options.geometry = feature.geometry(); + options.geometry = (Point) feature.geometry(); if (feature.hasProperty("circle-radius")) { options.circleRadius = feature.getProperty("circle-radius").getAsFloat(); } diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Fill.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Fill.java index 6a1c1ae56..2e2ce43b3 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Fill.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Fill.java @@ -24,7 +24,7 @@ import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE; @UiThread -public class Fill extends Annotation { +public class Fill extends Annotation { private final AnnotationManager annotationManager; @@ -35,7 +35,7 @@ public class Fill extends Annotation { * @param jsonObject the features of the annotation * @param geometry the geometry of the annotation */ - Fill(long id, AnnotationManager annotationManager, JsonObject jsonObject, Geometry geometry) { + Fill(long id, AnnotationManager annotationManager, JsonObject jsonObject, Polygon geometry) { super(id, jsonObject, geometry); this.annotationManager = annotationManager; } @@ -116,7 +116,7 @@ public void setGeometry(Polygon geometry) { * @return the geometry of the fill */ public Polygon getGeometry() { - return ((Polygon) geometry); + return geometry; } // Property accessors @@ -211,7 +211,7 @@ public void setFillPattern(String value) { @Nullable Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject, float touchAreaShiftX, float touchAreaShiftY) { - List> originalPoints = ((Polygon) getGeometry()).coordinates(); + List> originalPoints = geometry.coordinates(); if (originalPoints != null) { List> resultingPoints = new ArrayList<>(originalPoints.size()); for (List points : originalPoints) { diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/FillOptions.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/FillOptions.java index b20c791f0..023bdaba8 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/FillOptions.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/FillOptions.java @@ -25,7 +25,7 @@ public class FillOptions extends Options { private boolean isDraggable; - private Geometry geometry; + private Polygon geometry; private Float fillOpacity; private String fillColor; private String fillOutlineColor; @@ -200,7 +200,7 @@ static FillOptions fromFeature(@NonNull Feature feature) { } FillOptions options = new FillOptions(); - options.geometry = feature.geometry(); + options.geometry = (Polygon) feature.geometry(); if (feature.hasProperty("fill-opacity")) { options.fillOpacity = feature.getProperty("fill-opacity").getAsFloat(); } diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Line.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Line.java index c5551d409..0eaec3971 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Line.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Line.java @@ -24,7 +24,7 @@ import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE; @UiThread -public class Line extends Annotation { +public class Line extends Annotation { private final AnnotationManager annotationManager; @@ -35,7 +35,7 @@ public class Line extends Annotation { * @param jsonObject the features of the annotation * @param geometry the geometry of the annotation */ - Line(long id, AnnotationManager annotationManager, JsonObject jsonObject, Geometry geometry) { + Line(long id, AnnotationManager annotationManager, JsonObject jsonObject, LineString geometry) { super(id, jsonObject, geometry); this.annotationManager = annotationManager; } @@ -114,10 +114,10 @@ public void setGeometry(LineString geometry) { /** * Get the Geometry of the line, which represents the location of the line on the map * - * @return geometry the geometry of the line + * @return the geometry of the line */ public LineString getGeometry() { - return ((LineString) geometry); + return geometry; } // Property accessors @@ -295,7 +295,7 @@ public void setLinePattern(String value) { @Nullable Geometry getOffsetGeometry(@NonNull Projection projection, @NonNull MoveDistancesObject moveDistancesObject, float touchAreaShiftX, float touchAreaShiftY) { - List originalPoints = ((LineString) getGeometry()).coordinates(); + List originalPoints = geometry.coordinates(); List resultingPoints = new ArrayList<>(originalPoints.size()); for (Point jsonPoint : originalPoints) { PointF pointF = projection.toScreenLocation(new LatLng(jsonPoint.latitude(), jsonPoint.longitude())); diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/LineOptions.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/LineOptions.java index 9eb99fbd5..42e7d7de3 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/LineOptions.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/LineOptions.java @@ -25,7 +25,7 @@ public class LineOptions extends Options { private boolean isDraggable; - private Geometry geometry; + private LineString geometry; private String lineJoin; private Float lineOpacity; private String lineColor; @@ -289,7 +289,7 @@ static LineOptions fromFeature(@NonNull Feature feature) { } LineOptions options = new LineOptions(); - options.geometry = feature.geometry(); + options.geometry = (LineString) feature.geometry(); if (feature.hasProperty("line-join")) { options.lineJoin = feature.getProperty("line-join").getAsString(); } diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Symbol.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Symbol.java index a6178144f..025ca7a48 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Symbol.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/Symbol.java @@ -24,7 +24,7 @@ import static com.mapbox.mapboxsdk.constants.GeometryConstants.MIN_MERCATOR_LATITUDE; @UiThread -public class Symbol extends Annotation { +public class Symbol extends Annotation { private final AnnotationManager annotationManager; @@ -37,7 +37,7 @@ public class Symbol extends Annotation { * @param jsonObject the features of the annotation * @param geometry the geometry of the annotation */ - Symbol(long id, AnnotationManager annotationManager, JsonObject jsonObject, Geometry geometry) { + Symbol(long id, AnnotationManager annotationManager, JsonObject jsonObject, Point geometry) { super(id, jsonObject, geometry); this.annotationManager = annotationManager; } @@ -143,8 +143,7 @@ public void setLatLng(LatLng latLng) { */ @NonNull public LatLng getLatLng() { - Point point = (Point) geometry; - return new LatLng(point.latitude(), point.longitude()); + return new LatLng(geometry.latitude(), geometry.longitude()); } /** @@ -165,7 +164,7 @@ public void setGeometry(Point geometry) { * @return the geometry of the symbol */ public Point getGeometry() { - return ((Point) geometry); + return geometry; } /** diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/SymbolOptions.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/SymbolOptions.java index 14c58bc90..cad145ceb 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/SymbolOptions.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/SymbolOptions.java @@ -25,7 +25,7 @@ public class SymbolOptions extends Options { private boolean isDraggable; - private Geometry geometry; + private Point geometry; private Float iconSize; private String iconImage; private Float iconRotate; @@ -702,7 +702,7 @@ static SymbolOptions fromFeature(@NonNull Feature feature) { } SymbolOptions options = new SymbolOptions(); - options.geometry = feature.geometry(); + options.geometry = (Point) feature.geometry(); if (feature.hasProperty("icon-size")) { options.iconSize = feature.getProperty("icon-size").getAsFloat(); }