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 //
@@ -464,7 +462,7 @@ public Document convert(GeoCommand source) {
464
462
return null ;
465
463
}
466
464
467
- List argument = new ArrayList ();
465
+ List < Object > argument = new ArrayList <> ();
468
466
469
467
Shape shape = source .getShape ();
470
468
@@ -502,8 +500,7 @@ public Document convert(GeoCommand source) {
502
500
* @author Christoph Strobl
503
501
* @since 1.7
504
502
*/
505
- @ SuppressWarnings ("rawtypes" )
506
- enum GeoJsonToDocumentConverter implements Converter <GeoJson , Document > {
503
+ enum GeoJsonToDocumentConverter implements Converter <GeoJson <?>, Document > {
507
504
508
505
INSTANCE ;
509
506
@@ -512,7 +509,7 @@ enum GeoJsonToDocumentConverter implements Converter<GeoJson, Document> {
512
509
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
513
510
*/
514
511
@ Override
515
- public Document convert (GeoJson source ) {
512
+ public Document convert (GeoJson <?> source ) {
516
513
517
514
if (source == null ) {
518
515
return null ;
@@ -522,33 +519,33 @@ public Document convert(GeoJson source) {
522
519
523
520
if (source instanceof GeoJsonGeometryCollection ) {
524
521
525
- List dbl = new ArrayList ();
522
+ List < Object > dbl = new ArrayList <> ();
526
523
527
- for (GeoJson geometry : ((GeoJsonGeometryCollection ) source ).getCoordinates ()) {
524
+ for (GeoJson <?> geometry : ((GeoJsonGeometryCollection ) source ).getCoordinates ()) {
528
525
dbl .add (convert (geometry ));
529
526
}
530
527
531
528
dbo .put ("geometries" , dbl );
532
529
533
530
} else {
534
- dbo .put ("coordinates" , convertIfNecessarry (source .getCoordinates ()));
531
+ dbo .put ("coordinates" , convertIfNecessary (source .getCoordinates ()));
535
532
}
536
533
537
534
return dbo ;
538
535
}
539
536
540
- private Object convertIfNecessarry (Object candidate ) {
537
+ private Object convertIfNecessary (Object candidate ) {
541
538
542
539
if (candidate instanceof GeoJson ) {
543
- return convertIfNecessarry (((GeoJson ) candidate ).getCoordinates ());
540
+ return convertIfNecessary (((GeoJson <?> ) candidate ).getCoordinates ());
544
541
}
545
542
546
- if (candidate instanceof Iterable ) {
543
+ if (candidate instanceof Iterable <?> ) {
547
544
548
- List dbl = new ArrayList ();
545
+ List < Object > dbl = new ArrayList <> ();
549
546
550
- for (Object element : (Iterable ) candidate ) {
551
- dbl .add (convertIfNecessarry (element ));
547
+ for (Object element : (Iterable <?> ) candidate ) {
548
+ dbl .add (convertIfNecessary (element ));
552
549
}
553
550
554
551
return dbl ;
@@ -648,7 +645,7 @@ public GeoJsonPolygon convert(Document source) {
648
645
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "Polygon" ),
649
646
String .format ("Cannot convert type '%s' to Polygon." , source .get ("type" )));
650
647
651
- return toGeoJsonPolygon ((List ) source .get ("coordinates" ));
648
+ return toGeoJsonPolygon ((List <?> ) source .get ("coordinates" ));
652
649
}
653
650
}
654
651
@@ -674,11 +671,11 @@ public GeoJsonMultiPolygon convert(Document source) {
674
671
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "MultiPolygon" ),
675
672
String .format ("Cannot convert type '%s' to MultiPolygon." , source .get ("type" )));
676
673
677
- List dbl = (List ) source .get ("coordinates" );
674
+ List <?> dbl = (List <?> ) source .get ("coordinates" );
678
675
List <GeoJsonPolygon > polygones = new ArrayList <>();
679
676
680
677
for (Object polygon : dbl ) {
681
- polygones .add (toGeoJsonPolygon ((List ) polygon ));
678
+ polygones .add (toGeoJsonPolygon ((List <?> ) polygon ));
682
679
}
683
680
684
681
return new GeoJsonMultiPolygon (polygones );
@@ -707,7 +704,7 @@ public GeoJsonLineString convert(Document source) {
707
704
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "LineString" ),
708
705
String .format ("Cannot convert type '%s' to LineString." , source .get ("type" )));
709
706
710
- List cords = (List ) source .get ("coordinates" );
707
+ List <?> cords = (List <?> ) source .get ("coordinates" );
711
708
712
709
return new GeoJsonLineString (toListOfPoint (cords ));
713
710
}
@@ -735,7 +732,7 @@ public GeoJsonMultiPoint convert(Document source) {
735
732
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "MultiPoint" ),
736
733
String .format ("Cannot convert type '%s' to MultiPoint." , source .get ("type" )));
737
734
738
- List cords = (List ) source .get ("coordinates" );
735
+ List <?> cords = (List <?> ) source .get ("coordinates" );
739
736
740
737
return new GeoJsonMultiPoint (toListOfPoint (cords ));
741
738
}
@@ -763,11 +760,11 @@ public GeoJsonMultiLineString convert(Document source) {
763
760
Assert .isTrue (ObjectUtils .nullSafeEquals (source .get ("type" ), "MultiLineString" ),
764
761
String .format ("Cannot convert type '%s' to MultiLineString." , source .get ("type" )));
765
762
766
- List <GeoJsonLineString > lines = new ArrayList <GeoJsonLineString >();
767
- List cords = (List ) source .get ("coordinates" );
763
+ List <GeoJsonLineString > lines = new ArrayList <>();
764
+ List <?> cords = (List <?> ) source .get ("coordinates" );
768
765
769
766
for (Object line : cords ) {
770
- lines .add (new GeoJsonLineString (toListOfPoint ((List ) line )));
767
+ lines .add (new GeoJsonLineString (toListOfPoint ((List <?> ) line )));
771
768
}
772
769
return new GeoJsonMultiLineString (lines );
773
770
}
@@ -810,16 +807,16 @@ static List<Double> toList(Point point) {
810
807
}
811
808
812
809
/**
813
- * Converts a coordinate pairs nested in in {@link BasicDBList } into {@link GeoJsonPoint}s.
810
+ * Converts a coordinate pairs nested in {@link List } into {@link GeoJsonPoint}s.
814
811
*
815
812
* @param listOfCoordinatePairs must not be {@literal null}.
816
813
* @return never {@literal null}.
817
814
* @since 1.7
818
815
*/
819
816
@ SuppressWarnings ("unchecked" )
820
- static List <Point > toListOfPoint (List listOfCoordinatePairs ) {
817
+ static List <Point > toListOfPoint (List <?> listOfCoordinatePairs ) {
821
818
822
- List <Point > points = new ArrayList <>();
819
+ List <Point > points = new ArrayList <>(listOfCoordinatePairs . size () );
823
820
824
821
for (Object point : listOfCoordinatePairs ) {
825
822
@@ -834,16 +831,16 @@ static List<Point> toListOfPoint(List listOfCoordinatePairs) {
834
831
}
835
832
836
833
/**
837
- * Converts a coordinate pairs nested in in {@link BasicDBList } into {@link GeoJsonPolygon}.
834
+ * Converts a coordinate pairs nested in {@link List } into {@link GeoJsonPolygon}.
838
835
*
839
836
* @param dbList must not be {@literal null}.
840
837
* @return never {@literal null}.
841
838
* @since 1.7
842
839
*/
843
- static GeoJsonPolygon toGeoJsonPolygon (List dbList ) {
840
+ static GeoJsonPolygon toGeoJsonPolygon (List <?> dbList ) {
844
841
845
- GeoJsonPolygon polygon = new GeoJsonPolygon (toListOfPoint ((List ) dbList .get (0 )));
846
- return dbList .size () > 1 ? polygon .withInnerRing (toListOfPoint ((List ) dbList .get (1 ))) : polygon ;
842
+ GeoJsonPolygon polygon = new GeoJsonPolygon (toListOfPoint ((List <?> ) dbList .get (0 )));
843
+ return dbList .size () > 1 ? polygon .withInnerRing (toListOfPoint ((List <?> ) dbList .get (1 ))) : polygon ;
847
844
}
848
845
849
846
/**
@@ -854,17 +851,11 @@ static GeoJsonPolygon toGeoJsonPolygon(List dbList) {
854
851
* @author Christoph Strobl
855
852
*/
856
853
@ ReadingConverter
857
- enum DocumentToGeoJsonConverter implements Converter <Document , GeoJson > {
854
+ enum DocumentToGeoJsonConverter implements Converter <Document , GeoJson <?> > {
858
855
INSTANCE ;
859
856
860
-
861
- /*
862
- * (non-Javadoc)
863
- * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
864
- */
865
- @ Nullable
866
857
@ Override
867
- public GeoJson convert (Document source ) {
858
+ public GeoJson <?> convert (Document source ) {
868
859
return toGenericGeoJson (source );
869
860
}
870
861
}
@@ -873,22 +864,21 @@ private static GeoJson<?> toGenericGeoJson(Document source) {
873
864
874
865
String type = source .get ("type" , String .class );
875
866
876
- if (type != null ) {
867
+ if (type != null ) {
877
868
878
869
Function <Document , GeoJson <?>> converter = converters .get (type );
879
870
880
- if (converter != null ){
871
+ if (converter != null ) {
881
872
return converter .apply (source );
882
873
}
883
874
}
884
875
885
- throw new IllegalArgumentException (
886
- String .format ("No converter found capable of converting GeoJson type %s." , type ));
876
+ throw new IllegalArgumentException (String .format ("No converter found capable of converting GeoJson type %s." , type ));
887
877
}
888
878
889
879
private static double toPrimitiveDoubleValue (Object value ) {
890
880
891
881
Assert .isInstanceOf (Number .class , value , "Argument must be a Number." );
892
- return NumberUtils .convertNumberToTargetClass ((Number ) value , Double .class ). doubleValue () ;
882
+ return NumberUtils .convertNumberToTargetClass ((Number ) value , Double .class );
893
883
}
894
884
}
0 commit comments