DATAMONGO-1607 - Polishing.
Move coordinate conversion to dedicated method. Additionally fix issue with assertions applied to late in the chain and added some tests. Original Pull Request: #438
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -41,6 +41,7 @@ import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
|
||||
import org.springframework.data.mongodb.core.geo.Sphere;
|
||||
import org.springframework.data.mongodb.core.query.GeoCommand;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.NumberUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import com.mongodb.BasicDBList;
|
||||
@@ -121,11 +122,8 @@ abstract class GeoConverters {
|
||||
if (source.containsField("type")) {
|
||||
return DbObjectToGeoJsonPointConverter.INSTANCE.convert(source);
|
||||
}
|
||||
|
||||
Number x = (Number) source.get("x");
|
||||
Number y = (Number) source.get("y");
|
||||
|
||||
return new Point(x.doubleValue(), y.doubleValue());
|
||||
return new Point(toPrimitiveDoubleValue(source.get("x")), toPrimitiveDoubleValue(source.get("y")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,10 +257,12 @@ abstract class GeoConverters {
|
||||
}
|
||||
|
||||
DBObject center = (DBObject) source.get("center");
|
||||
Number radiusNumber = (Number) source.get("radius");
|
||||
Double radius = radiusNumber.doubleValue();
|
||||
Number radius = (Number) source.get("radius");
|
||||
|
||||
Distance distance = new Distance(radius);
|
||||
Assert.notNull(center, "Center must not be null!");
|
||||
Assert.notNull(radius, "Radius must not be null!");
|
||||
|
||||
Distance distance = new Distance(toPrimitiveDoubleValue(radius));
|
||||
|
||||
if (source.containsField("metric")) {
|
||||
|
||||
@@ -272,9 +272,6 @@ abstract class GeoConverters {
|
||||
distance = distance.in(Metrics.valueOf(metricString));
|
||||
}
|
||||
|
||||
Assert.notNull(center, "Center must not be null!");
|
||||
Assert.notNull(radius, "Radius must not be null!");
|
||||
|
||||
return new Circle(DbObjectToPointConverter.INSTANCE.convert(center), distance);
|
||||
}
|
||||
}
|
||||
@@ -331,11 +328,12 @@ abstract class GeoConverters {
|
||||
}
|
||||
|
||||
DBObject center = (DBObject) source.get("center");
|
||||
Number radiusNumber = (Number) source.get("radius");
|
||||
Double radius = radiusNumber.doubleValue();
|
||||
Number radius = (Number) source.get("radius");
|
||||
|
||||
Assert.notNull(center, "Center must not be null!");
|
||||
Assert.notNull(radius, "Radius must not be null!");
|
||||
|
||||
Distance distance = new Distance(radius);
|
||||
Distance distance = new Distance(toPrimitiveDoubleValue(radius));
|
||||
|
||||
if (source.containsField("metric")) {
|
||||
|
||||
@@ -345,9 +343,6 @@ abstract class GeoConverters {
|
||||
distance = distance.in(Metrics.valueOf(metricString));
|
||||
}
|
||||
|
||||
Assert.notNull(center, "Center must not be null!");
|
||||
Assert.notNull(radius, "Radius must not be null!");
|
||||
|
||||
return new Sphere(DbObjectToPointConverter.INSTANCE.convert(center), distance);
|
||||
}
|
||||
}
|
||||
@@ -607,7 +602,7 @@ abstract class GeoConverters {
|
||||
String.format("Cannot convert type '%s' to Point.", source.get("type")));
|
||||
|
||||
List<Number> dbl = (List<Number>) source.get("coordinates");
|
||||
return new GeoJsonPoint(dbl.get(0).doubleValue(), dbl.get(1).doubleValue());
|
||||
return new GeoJsonPoint(toPrimitiveDoubleValue(dbl.get(0)), toPrimitiveDoubleValue(dbl.get(1)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -841,7 +836,8 @@ abstract class GeoConverters {
|
||||
|
||||
List<Number> coordinatesList = (List<Number>) point;
|
||||
|
||||
points.add(new GeoJsonPoint(coordinatesList.get(0).doubleValue(), coordinatesList.get(1).doubleValue()));
|
||||
points.add(new GeoJsonPoint(toPrimitiveDoubleValue(coordinatesList.get(0)),
|
||||
toPrimitiveDoubleValue(coordinatesList.get(1))));
|
||||
}
|
||||
return points;
|
||||
}
|
||||
@@ -856,4 +852,10 @@ abstract class GeoConverters {
|
||||
static GeoJsonPolygon toGeoJsonPolygon(BasicDBList dbList) {
|
||||
return new GeoJsonPolygon(toListOfPoint((BasicDBList) dbList.get(0)));
|
||||
}
|
||||
|
||||
private static double toPrimitiveDoubleValue(Object value) {
|
||||
|
||||
Assert.isInstanceOf(Number.class, value, "Argument must be a Number.");
|
||||
return NumberUtils.convertNumberToTargetClass((Number) value, Double.class).doubleValue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ import org.springframework.data.mongodb.core.convert.GeoConverters.SphereToDbObj
|
||||
import org.springframework.data.mongodb.core.geo.Sphere;
|
||||
import org.springframework.data.mongodb.core.query.GeoCommand;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
@@ -48,6 +49,7 @@ import com.mongodb.DBObject;
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @since 1.5
|
||||
*/
|
||||
public class GeoConvertersUnitTests {
|
||||
@@ -153,4 +155,32 @@ public class GeoConvertersUnitTests {
|
||||
assertThat(boxObject,
|
||||
is((Object) Arrays.asList(GeoConverters.toList(box.getFirst()), GeoConverters.toList(box.getSecond()))));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1607
|
||||
public void convertsPointCorrectlyWhenUsingNonDoubleForCoordinates() {
|
||||
|
||||
assertThat(DbObjectToPointConverter.INSTANCE.convert(new BasicDBObject().append("x", 1L).append("y", 2L)),
|
||||
is(new Point(1, 2)));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1607
|
||||
public void convertsCircleCorrectlyWhenUsingNonDoubleForCoordinates() {
|
||||
|
||||
DBObject circle = new BasicDBObject();
|
||||
circle.put("center", new BasicDBObject().append("x", 1).append("y", 2));
|
||||
circle.put("radius", 3L);
|
||||
|
||||
assertThat(DbObjectToCircleConverter.INSTANCE.convert(circle), is(new Circle(new Point(1, 2), new Distance(3))));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1607
|
||||
public void convertsSphereCorrectlyWhenUsingNonDoubleForCoordinates() {
|
||||
|
||||
DBObject sphere = new BasicDBObject();
|
||||
sphere.put("center", new BasicDBObject().append("x", 1).append("y", 2));
|
||||
sphere.put("radius", 3L);
|
||||
|
||||
assertThat(DbObjectToSphereConverter.INSTANCE.convert(sphere), is(new Sphere(new Point(1, 2), new Distance(3))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user