Skip to content

Commit 7c7b05f

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

File tree

1 file changed

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

1 file changed

+35
-45
lines changed

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

+35-45
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 //
@@ -464,7 +462,7 @@ public Document convert(GeoCommand source) {
464462
return null;
465463
}
466464

467-
List argument = new ArrayList();
465+
List<Object> argument = new ArrayList<>();
468466

469467
Shape shape = source.getShape();
470468

@@ -502,8 +500,7 @@ public Document convert(GeoCommand source) {
502500
* @author Christoph Strobl
503501
* @since 1.7
504502
*/
505-
@SuppressWarnings("rawtypes")
506-
enum GeoJsonToDocumentConverter implements Converter<GeoJson, Document> {
503+
enum GeoJsonToDocumentConverter implements Converter<GeoJson<?>, Document> {
507504

508505
INSTANCE;
509506

@@ -512,7 +509,7 @@ enum GeoJsonToDocumentConverter implements Converter<GeoJson, Document> {
512509
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
513510
*/
514511
@Override
515-
public Document convert(GeoJson source) {
512+
public Document convert(GeoJson<?> source) {
516513

517514
if (source == null) {
518515
return null;
@@ -522,33 +519,33 @@ public Document convert(GeoJson source) {
522519

523520
if (source instanceof GeoJsonGeometryCollection) {
524521

525-
List dbl = new ArrayList();
522+
List<Object> dbl = new ArrayList<>();
526523

527-
for (GeoJson geometry : ((GeoJsonGeometryCollection) source).getCoordinates()) {
524+
for (GeoJson<?> geometry : ((GeoJsonGeometryCollection) source).getCoordinates()) {
528525
dbl.add(convert(geometry));
529526
}
530527

531528
dbo.put("geometries", dbl);
532529

533530
} else {
534-
dbo.put("coordinates", convertIfNecessarry(source.getCoordinates()));
531+
dbo.put("coordinates", convertIfNecessary(source.getCoordinates()));
535532
}
536533

537534
return dbo;
538535
}
539536

540-
private Object convertIfNecessarry(Object candidate) {
537+
private Object convertIfNecessary(Object candidate) {
541538

542539
if (candidate instanceof GeoJson) {
543-
return convertIfNecessarry(((GeoJson) candidate).getCoordinates());
540+
return convertIfNecessary(((GeoJson<?>) candidate).getCoordinates());
544541
}
545542

546-
if (candidate instanceof Iterable) {
543+
if (candidate instanceof Iterable<?>) {
547544

548-
List dbl = new ArrayList();
545+
List<Object> dbl = new ArrayList<>();
549546

550-
for (Object element : (Iterable) candidate) {
551-
dbl.add(convertIfNecessarry(element));
547+
for (Object element : (Iterable<?>) candidate) {
548+
dbl.add(convertIfNecessary(element));
552549
}
553550

554551
return dbl;
@@ -648,7 +645,7 @@ public GeoJsonPolygon convert(Document source) {
648645
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "Polygon"),
649646
String.format("Cannot convert type '%s' to Polygon.", source.get("type")));
650647

651-
return toGeoJsonPolygon((List) source.get("coordinates"));
648+
return toGeoJsonPolygon((List<?>) source.get("coordinates"));
652649
}
653650
}
654651

@@ -674,11 +671,11 @@ public GeoJsonMultiPolygon convert(Document source) {
674671
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiPolygon"),
675672
String.format("Cannot convert type '%s' to MultiPolygon.", source.get("type")));
676673

677-
List dbl = (List) source.get("coordinates");
674+
List<?> dbl = (List<?>) source.get("coordinates");
678675
List<GeoJsonPolygon> polygones = new ArrayList<>();
679676

680677
for (Object polygon : dbl) {
681-
polygones.add(toGeoJsonPolygon((List) polygon));
678+
polygones.add(toGeoJsonPolygon((List<?>) polygon));
682679
}
683680

684681
return new GeoJsonMultiPolygon(polygones);
@@ -707,7 +704,7 @@ public GeoJsonLineString convert(Document source) {
707704
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "LineString"),
708705
String.format("Cannot convert type '%s' to LineString.", source.get("type")));
709706

710-
List cords = (List) source.get("coordinates");
707+
List<?> cords = (List<?>) source.get("coordinates");
711708

712709
return new GeoJsonLineString(toListOfPoint(cords));
713710
}
@@ -735,7 +732,7 @@ public GeoJsonMultiPoint convert(Document source) {
735732
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiPoint"),
736733
String.format("Cannot convert type '%s' to MultiPoint.", source.get("type")));
737734

738-
List cords = (List) source.get("coordinates");
735+
List<?> cords = (List<?>) source.get("coordinates");
739736

740737
return new GeoJsonMultiPoint(toListOfPoint(cords));
741738
}
@@ -763,11 +760,11 @@ public GeoJsonMultiLineString convert(Document source) {
763760
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiLineString"),
764761
String.format("Cannot convert type '%s' to MultiLineString.", source.get("type")));
765762

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

769766
for (Object line : cords) {
770-
lines.add(new GeoJsonLineString(toListOfPoint((List) line)));
767+
lines.add(new GeoJsonLineString(toListOfPoint((List<?>) line)));
771768
}
772769
return new GeoJsonMultiLineString(lines);
773770
}
@@ -810,16 +807,16 @@ static List<Double> toList(Point point) {
810807
}
811808

812809
/**
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.
814811
*
815812
* @param listOfCoordinatePairs must not be {@literal null}.
816813
* @return never {@literal null}.
817814
* @since 1.7
818815
*/
819816
@SuppressWarnings("unchecked")
820-
static List<Point> toListOfPoint(List listOfCoordinatePairs) {
817+
static List<Point> toListOfPoint(List<?> listOfCoordinatePairs) {
821818

822-
List<Point> points = new ArrayList<>();
819+
List<Point> points = new ArrayList<>(listOfCoordinatePairs.size());
823820

824821
for (Object point : listOfCoordinatePairs) {
825822

@@ -834,16 +831,16 @@ static List<Point> toListOfPoint(List listOfCoordinatePairs) {
834831
}
835832

836833
/**
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}.
838835
*
839836
* @param dbList must not be {@literal null}.
840837
* @return never {@literal null}.
841838
* @since 1.7
842839
*/
843-
static GeoJsonPolygon toGeoJsonPolygon(List dbList) {
840+
static GeoJsonPolygon toGeoJsonPolygon(List<?> dbList) {
844841

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;
847844
}
848845

849846
/**
@@ -854,17 +851,11 @@ static GeoJsonPolygon toGeoJsonPolygon(List dbList) {
854851
* @author Christoph Strobl
855852
*/
856853
@ReadingConverter
857-
enum DocumentToGeoJsonConverter implements Converter<Document, GeoJson> {
854+
enum DocumentToGeoJsonConverter implements Converter<Document, GeoJson<?>> {
858855
INSTANCE;
859856

860-
861-
/*
862-
* (non-Javadoc)
863-
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
864-
*/
865-
@Nullable
866857
@Override
867-
public GeoJson convert(Document source) {
858+
public GeoJson<?> convert(Document source) {
868859
return toGenericGeoJson(source);
869860
}
870861
}
@@ -873,22 +864,21 @@ private static GeoJson<?> toGenericGeoJson(Document source) {
873864

874865
String type = source.get("type", String.class);
875866

876-
if(type != null) {
867+
if (type != null) {
877868

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

880-
if(converter != null){
871+
if (converter != null) {
881872
return converter.apply(source);
882873
}
883874
}
884875

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));
887877
}
888878

889879
private static double toPrimitiveDoubleValue(Object value) {
890880

891881
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);
893883
}
894884
}

0 commit comments

Comments
 (0)