diff --git a/src/main/java/org/geojson/Geometry.java b/src/main/java/org/geojson/Geometry.java index 2f65fcd..36ef303 100644 --- a/src/main/java/org/geojson/Geometry.java +++ b/src/main/java/org/geojson/Geometry.java @@ -1,31 +1,21 @@ package org.geojson; -import java.util.ArrayList; -import java.util.List; - public abstract class Geometry extends GeoJsonObject { - protected List coordinates = new ArrayList(); + protected T coordinates; public Geometry() { } - public Geometry(T... elements) { - for (T coordinate : elements) { - coordinates.add(coordinate); - } - } - - public Geometry add(T elements) { - coordinates.add(elements); - return this; + public Geometry(T coordinates) { + this.coordinates = coordinates; } - public List getCoordinates() { + public T getCoordinates() { return coordinates; } - public void setCoordinates(List coordinates) { + public void setCoordinates(T coordinates) { this.coordinates = coordinates; } diff --git a/src/main/java/org/geojson/LineString.java b/src/main/java/org/geojson/LineString.java index 144be96..88d9761 100644 --- a/src/main/java/org/geojson/LineString.java +++ b/src/main/java/org/geojson/LineString.java @@ -3,6 +3,7 @@ public class LineString extends MultiPoint { public LineString() { + super(); } public LineString(LngLatAlt... points) { diff --git a/src/main/java/org/geojson/MultiLineString.java b/src/main/java/org/geojson/MultiLineString.java index 4d32c77..afbfbcb 100644 --- a/src/main/java/org/geojson/MultiLineString.java +++ b/src/main/java/org/geojson/MultiLineString.java @@ -1,14 +1,22 @@ package org.geojson; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; -public class MultiLineString extends Geometry> { +public class MultiLineString extends Geometry>> { public MultiLineString() { + super(new ArrayList>()); } public MultiLineString(List line) { - add(line); + super(new ArrayList>(Collections.singletonList(line))); + } + + public MultiLineString add(List line) { + coordinates.add(line); + return this; } @Override diff --git a/src/main/java/org/geojson/MultiPoint.java b/src/main/java/org/geojson/MultiPoint.java index b022ec0..cc7ca3e 100644 --- a/src/main/java/org/geojson/MultiPoint.java +++ b/src/main/java/org/geojson/MultiPoint.java @@ -1,15 +1,30 @@ package org.geojson; -public class MultiPoint extends Geometry { +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class MultiPoint extends Geometry> { public MultiPoint() { + this(Collections.emptyList()); } public MultiPoint(LngLatAlt... points) { - super(points); + this(Arrays.asList(points)); } - @Override + private MultiPoint(List points) { + super(new ArrayList(points)); + } + + public MultiPoint add(LngLatAlt point) { + coordinates.add(point); + return this; + } + + @Override public T accept(GeoJsonObjectVisitor geoJsonObjectVisitor) { return geoJsonObjectVisitor.visit(this); } diff --git a/src/main/java/org/geojson/MultiPolygon.java b/src/main/java/org/geojson/MultiPolygon.java index 9698c2f..ab79ad0 100644 --- a/src/main/java/org/geojson/MultiPolygon.java +++ b/src/main/java/org/geojson/MultiPolygon.java @@ -1,14 +1,21 @@ package org.geojson; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; -public class MultiPolygon extends Geometry>> { +public class MultiPolygon extends Geometry>>> { public MultiPolygon() { + this(Collections.>>emptyList()); } public MultiPolygon(Polygon polygon) { - add(polygon); + this(Collections.singletonList(polygon.getCoordinates())); + } + + private MultiPolygon(List>> elements) { + super(new ArrayList>>(elements)); } public MultiPolygon add(Polygon polygon) { diff --git a/src/main/java/org/geojson/Point.java b/src/main/java/org/geojson/Point.java index 3f4f349..e92550a 100644 --- a/src/main/java/org/geojson/Point.java +++ b/src/main/java/org/geojson/Point.java @@ -1,34 +1,25 @@ package org.geojson; -public class Point extends GeoJsonObject { - - private LngLatAlt coordinates; +public class Point extends Geometry { public Point() { + super(); } - public Point(LngLatAlt coordinates) { - this.coordinates = coordinates; + public Point(LngLatAlt coordinate) { + this.coordinates = coordinate; } public Point(double longitude, double latitude) { - coordinates = new LngLatAlt(longitude, latitude); + this(new LngLatAlt(longitude, latitude)); } public Point(double longitude, double latitude, double altitude) { - coordinates = new LngLatAlt(longitude, latitude, altitude); + this(new LngLatAlt(longitude, latitude, altitude)); } public Point(double longitude, double latitude, double altitude, double... additionalElements) { - coordinates = new LngLatAlt(longitude, latitude, altitude, additionalElements); - } - - public LngLatAlt getCoordinates() { - return coordinates; - } - - public void setCoordinates(LngLatAlt coordinates) { - this.coordinates = coordinates; + this(new LngLatAlt(longitude, latitude, altitude, additionalElements)); } @Override @@ -47,8 +38,9 @@ public boolean equals(Object o) { if (!super.equals(o)) { return false; } - Point point = (Point)o; - return !(coordinates != null ? !coordinates.equals(point.coordinates) : point.coordinates != null); + Point point = (Point) o; + return !(coordinates != null ? !coordinates.equals(point.coordinates) + : point.coordinates != null); } @Override @@ -60,6 +52,6 @@ public int hashCode() { @Override public String toString() { - return "Point{" + "coordinates=" + coordinates + "} " + super.toString(); + return "Point{} " + super.toString(); } } diff --git a/src/main/java/org/geojson/Polygon.java b/src/main/java/org/geojson/Polygon.java index d4c42af..984c966 100644 --- a/src/main/java/org/geojson/Polygon.java +++ b/src/main/java/org/geojson/Polygon.java @@ -1,21 +1,23 @@ package org.geojson; +import com.fasterxml.jackson.annotation.JsonIgnore; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; -import com.fasterxml.jackson.annotation.JsonIgnore; - -public class Polygon extends Geometry> { +public class Polygon extends Geometry>> { public Polygon() { + super(new ArrayList>()); } public Polygon(List polygon) { - add(polygon); + super(new ArrayList>(Collections.singletonList(polygon))); } public Polygon(LngLatAlt... polygon) { - add(Arrays.asList(polygon)); + this(Arrays.asList(polygon)); } public void setExteriorRing(List points) { @@ -54,8 +56,9 @@ public void addInteriorRing(LngLatAlt... points) { } private void assertExteriorRing() { - if (coordinates.isEmpty()) + if (coordinates.isEmpty()) { throw new RuntimeException("No exterior ring definied"); + } } @Override diff --git a/src/test/java/org/geojson/ToStringTest.java b/src/test/java/org/geojson/ToStringTest.java index e892c5c..249573e 100644 --- a/src/test/java/org/geojson/ToStringTest.java +++ b/src/test/java/org/geojson/ToStringTest.java @@ -27,7 +27,7 @@ public void itShouldToStringFeatureCollection() throws Exception { public void itShouldToStringPoint() throws Exception { Point geometry = new Point(10, 20); assertEquals( - "Point{coordinates=LngLatAlt{longitude=10.0, latitude=20.0, altitude=NaN}} GeoJsonObject{}", + "Point{} Geometry{coordinates=LngLatAlt{longitude=10.0, latitude=20.0, altitude=NaN}} GeoJsonObject{}", geometry.toString()); } @@ -35,7 +35,7 @@ public void itShouldToStringPoint() throws Exception { public void itShouldToStringPointWithAdditionalElements() { Point geometry = new Point(10, 20, 30, 40D, 50D); assertEquals( - "Point{coordinates=LngLatAlt{longitude=10.0, latitude=20.0, altitude=30.0, additionalElements=[40.0, 50.0]}} GeoJsonObject{}", + "Point{} Geometry{coordinates=LngLatAlt{longitude=10.0, latitude=20.0, altitude=30.0, additionalElements=[40.0, 50.0]}} GeoJsonObject{}", geometry.toString()); } @@ -43,7 +43,7 @@ public void itShouldToStringPointWithAdditionalElements() { public void itShouldToStringPointWithAdditionalElementsAndIgnoreNulls() { Point geometry = new Point(10, 20, 30, 40D, 50D); assertEquals( - "Point{coordinates=LngLatAlt{longitude=10.0, latitude=20.0, altitude=30.0, additionalElements=[40.0, 50.0]}} GeoJsonObject{}", + "Point{} Geometry{coordinates=LngLatAlt{longitude=10.0, latitude=20.0, altitude=30.0, additionalElements=[40.0, 50.0]}} GeoJsonObject{}", geometry.toString()); }