Browse Source

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
pull/371/head
Christoph Strobl 9 years ago
parent
commit
0000a8fd11
  1. 40
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java
  2. 29
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java

40
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java

@ -1,5 +1,5 @@ @@ -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.
@ -42,6 +42,7 @@ import org.springframework.data.mongodb.core.geo.GeoJsonPolygon; @@ -42,6 +42,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,10 +122,7 @@ abstract class GeoConverters { @@ -121,10 +122,7 @@ abstract class GeoConverters {
return DocumentToGeoJsonPointConverter.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")));
}
}
@ -258,10 +256,12 @@ abstract class GeoConverters { @@ -258,10 +256,12 @@ abstract class GeoConverters {
}
Document center = (Document) 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.containsKey("metric")) {
@ -271,9 +271,6 @@ abstract class GeoConverters { @@ -271,9 +271,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(DocumentToPointConverter.INSTANCE.convert(center), distance);
}
}
@ -330,11 +327,12 @@ abstract class GeoConverters { @@ -330,11 +327,12 @@ abstract class GeoConverters {
}
Document center = (Document) 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.containsKey("metric")) {
@ -344,9 +342,6 @@ abstract class GeoConverters { @@ -344,9 +342,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(DocumentToPointConverter.INSTANCE.convert(center), distance);
}
}
@ -606,7 +601,7 @@ abstract class GeoConverters { @@ -606,7 +601,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)));
}
}
@ -840,7 +835,8 @@ abstract class GeoConverters { @@ -840,7 +835,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;
}
@ -855,4 +851,10 @@ abstract class GeoConverters { @@ -855,4 +851,10 @@ abstract class GeoConverters {
static GeoJsonPolygon toGeoJsonPolygon(List dbList) {
return new GeoJsonPolygon(toListOfPoint((List) 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();
}
}

29
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java

@ -48,6 +48,7 @@ import org.springframework.data.mongodb.core.query.GeoCommand; @@ -48,6 +48,7 @@ import org.springframework.data.mongodb.core.query.GeoCommand;
*
* @author Thomas Darimont
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.5
*/
public class GeoConvertersUnitTests {
@ -153,4 +154,32 @@ public class GeoConvertersUnitTests { @@ -153,4 +154,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(DocumentToPointConverter.INSTANCE.convert(new Document().append("x", 1L).append("y", 2L)),
is(new Point(1, 2)));
}
@Test // DATAMONGO-1607
public void convertsCircleCorrectlyWhenUsingNonDoubleForCoordinates() {
Document circle = new Document();
circle.put("center", new Document().append("x", 1).append("y", 2));
circle.put("radius", 3L);
assertThat(DocumentToCircleConverter.INSTANCE.convert(circle), is(new Circle(new Point(1, 2), new Distance(3))));
}
@Test // DATAMONGO-1607
public void convertsSphereCorrectlyWhenUsingNonDoubleForCoordinates() {
Document sphere = new Document();
sphere.put("center", new Document().append("x", 1).append("y", 2));
sphere.put("radius", 3L);
assertThat(DocumentToSphereConverter.INSTANCE.convert(sphere), is(new Sphere(new Point(1, 2), new Distance(3))));
}
}

Loading…
Cancel
Save