Remove unnecessary Writeable implementation from GeoPoint

GeoPoint now has native support in StreamOutput/StreamInput
impementing Writable is not necessary. This also adds tests
for XContentBuilder rendering GeoPoint
This commit is contained in:
Simon Willnauer 2015-10-08 12:33:14 +02:00
parent a46cf97388
commit cc40f20544
6 changed files with 24 additions and 37 deletions

View File

@ -19,12 +19,6 @@
package org.elasticsearch.common.geo;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import java.io.IOException;
import org.apache.lucene.util.BitUtil;
import org.apache.lucene.util.XGeoHashUtils;
import org.apache.lucene.util.XGeoUtils;
@ -32,15 +26,12 @@ import org.apache.lucene.util.XGeoUtils;
/**
*
*/
public final class GeoPoint implements Writeable<GeoPoint> {
public final class GeoPoint {
private double lat;
private double lon;
private final static double TOLERANCE = XGeoUtils.TOLERANCE;
// for serialization purposes
private static final GeoPoint PROTOTYPE = new GeoPoint(Double.NaN, Double.NaN);
public GeoPoint() {
}
@ -179,21 +170,4 @@ public final class GeoPoint implements Writeable<GeoPoint> {
public static GeoPoint fromIndexLong(long indexLong) {
return new GeoPoint().resetFromIndexHash(indexLong);
}
@Override
public GeoPoint readFrom(StreamInput in) throws IOException {
double lat = in.readDouble();
double lon = in.readDouble();
return new GeoPoint(lat, lon);
}
public static GeoPoint readGeoPointFrom(StreamInput in) throws IOException {
return PROTOTYPE.readFrom(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeDouble(lat);
out.writeDouble(lon);
}
}

View File

@ -314,8 +314,8 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBounding
public GeoBoundingBoxQueryBuilder doReadFrom(StreamInput in) throws IOException {
String fieldName = in.readString();
GeoBoundingBoxQueryBuilder geo = new GeoBoundingBoxQueryBuilder(fieldName);
geo.topLeft = geo.topLeft.readFrom(in);
geo.bottomRight = geo.bottomRight.readFrom(in);
geo.topLeft = in.readGeoPoint();
geo.bottomRight = in.readGeoPoint();
geo.type = GeoExecType.readTypeFrom(in);
geo.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in);
return geo;
@ -324,8 +324,8 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBounding
@Override
public void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName);
topLeft.writeTo(out);
bottomRight.writeTo(out);
out.writeGeoPoint(topLeft);
out.writeGeoPoint(bottomRight);
type.writeTo(out);
validationMethod.writeTo(out);
}

View File

@ -261,7 +261,7 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQue
GeoDistanceQueryBuilder result = new GeoDistanceQueryBuilder(fieldName);
result.distance = in.readDouble();
result.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in);
result.center = GeoPoint.readGeoPointFrom(in);
result.center = in.readGeoPoint();
result.optimizeBbox = in.readString();
result.geoDistance = GeoDistance.readGeoDistanceFrom(in);
return result;
@ -272,7 +272,7 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQue
out.writeString(fieldName);
out.writeDouble(distance);
validationMethod.writeTo(out);
center.writeTo(out);
out.writeGeoPoint(center);
out.writeString(optimizeBbox);
geoDistance.writeTo(out);
}

View File

@ -277,7 +277,7 @@ public class GeoDistanceRangeQueryBuilder extends AbstractQueryBuilder<GeoDistan
@Override
protected GeoDistanceRangeQueryBuilder doReadFrom(StreamInput in) throws IOException {
GeoDistanceRangeQueryBuilder queryBuilder = new GeoDistanceRangeQueryBuilder(in.readString(), GeoPoint.readGeoPointFrom(in));
GeoDistanceRangeQueryBuilder queryBuilder = new GeoDistanceRangeQueryBuilder(in.readString(), in.readGeoPoint());
queryBuilder.from = in.readGenericValue();
queryBuilder.to = in.readGenericValue();
queryBuilder.includeLower = in.readBoolean();
@ -292,7 +292,7 @@ public class GeoDistanceRangeQueryBuilder extends AbstractQueryBuilder<GeoDistan
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName);
point.writeTo(out);
out.writeGeoPoint(point);
out.writeGenericValue(from);
out.writeGenericValue(to);
out.writeBoolean(includeLower);

View File

@ -164,7 +164,7 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
List<GeoPoint> shell = new ArrayList<>();
int size = in.readVInt();
for (int i = 0; i < size; i++) {
shell.add(GeoPoint.readGeoPointFrom(in));
shell.add(in.readGeoPoint());
}
GeoPolygonQueryBuilder builder = new GeoPolygonQueryBuilder(fieldName, shell);
builder.validationMethod = GeoValidationMethod.readGeoValidationMethodFrom(in);
@ -176,7 +176,7 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
out.writeString(fieldName);
out.writeVInt(shell.size());
for (GeoPoint point : shell) {
point.writeTo(out);
out.writeGeoPoint(point);
}
validationMethod.writeTo(out);
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.xcontent.builder;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.FastCharArrayWriter;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
@ -351,4 +352,16 @@ public class XContentBuilderTests extends ESTestCase {
" foobar: \"boom\"\n", string);
}
public void testRenderGeoPoint() throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
builder.startObject().field("foo").value(new GeoPoint(1,2)).endObject();
String string = builder.string();
assertEquals("{\n" +
" \"foo\" : {\n" +
" \"lat\" : 1.0,\n" +
" \"lon\" : 2.0\n" +
" }\n" +
"}", string.trim());
}
}