Skip to content

Commit a9d2050

Browse files
committed
Polishing.
Fix generics. Add warning suppressions for nullability checks. See: #4104 Original pull request: #4156.
1 parent 6676389 commit a9d2050

File tree

1 file changed

+35
-41
lines changed
  • spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert

1 file changed

+35
-41
lines changed

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

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.TreeMap;
2525

2626
import org.bson.Document;
27+
2728
import org.springframework.core.convert.converter.Converter;
2829
import org.springframework.data.convert.ReadingConverter;
2930
import org.springframework.data.convert.WritingConverter;
@@ -44,12 +45,10 @@
4445
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
4546
import org.springframework.data.mongodb.core.geo.Sphere;
4647
import org.springframework.data.mongodb.core.query.GeoCommand;
47-
import org.springframework.lang.Nullable;
4848
import org.springframework.util.Assert;
4949
import org.springframework.util.NumberUtils;
5050
import org.springframework.util.ObjectUtils;
5151

52-
import com.mongodb.BasicDBList;
5352
import com.mongodb.Function;
5453

5554
/**
@@ -61,9 +60,9 @@
6160
* @author Thiago Diniz da Silveira
6261
* @since 1.5
6362
*/
63+
@SuppressWarnings("ConstantConditions")
6464
abstract class GeoConverters {
6565

66-
6766
private final static Map<String, Function<Document, GeoJson<?>>> converters;
6867

6968
static {
@@ -93,7 +92,6 @@ private GeoConverters() {}
9392
*
9493
* @return never {@literal null}.
9594
*/
96-
@SuppressWarnings("unchecked")
9795
public static Collection<? extends Object> getConvertersToRegister() {
9896
return Arrays.asList( //
9997
BoxToDocumentConverter.INSTANCE //
@@ -420,7 +418,7 @@ public Document convert(GeoCommand source) {
420418
return null;
421419
}
422420

423-
List argument = new ArrayList();
421+
List<Object> argument = new ArrayList<>();
424422

425423
Shape shape = source.getShape();
426424

@@ -458,13 +456,12 @@ public Document convert(GeoCommand source) {
458456
* @author Christoph Strobl
459457
* @since 1.7
460458
*/
461-
@SuppressWarnings("rawtypes")
462-
enum GeoJsonToDocumentConverter implements Converter<GeoJson, Document> {
459+
enum GeoJsonToDocumentConverter implements Converter<GeoJson<?>, Document> {
463460

464461
INSTANCE;
465462

466463
@Override
467-
public Document convert(GeoJson source) {
464+
public Document convert(GeoJson<?> source) {
468465

469466
if (source == null) {
470467
return null;
@@ -474,33 +471,33 @@ public Document convert(GeoJson source) {
474471

475472
if (source instanceof GeoJsonGeometryCollection) {
476473

477-
List dbl = new ArrayList();
474+
List<Object> dbl = new ArrayList<>();
478475

479-
for (GeoJson geometry : ((GeoJsonGeometryCollection) source).getCoordinates()) {
476+
for (GeoJson<?> geometry : ((GeoJsonGeometryCollection) source).getCoordinates()) {
480477
dbl.add(convert(geometry));
481478
}
482479

483480
dbo.put("geometries", dbl);
484481

485482
} else {
486-
dbo.put("coordinates", convertIfNecessarry(source.getCoordinates()));
483+
dbo.put("coordinates", convertIfNecessary(source.getCoordinates()));
487484
}
488485

489486
return dbo;
490487
}
491488

492-
private Object convertIfNecessarry(Object candidate) {
489+
private Object convertIfNecessary(Object candidate) {
493490

494491
if (candidate instanceof GeoJson) {
495-
return convertIfNecessarry(((GeoJson) candidate).getCoordinates());
492+
return convertIfNecessary(((GeoJson<?>) candidate).getCoordinates());
496493
}
497494

498-
if (candidate instanceof Iterable) {
495+
if (candidate instanceof Iterable<?>) {
499496

500-
List dbl = new ArrayList();
497+
List<Object> dbl = new ArrayList<>();
501498

502-
for (Object element : (Iterable) candidate) {
503-
dbl.add(convertIfNecessarry(element));
499+
for (Object element : (Iterable<?>) candidate) {
500+
dbl.add(convertIfNecessary(element));
504501
}
505502

506503
return dbl;
@@ -584,7 +581,7 @@ public GeoJsonPolygon convert(Document source) {
584581
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "Polygon"),
585582
String.format("Cannot convert type '%s' to Polygon", source.get("type")));
586583

587-
return toGeoJsonPolygon((List) source.get("coordinates"));
584+
return toGeoJsonPolygon((List<?>) source.get("coordinates"));
588585
}
589586
}
590587

@@ -606,11 +603,11 @@ public GeoJsonMultiPolygon convert(Document source) {
606603
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiPolygon"),
607604
String.format("Cannot convert type '%s' to MultiPolygon", source.get("type")));
608605

609-
List dbl = (List) source.get("coordinates");
606+
List<?> dbl = (List<?>) source.get("coordinates");
610607
List<GeoJsonPolygon> polygones = new ArrayList<>();
611608

612609
for (Object polygon : dbl) {
613-
polygones.add(toGeoJsonPolygon((List) polygon));
610+
polygones.add(toGeoJsonPolygon((List<?>) polygon));
614611
}
615612

616613
return new GeoJsonMultiPolygon(polygones);
@@ -635,7 +632,7 @@ public GeoJsonLineString convert(Document source) {
635632
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "LineString"),
636633
String.format("Cannot convert type '%s' to LineString", source.get("type")));
637634

638-
List cords = (List) source.get("coordinates");
635+
List<?> cords = (List<?>) source.get("coordinates");
639636

640637
return new GeoJsonLineString(toListOfPoint(cords));
641638
}
@@ -659,7 +656,7 @@ public GeoJsonMultiPoint convert(Document source) {
659656
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiPoint"),
660657
String.format("Cannot convert type '%s' to MultiPoint", source.get("type")));
661658

662-
List cords = (List) source.get("coordinates");
659+
List<?> cords = (List<?>) source.get("coordinates");
663660

664661
return new GeoJsonMultiPoint(toListOfPoint(cords));
665662
}
@@ -683,11 +680,11 @@ public GeoJsonMultiLineString convert(Document source) {
683680
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiLineString"),
684681
String.format("Cannot convert type '%s' to MultiLineString", source.get("type")));
685682

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");
688685

689686
for (Object line : cords) {
690-
lines.add(new GeoJsonLineString(toListOfPoint((List) line)));
687+
lines.add(new GeoJsonLineString(toListOfPoint((List<?>) line)));
691688
}
692689
return new GeoJsonMultiLineString(lines);
693690
}
@@ -726,16 +723,16 @@ static List<Double> toList(Point point) {
726723
}
727724

728725
/**
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.
730727
*
731728
* @param listOfCoordinatePairs must not be {@literal null}.
732729
* @return never {@literal null}.
733730
* @since 1.7
734731
*/
735732
@SuppressWarnings("unchecked")
736-
static List<Point> toListOfPoint(List listOfCoordinatePairs) {
733+
static List<Point> toListOfPoint(List<?> listOfCoordinatePairs) {
737734

738-
List<Point> points = new ArrayList<>();
735+
List<Point> points = new ArrayList<>(listOfCoordinatePairs.size());
739736

740737
for (Object point : listOfCoordinatePairs) {
741738

@@ -750,16 +747,16 @@ static List<Point> toListOfPoint(List listOfCoordinatePairs) {
750747
}
751748

752749
/**
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}.
754751
*
755752
* @param dbList must not be {@literal null}.
756753
* @return never {@literal null}.
757754
* @since 1.7
758755
*/
759-
static GeoJsonPolygon toGeoJsonPolygon(List dbList) {
756+
static GeoJsonPolygon toGeoJsonPolygon(List<?> dbList) {
760757

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;
763760
}
764761

765762
/**
@@ -770,13 +767,11 @@ static GeoJsonPolygon toGeoJsonPolygon(List dbList) {
770767
* @author Christoph Strobl
771768
*/
772769
@ReadingConverter
773-
enum DocumentToGeoJsonConverter implements Converter<Document, GeoJson> {
770+
enum DocumentToGeoJsonConverter implements Converter<Document, GeoJson<?>> {
774771
INSTANCE;
775772

776-
777-
@Nullable
778773
@Override
779-
public GeoJson convert(Document source) {
774+
public GeoJson<?> convert(Document source) {
780775
return toGenericGeoJson(source);
781776
}
782777
}
@@ -785,22 +780,21 @@ private static GeoJson<?> toGenericGeoJson(Document source) {
785780

786781
String type = source.get("type", String.class);
787782

788-
if(type != null) {
783+
if (type != null) {
789784

790785
Function<Document, GeoJson<?>> converter = converters.get(type);
791786

792-
if(converter != null){
787+
if (converter != null) {
793788
return converter.apply(source);
794789
}
795790
}
796791

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));
799793
}
800794

801795
private static double toPrimitiveDoubleValue(Object value) {
802796

803797
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);
805799
}
806800
}

0 commit comments

Comments
 (0)