Skip to content

Commit a4782ae

Browse files
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
1 parent 4cc52dc commit a4782ae

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@
4141
import org.springframework.data.mongodb.core.geo.Sphere;
4242
import org.springframework.data.mongodb.core.query.GeoCommand;
4343
import org.springframework.util.Assert;
44+
import org.springframework.util.NumberUtils;
4445
import org.springframework.util.ObjectUtils;
4546

4647
import com.mongodb.BasicDBList;
@@ -121,11 +122,8 @@ public Point convert(DBObject source) {
121122
if (source.containsField("type")) {
122123
return DbObjectToGeoJsonPointConverter.INSTANCE.convert(source);
123124
}
124-
125-
Number x = (Number) source.get("x");
126-
Number y = (Number) source.get("y");
127125

128-
return new Point(x.doubleValue(), y.doubleValue());
126+
return new Point(toPrimitiveDoubleValue(source.get("x")), toPrimitiveDoubleValue(source.get("y")));
129127
}
130128
}
131129

@@ -259,10 +257,12 @@ public Circle convert(DBObject source) {
259257
}
260258

261259
DBObject center = (DBObject) source.get("center");
262-
Number radiusNumber = (Number) source.get("radius");
263-
Double radius = radiusNumber.doubleValue();
260+
Number radius = (Number) source.get("radius");
264261

265-
Distance distance = new Distance(radius);
262+
Assert.notNull(center, "Center must not be null!");
263+
Assert.notNull(radius, "Radius must not be null!");
264+
265+
Distance distance = new Distance(toPrimitiveDoubleValue(radius));
266266

267267
if (source.containsField("metric")) {
268268

@@ -272,9 +272,6 @@ public Circle convert(DBObject source) {
272272
distance = distance.in(Metrics.valueOf(metricString));
273273
}
274274

275-
Assert.notNull(center, "Center must not be null!");
276-
Assert.notNull(radius, "Radius must not be null!");
277-
278275
return new Circle(DbObjectToPointConverter.INSTANCE.convert(center), distance);
279276
}
280277
}
@@ -331,11 +328,12 @@ public Sphere convert(DBObject source) {
331328
}
332329

333330
DBObject center = (DBObject) source.get("center");
334-
Number radiusNumber = (Number) source.get("radius");
335-
Double radius = radiusNumber.doubleValue();
331+
Number radius = (Number) source.get("radius");
336332

333+
Assert.notNull(center, "Center must not be null!");
334+
Assert.notNull(radius, "Radius must not be null!");
337335

338-
Distance distance = new Distance(radius);
336+
Distance distance = new Distance(toPrimitiveDoubleValue(radius));
339337

340338
if (source.containsField("metric")) {
341339

@@ -345,9 +343,6 @@ public Sphere convert(DBObject source) {
345343
distance = distance.in(Metrics.valueOf(metricString));
346344
}
347345

348-
Assert.notNull(center, "Center must not be null!");
349-
Assert.notNull(radius, "Radius must not be null!");
350-
351346
return new Sphere(DbObjectToPointConverter.INSTANCE.convert(center), distance);
352347
}
353348
}
@@ -607,7 +602,7 @@ public GeoJsonPoint convert(DBObject source) {
607602
String.format("Cannot convert type '%s' to Point.", source.get("type")));
608603

609604
List<Number> dbl = (List<Number>) source.get("coordinates");
610-
return new GeoJsonPoint(dbl.get(0).doubleValue(), dbl.get(1).doubleValue());
605+
return new GeoJsonPoint(toPrimitiveDoubleValue(dbl.get(0)), toPrimitiveDoubleValue(dbl.get(1)));
611606
}
612607
}
613608

@@ -841,7 +836,8 @@ static List<Point> toListOfPoint(BasicDBList listOfCoordinatePairs) {
841836

842837
List<Number> coordinatesList = (List<Number>) point;
843838

844-
points.add(new GeoJsonPoint(coordinatesList.get(0).doubleValue(), coordinatesList.get(1).doubleValue()));
839+
points.add(new GeoJsonPoint(toPrimitiveDoubleValue(coordinatesList.get(0)),
840+
toPrimitiveDoubleValue(coordinatesList.get(1))));
845841
}
846842
return points;
847843
}
@@ -856,4 +852,10 @@ static List<Point> toListOfPoint(BasicDBList listOfCoordinatePairs) {
856852
static GeoJsonPolygon toGeoJsonPolygon(BasicDBList dbList) {
857853
return new GeoJsonPolygon(toListOfPoint((BasicDBList) dbList.get(0)));
858854
}
855+
856+
private static double toPrimitiveDoubleValue(Object value) {
857+
858+
Assert.isInstanceOf(Number.class, value, "Argument must be a Number.");
859+
return NumberUtils.convertNumberToTargetClass((Number) value, Double.class).doubleValue();
860+
}
859861
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@
4141
import org.springframework.data.mongodb.core.geo.Sphere;
4242
import org.springframework.data.mongodb.core.query.GeoCommand;
4343

44+
import com.mongodb.BasicDBObject;
4445
import com.mongodb.DBObject;
4546

4647
/**
4748
* Unit tests for {@link GeoConverters}.
4849
*
4950
* @author Thomas Darimont
5051
* @author Oliver Gierke
52+
* @author Christoph Strobl
5153
* @since 1.5
5254
*/
5355
public class GeoConvertersUnitTests {
@@ -153,4 +155,32 @@ public void convertsGeoCommandToDbObjectCorrectly() {
153155
assertThat(boxObject,
154156
is((Object) Arrays.asList(GeoConverters.toList(box.getFirst()), GeoConverters.toList(box.getSecond()))));
155157
}
158+
159+
@Test // DATAMONGO-1607
160+
public void convertsPointCorrectlyWhenUsingNonDoubleForCoordinates() {
161+
162+
assertThat(DbObjectToPointConverter.INSTANCE.convert(new BasicDBObject().append("x", 1L).append("y", 2L)),
163+
is(new Point(1, 2)));
164+
}
165+
166+
@Test // DATAMONGO-1607
167+
public void convertsCircleCorrectlyWhenUsingNonDoubleForCoordinates() {
168+
169+
DBObject circle = new BasicDBObject();
170+
circle.put("center", new BasicDBObject().append("x", 1).append("y", 2));
171+
circle.put("radius", 3L);
172+
173+
assertThat(DbObjectToCircleConverter.INSTANCE.convert(circle), is(new Circle(new Point(1, 2), new Distance(3))));
174+
}
175+
176+
@Test // DATAMONGO-1607
177+
public void convertsSphereCorrectlyWhenUsingNonDoubleForCoordinates() {
178+
179+
DBObject sphere = new BasicDBObject();
180+
sphere.put("center", new BasicDBObject().append("x", 1).append("y", 2));
181+
sphere.put("radius", 3L);
182+
183+
assertThat(DbObjectToSphereConverter.INSTANCE.convert(sphere), is(new Sphere(new Point(1, 2), new Distance(3))));
184+
}
185+
156186
}

0 commit comments

Comments
 (0)