1
1
/*
2
- * Copyright 2014-2016 the original author or authors.
2
+ * Copyright 2014-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
41
41
import org .springframework .data .mongodb .core .geo .Sphere ;
42
42
import org .springframework .data .mongodb .core .query .GeoCommand ;
43
43
import org .springframework .util .Assert ;
44
+ import org .springframework .util .NumberUtils ;
44
45
import org .springframework .util .ObjectUtils ;
45
46
46
47
import com .mongodb .BasicDBList ;
@@ -121,11 +122,8 @@ public Point convert(DBObject source) {
121
122
if (source .containsField ("type" )) {
122
123
return DbObjectToGeoJsonPointConverter .INSTANCE .convert (source );
123
124
}
124
-
125
- Number x = (Number ) source .get ("x" );
126
- Number y = (Number ) source .get ("y" );
127
125
128
- return new Point (x . doubleValue (), y . doubleValue ( ));
126
+ return new Point (toPrimitiveDoubleValue ( source . get ( "x" )), toPrimitiveDoubleValue ( source . get ( "y" ) ));
129
127
}
130
128
}
131
129
@@ -259,10 +257,12 @@ public Circle convert(DBObject source) {
259
257
}
260
258
261
259
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" );
264
261
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 ));
266
266
267
267
if (source .containsField ("metric" )) {
268
268
@@ -272,9 +272,6 @@ public Circle convert(DBObject source) {
272
272
distance = distance .in (Metrics .valueOf (metricString ));
273
273
}
274
274
275
- Assert .notNull (center , "Center must not be null!" );
276
- Assert .notNull (radius , "Radius must not be null!" );
277
-
278
275
return new Circle (DbObjectToPointConverter .INSTANCE .convert (center ), distance );
279
276
}
280
277
}
@@ -331,11 +328,12 @@ public Sphere convert(DBObject source) {
331
328
}
332
329
333
330
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" );
336
332
333
+ Assert .notNull (center , "Center must not be null!" );
334
+ Assert .notNull (radius , "Radius must not be null!" );
337
335
338
- Distance distance = new Distance (radius );
336
+ Distance distance = new Distance (toPrimitiveDoubleValue ( radius ) );
339
337
340
338
if (source .containsField ("metric" )) {
341
339
@@ -345,9 +343,6 @@ public Sphere convert(DBObject source) {
345
343
distance = distance .in (Metrics .valueOf (metricString ));
346
344
}
347
345
348
- Assert .notNull (center , "Center must not be null!" );
349
- Assert .notNull (radius , "Radius must not be null!" );
350
-
351
346
return new Sphere (DbObjectToPointConverter .INSTANCE .convert (center ), distance );
352
347
}
353
348
}
@@ -607,7 +602,7 @@ public GeoJsonPoint convert(DBObject source) {
607
602
String .format ("Cannot convert type '%s' to Point." , source .get ("type" )));
608
603
609
604
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 )));
611
606
}
612
607
}
613
608
@@ -841,7 +836,8 @@ static List<Point> toListOfPoint(BasicDBList listOfCoordinatePairs) {
841
836
842
837
List <Number > coordinatesList = (List <Number >) point ;
843
838
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 ))));
845
841
}
846
842
return points ;
847
843
}
@@ -856,4 +852,10 @@ static List<Point> toListOfPoint(BasicDBList listOfCoordinatePairs) {
856
852
static GeoJsonPolygon toGeoJsonPolygon (BasicDBList dbList ) {
857
853
return new GeoJsonPolygon (toListOfPoint ((BasicDBList ) dbList .get (0 )));
858
854
}
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
+ }
859
861
}
0 commit comments