24
24
import java .util .TreeMap ;
25
25
26
26
import org .bson .Document ;
27
+
27
28
import org .springframework .core .convert .converter .Converter ;
28
29
import org .springframework .data .convert .ReadingConverter ;
29
30
import org .springframework .data .convert .WritingConverter ;
44
45
import org .springframework .data .mongodb .core .geo .GeoJsonPolygon ;
45
46
import org .springframework .data .mongodb .core .geo .Sphere ;
46
47
import org .springframework .data .mongodb .core .query .GeoCommand ;
47
- import org .springframework .lang .Nullable ;
48
48
import org .springframework .util .Assert ;
49
49
import org .springframework .util .NumberUtils ;
50
50
import org .springframework .util .ObjectUtils ;
51
51
52
- import com .mongodb .BasicDBList ;
53
52
import com .mongodb .Function ;
54
53
55
54
/**
61
60
* @author Thiago Diniz da Silveira
62
61
* @since 1.5
63
62
*/
63
+ @ SuppressWarnings ("ConstantConditions" )
64
64
abstract class GeoConverters {
65
65
66
-
67
66
private final static Map <String , Function <Document , GeoJson <?>>> converters ;
68
67
69
68
static {
@@ -93,7 +92,6 @@ private GeoConverters() {}
93
92
*
94
93
* @return never {@literal null}.
95
94
*/
96
- @ SuppressWarnings ("unchecked" )
97
95
public static Collection <? extends Object > getConvertersToRegister () {
98
96
return Arrays .asList ( //
99
97
BoxToDocumentConverter .INSTANCE //
@@ -420,7 +418,7 @@ public Document convert(GeoCommand source) {
420
418
return null ;
421
419
}
422
420
423
- List argument = new ArrayList ();
421
+ List < Object > argument = new ArrayList <> ();
424
422
425
423
Shape shape = source .getShape ();
426
424
@@ -458,13 +456,12 @@ public Document convert(GeoCommand source) {
458
456
* @author Christoph Strobl
459
457
* @since 1.7
460
458
*/
461
- @ SuppressWarnings ("rawtypes" )
462
- enum GeoJsonToDocumentConverter implements Converter <GeoJson , Document > {
459
+ enum GeoJsonToDocumentConverter implements Converter <GeoJson <?>, Document > {
463
460
464
461
INSTANCE ;
465
462
466
463
@ Override
467
- public Document convert (GeoJson source ) {
464
+ public Document convert (GeoJson <?> source ) {
468
465
469
466
if (source == null ) {
470
467
return null ;
@@ -474,33 +471,33 @@ public Document convert(GeoJson source) {
474
471
475
472
if (source instanceof GeoJsonGeometryCollection ) {
476
473
477
- List dbl = new ArrayList ();
474
+ List < Object > dbl = new ArrayList <> ();
478
475
479
- for (GeoJson geometry : ((GeoJsonGeometryCollection ) source ).getCoordinates ()) {
476
+ for (GeoJson <?> geometry : ((GeoJsonGeometryCollection ) source ).getCoordinates ()) {
480
477
dbl .add (convert (geometry ));
481
478
}
482
479
483
480
dbo .put ("geometries" , dbl );
484
481
485
482
} else {
486
- dbo .put ("coordinates" , convertIfNecessarry (source .getCoordinates ()));
483
+ dbo .put ("coordinates" , convertIfNecessary (source .getCoordinates ()));
487
484
}
488
485
489
486
return dbo ;
490
487
}
491
488
492
- private Object convertIfNecessarry (Object candidate ) {
489
+ private Object convertIfNecessary (Object candidate ) {
493
490
494
491
if (candidate instanceof GeoJson ) {
495
- return convertIfNecessarry (((GeoJson ) candidate ).getCoordinates ());
492
+ return convertIfNecessary (((GeoJson <?> ) candidate ).getCoordinates ());
496
493
}
497
494
498
- if (candidate instanceof Iterable ) {
495
+ if (candidate instanceof Iterable <?> ) {
499
496
500
- List dbl = new ArrayList ();
497
+ List < Object > dbl = new ArrayList <> ();
501
498
502
- for (Object element : (Iterable ) candidate ) {
503
- dbl .add (convertIfNecessarry (element ));
499
+ for (Object element : (Iterable <?> ) candidate ) {
500
+ dbl .add (convertIfNecessary (element ));
504
501
}
505
502
506
503
return dbl ;
@@ -584,7 +581,7 @@ public GeoJsonPolygon convert(Document source) {
584
581
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "Polygon" ),
585
582
String .format ("Cannot convert type '%s' to Polygon" , source .get ("type" )));
586
583
587
- return toGeoJsonPolygon ((List ) source .get ("coordinates" ));
584
+ return toGeoJsonPolygon ((List <?> ) source .get ("coordinates" ));
588
585
}
589
586
}
590
587
@@ -606,11 +603,11 @@ public GeoJsonMultiPolygon convert(Document source) {
606
603
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "MultiPolygon" ),
607
604
String .format ("Cannot convert type '%s' to MultiPolygon" , source .get ("type" )));
608
605
609
- List dbl = (List ) source .get ("coordinates" );
606
+ List <?> dbl = (List <?> ) source .get ("coordinates" );
610
607
List <GeoJsonPolygon > polygones = new ArrayList <>();
611
608
612
609
for (Object polygon : dbl ) {
613
- polygones .add (toGeoJsonPolygon ((List ) polygon ));
610
+ polygones .add (toGeoJsonPolygon ((List <?> ) polygon ));
614
611
}
615
612
616
613
return new GeoJsonMultiPolygon (polygones );
@@ -635,7 +632,7 @@ public GeoJsonLineString convert(Document source) {
635
632
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "LineString" ),
636
633
String .format ("Cannot convert type '%s' to LineString" , source .get ("type" )));
637
634
638
- List cords = (List ) source .get ("coordinates" );
635
+ List <?> cords = (List <?> ) source .get ("coordinates" );
639
636
640
637
return new GeoJsonLineString (toListOfPoint (cords ));
641
638
}
@@ -659,7 +656,7 @@ public GeoJsonMultiPoint convert(Document source) {
659
656
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "MultiPoint" ),
660
657
String .format ("Cannot convert type '%s' to MultiPoint" , source .get ("type" )));
661
658
662
- List cords = (List ) source .get ("coordinates" );
659
+ List <?> cords = (List <?> ) source .get ("coordinates" );
663
660
664
661
return new GeoJsonMultiPoint (toListOfPoint (cords ));
665
662
}
@@ -683,11 +680,11 @@ public GeoJsonMultiLineString convert(Document source) {
683
680
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "MultiLineString" ),
684
681
String .format ("Cannot convert type '%s' to MultiLineString" , source .get ("type" )));
685
682
686
- List <GeoJsonLineString > lines = new ArrayList <GeoJsonLineString >();
687
- List cords = (List ) source .get ("coordinates" );
683
+ List <GeoJsonLineString > lines = new ArrayList <>();
684
+ List <?> cords = (List <?> ) source .get ("coordinates" );
688
685
689
686
for (Object line : cords ) {
690
- lines .add (new GeoJsonLineString (toListOfPoint ((List ) line )));
687
+ lines .add (new GeoJsonLineString (toListOfPoint ((List <?> ) line )));
691
688
}
692
689
return new GeoJsonMultiLineString (lines );
693
690
}
@@ -726,16 +723,16 @@ static List<Double> toList(Point point) {
726
723
}
727
724
728
725
/**
729
- * Converts a coordinate pairs nested in in {@link BasicDBList } into {@link GeoJsonPoint}s.
726
+ * Converts a coordinate pairs nested in {@link List } into {@link GeoJsonPoint}s.
730
727
*
731
728
* @param listOfCoordinatePairs must not be {@literal null}.
732
729
* @return never {@literal null}.
733
730
* @since 1.7
734
731
*/
735
732
@ SuppressWarnings ("unchecked" )
736
- static List <Point > toListOfPoint (List listOfCoordinatePairs ) {
733
+ static List <Point > toListOfPoint (List <?> listOfCoordinatePairs ) {
737
734
738
- List <Point > points = new ArrayList <>();
735
+ List <Point > points = new ArrayList <>(listOfCoordinatePairs . size () );
739
736
740
737
for (Object point : listOfCoordinatePairs ) {
741
738
@@ -750,16 +747,16 @@ static List<Point> toListOfPoint(List listOfCoordinatePairs) {
750
747
}
751
748
752
749
/**
753
- * Converts a coordinate pairs nested in in {@link BasicDBList } into {@link GeoJsonPolygon}.
750
+ * Converts a coordinate pairs nested in {@link List } into {@link GeoJsonPolygon}.
754
751
*
755
752
* @param dbList must not be {@literal null}.
756
753
* @return never {@literal null}.
757
754
* @since 1.7
758
755
*/
759
- static GeoJsonPolygon toGeoJsonPolygon (List dbList ) {
756
+ static GeoJsonPolygon toGeoJsonPolygon (List <?> dbList ) {
760
757
761
- GeoJsonPolygon polygon = new GeoJsonPolygon (toListOfPoint ((List ) dbList .get (0 )));
762
- return dbList .size () > 1 ? polygon .withInnerRing (toListOfPoint ((List ) dbList .get (1 ))) : polygon ;
758
+ GeoJsonPolygon polygon = new GeoJsonPolygon (toListOfPoint ((List <?> ) dbList .get (0 )));
759
+ return dbList .size () > 1 ? polygon .withInnerRing (toListOfPoint ((List <?> ) dbList .get (1 ))) : polygon ;
763
760
}
764
761
765
762
/**
@@ -770,13 +767,11 @@ static GeoJsonPolygon toGeoJsonPolygon(List dbList) {
770
767
* @author Christoph Strobl
771
768
*/
772
769
@ ReadingConverter
773
- enum DocumentToGeoJsonConverter implements Converter <Document , GeoJson > {
770
+ enum DocumentToGeoJsonConverter implements Converter <Document , GeoJson <?> > {
774
771
INSTANCE ;
775
772
776
-
777
- @ Nullable
778
773
@ Override
779
- public GeoJson convert (Document source ) {
774
+ public GeoJson <?> convert (Document source ) {
780
775
return toGenericGeoJson (source );
781
776
}
782
777
}
@@ -785,22 +780,21 @@ private static GeoJson<?> toGenericGeoJson(Document source) {
785
780
786
781
String type = source .get ("type" , String .class );
787
782
788
- if (type != null ) {
783
+ if (type != null ) {
789
784
790
785
Function <Document , GeoJson <?>> converter = converters .get (type );
791
786
792
- if (converter != null ){
787
+ if (converter != null ) {
793
788
return converter .apply (source );
794
789
}
795
790
}
796
791
797
- throw new IllegalArgumentException (
798
- String .format ("No converter found capable of converting GeoJson type %s" , type ));
792
+ throw new IllegalArgumentException (String .format ("No converter found capable of converting GeoJson type %s" , type ));
799
793
}
800
794
801
795
private static double toPrimitiveDoubleValue (Object value ) {
802
796
803
797
Assert .isInstanceOf (Number .class , value , "Argument must be a Number" );
804
- return NumberUtils .convertNumberToTargetClass ((Number ) value , Double .class ). doubleValue () ;
798
+ return NumberUtils .convertNumberToTargetClass ((Number ) value , Double .class );
805
799
}
806
800
}
0 commit comments