Skip to content

Commit

Permalink
Geo: Add GeoJson parser to libs/geo classes (#41575) (#41657)
Browse files Browse the repository at this point in the history
Adds GeoJson parser for Geometry classes defined in libs/geo.

Relates #40908 and #29872
  • Loading branch information
imotov authored Apr 29, 2019
1 parent 92a820b commit 10ab838
Show file tree
Hide file tree
Showing 23 changed files with 1,869 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public int hashCode() {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface Geometry {

ShapeType type();

<T> T visit(GeometryVisitor<T> visitor);
<T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E;

boolean isEmpty();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ShapeType type() {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@
*
* @see <a href="https://en.wikipedia.org/wiki/Visitor_pattern">Visitor Pattern</a>
*/
public interface GeometryVisitor<T> {
public interface GeometryVisitor<T, E extends Exception> {

T visit(Circle circle);
T visit(Circle circle) throws E;

T visit(GeometryCollection<?> collection);
T visit(GeometryCollection<?> collection) throws E;

T visit(Line line);
T visit(Line line) throws E;

T visit(LinearRing ring);
T visit(LinearRing ring) throws E;

T visit(MultiLine multiLine);
T visit(MultiLine multiLine) throws E;

T visit(MultiPoint multiPoint);
T visit(MultiPoint multiPoint) throws E;

T visit(MultiPolygon multiPolygon);
T visit(MultiPolygon multiPolygon) throws E;

T visit(Point point);
T visit(Point point) throws E;

T visit(Polygon polygon);
T visit(Polygon polygon) throws E;

T visit(Rectangle rectangle);
T visit(Rectangle rectangle) throws E;

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ShapeType type() {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ShapeType type() {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ShapeType type() {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ShapeType type() {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ShapeType type() {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public int hashCode() {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public LinearRing getHole(int i) {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public int hashCode() {
}

@Override
public <T> T visit(GeometryVisitor<T> visitor) {
public <T, E extends Exception> T visit(GeometryVisitor<T, E> visitor) throws E {
return visitor.visit(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.elasticsearch.geo.geometry;

import java.util.Locale;

/**
* Shape types supported by elasticsearch
*/
Expand All @@ -33,4 +35,8 @@ public enum ShapeType {
LINEARRING, // not serialized by itself in WKT or WKB
ENVELOPE, // not part of the actual WKB spec
CIRCLE; // not part of the actual WKB spec

public static ShapeType forName(String shapeName) {
return ShapeType.valueOf(shapeName.toUpperCase(Locale.ROOT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void toWKT(Geometry geometry, StringBuilder sb) {
if (geometry.isEmpty()) {
sb.append(EMPTY);
} else {
geometry.visit(new GeometryVisitor<Void>() {
geometry.visit(new GeometryVisitor<Void, RuntimeException>() {
@Override
public Void visit(Circle circle) {
sb.append(LPAREN);
Expand Down Expand Up @@ -543,7 +543,7 @@ private static String nextCloserOrComma(StreamTokenizer stream) throws IOExcepti
}

public static String getWKTName(Geometry geometry) {
return geometry.visit(new GeometryVisitor<String>() {
return geometry.visit(new GeometryVisitor<String, RuntimeException>() {
@Override
public String visit(Circle circle) {
return "circle";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testVisitor() {

public static void testVisitor(Geometry geom) {
AtomicBoolean called = new AtomicBoolean(false);
Object result = geom.visit(new GeometryVisitor<Object>() {
Object result = geom.visit(new GeometryVisitor<Object, RuntimeException>() {
private Object verify(Geometry geometry, String expectedClass) {
assertFalse("Visitor should be called only once", called.getAndSet(true));
assertSame(geom, geometry);
Expand Down
Loading

0 comments on commit 10ab838

Please sign in to comment.