Browse Source

Polishing.

Fix generics. Add warning suppressions for nullability checks.

See: #4104
Original pull request: #4156.
3.3.x
Mark Paluch 3 years ago
parent
commit
7c7b05f10d
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 80
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java

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

@ -24,6 +24,7 @@ import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import org.bson.Document; import org.bson.Document;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter; import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter; import org.springframework.data.convert.WritingConverter;
@ -44,12 +45,10 @@ import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon; import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
import org.springframework.data.mongodb.core.geo.Sphere; import org.springframework.data.mongodb.core.geo.Sphere;
import org.springframework.data.mongodb.core.query.GeoCommand; import org.springframework.data.mongodb.core.query.GeoCommand;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.NumberUtils; import org.springframework.util.NumberUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import com.mongodb.BasicDBList;
import com.mongodb.Function; import com.mongodb.Function;
/** /**
@ -61,9 +60,9 @@ import com.mongodb.Function;
* @author Thiago Diniz da Silveira * @author Thiago Diniz da Silveira
* @since 1.5 * @since 1.5
*/ */
@SuppressWarnings("ConstantConditions")
abstract class GeoConverters { abstract class GeoConverters {
private final static Map<String, Function<Document, GeoJson<?>>> converters; private final static Map<String, Function<Document, GeoJson<?>>> converters;
static { static {
@ -93,7 +92,6 @@ abstract class GeoConverters {
* *
* @return never {@literal null}. * @return never {@literal null}.
*/ */
@SuppressWarnings("unchecked")
public static Collection<? extends Object> getConvertersToRegister() { public static Collection<? extends Object> getConvertersToRegister() {
return Arrays.asList( // return Arrays.asList( //
BoxToDocumentConverter.INSTANCE // BoxToDocumentConverter.INSTANCE //
@ -464,7 +462,7 @@ abstract class GeoConverters {
return null; return null;
} }
List argument = new ArrayList(); List<Object> argument = new ArrayList<>();
Shape shape = source.getShape(); Shape shape = source.getShape();
@ -502,8 +500,7 @@ abstract class GeoConverters {
* @author Christoph Strobl * @author Christoph Strobl
* @since 1.7 * @since 1.7
*/ */
@SuppressWarnings("rawtypes") enum GeoJsonToDocumentConverter implements Converter<GeoJson<?>, Document> {
enum GeoJsonToDocumentConverter implements Converter<GeoJson, Document> {
INSTANCE; INSTANCE;
@ -512,7 +509,7 @@ abstract class GeoConverters {
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object) * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/ */
@Override @Override
public Document convert(GeoJson source) { public Document convert(GeoJson<?> source) {
if (source == null) { if (source == null) {
return null; return null;
@ -522,33 +519,33 @@ abstract class GeoConverters {
if (source instanceof GeoJsonGeometryCollection) { if (source instanceof GeoJsonGeometryCollection) {
List dbl = new ArrayList(); List<Object> dbl = new ArrayList<>();
for (GeoJson geometry : ((GeoJsonGeometryCollection) source).getCoordinates()) { for (GeoJson<?> geometry : ((GeoJsonGeometryCollection) source).getCoordinates()) {
dbl.add(convert(geometry)); dbl.add(convert(geometry));
} }
dbo.put("geometries", dbl); dbo.put("geometries", dbl);
} else { } else {
dbo.put("coordinates", convertIfNecessarry(source.getCoordinates())); dbo.put("coordinates", convertIfNecessary(source.getCoordinates()));
} }
return dbo; return dbo;
} }
private Object convertIfNecessarry(Object candidate) { private Object convertIfNecessary(Object candidate) {
if (candidate instanceof GeoJson) { if (candidate instanceof GeoJson) {
return convertIfNecessarry(((GeoJson) candidate).getCoordinates()); return convertIfNecessary(((GeoJson<?>) candidate).getCoordinates());
} }
if (candidate instanceof Iterable) { if (candidate instanceof Iterable<?>) {
List dbl = new ArrayList(); List<Object> dbl = new ArrayList<>();
for (Object element : (Iterable) candidate) { for (Object element : (Iterable<?>) candidate) {
dbl.add(convertIfNecessarry(element)); dbl.add(convertIfNecessary(element));
} }
return dbl; return dbl;
@ -648,7 +645,7 @@ abstract class GeoConverters {
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "Polygon"), Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "Polygon"),
String.format("Cannot convert type '%s' to Polygon.", source.get("type"))); String.format("Cannot convert type '%s' to Polygon.", source.get("type")));
return toGeoJsonPolygon((List) source.get("coordinates")); return toGeoJsonPolygon((List<?>) source.get("coordinates"));
} }
} }
@ -674,11 +671,11 @@ abstract class GeoConverters {
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiPolygon"), Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiPolygon"),
String.format("Cannot convert type '%s' to MultiPolygon.", source.get("type"))); String.format("Cannot convert type '%s' to MultiPolygon.", source.get("type")));
List dbl = (List) source.get("coordinates"); List<?> dbl = (List<?>) source.get("coordinates");
List<GeoJsonPolygon> polygones = new ArrayList<>(); List<GeoJsonPolygon> polygones = new ArrayList<>();
for (Object polygon : dbl) { for (Object polygon : dbl) {
polygones.add(toGeoJsonPolygon((List) polygon)); polygones.add(toGeoJsonPolygon((List<?>) polygon));
} }
return new GeoJsonMultiPolygon(polygones); return new GeoJsonMultiPolygon(polygones);
@ -707,7 +704,7 @@ abstract class GeoConverters {
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "LineString"), Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "LineString"),
String.format("Cannot convert type '%s' to LineString.", source.get("type"))); String.format("Cannot convert type '%s' to LineString.", source.get("type")));
List cords = (List) source.get("coordinates"); List<?> cords = (List<?>) source.get("coordinates");
return new GeoJsonLineString(toListOfPoint(cords)); return new GeoJsonLineString(toListOfPoint(cords));
} }
@ -735,7 +732,7 @@ abstract class GeoConverters {
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiPoint"), Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiPoint"),
String.format("Cannot convert type '%s' to MultiPoint.", source.get("type"))); String.format("Cannot convert type '%s' to MultiPoint.", source.get("type")));
List cords = (List) source.get("coordinates"); List<?> cords = (List<?>) source.get("coordinates");
return new GeoJsonMultiPoint(toListOfPoint(cords)); return new GeoJsonMultiPoint(toListOfPoint(cords));
} }
@ -763,11 +760,11 @@ abstract class GeoConverters {
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiLineString"), Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "MultiLineString"),
String.format("Cannot convert type '%s' to MultiLineString.", source.get("type"))); String.format("Cannot convert type '%s' to MultiLineString.", source.get("type")));
List<GeoJsonLineString> lines = new ArrayList<GeoJsonLineString>(); List<GeoJsonLineString> lines = new ArrayList<>();
List cords = (List) source.get("coordinates"); List<?> cords = (List<?>) source.get("coordinates");
for (Object line : cords) { for (Object line : cords) {
lines.add(new GeoJsonLineString(toListOfPoint((List) line))); lines.add(new GeoJsonLineString(toListOfPoint((List<?>) line)));
} }
return new GeoJsonMultiLineString(lines); return new GeoJsonMultiLineString(lines);
} }
@ -810,16 +807,16 @@ abstract class GeoConverters {
} }
/** /**
* Converts a coordinate pairs nested in in {@link BasicDBList} into {@link GeoJsonPoint}s. * Converts a coordinate pairs nested in {@link List} into {@link GeoJsonPoint}s.
* *
* @param listOfCoordinatePairs must not be {@literal null}. * @param listOfCoordinatePairs must not be {@literal null}.
* @return never {@literal null}. * @return never {@literal null}.
* @since 1.7 * @since 1.7
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static List<Point> toListOfPoint(List listOfCoordinatePairs) { static List<Point> toListOfPoint(List<?> listOfCoordinatePairs) {
List<Point> points = new ArrayList<>(); List<Point> points = new ArrayList<>(listOfCoordinatePairs.size());
for (Object point : listOfCoordinatePairs) { for (Object point : listOfCoordinatePairs) {
@ -834,16 +831,16 @@ abstract class GeoConverters {
} }
/** /**
* Converts a coordinate pairs nested in in {@link BasicDBList} into {@link GeoJsonPolygon}. * Converts a coordinate pairs nested in {@link List} into {@link GeoJsonPolygon}.
* *
* @param dbList must not be {@literal null}. * @param dbList must not be {@literal null}.
* @return never {@literal null}. * @return never {@literal null}.
* @since 1.7 * @since 1.7
*/ */
static GeoJsonPolygon toGeoJsonPolygon(List dbList) { static GeoJsonPolygon toGeoJsonPolygon(List<?> dbList) {
GeoJsonPolygon polygon = new GeoJsonPolygon(toListOfPoint((List) dbList.get(0))); GeoJsonPolygon polygon = new GeoJsonPolygon(toListOfPoint((List<?>) dbList.get(0)));
return dbList.size() > 1 ? polygon.withInnerRing(toListOfPoint((List) dbList.get(1))) : polygon; return dbList.size() > 1 ? polygon.withInnerRing(toListOfPoint((List<?>) dbList.get(1))) : polygon;
} }
/** /**
@ -854,17 +851,11 @@ abstract class GeoConverters {
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@ReadingConverter @ReadingConverter
enum DocumentToGeoJsonConverter implements Converter<Document, GeoJson> { enum DocumentToGeoJsonConverter implements Converter<Document, GeoJson<?>> {
INSTANCE; INSTANCE;
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@Nullable
@Override @Override
public GeoJson convert(Document source) { public GeoJson<?> convert(Document source) {
return toGenericGeoJson(source); return toGenericGeoJson(source);
} }
} }
@ -873,22 +864,21 @@ abstract class GeoConverters {
String type = source.get("type", String.class); String type = source.get("type", String.class);
if(type != null) { if (type != null) {
Function<Document, GeoJson<?>> converter = converters.get(type); Function<Document, GeoJson<?>> converter = converters.get(type);
if(converter != null){ if (converter != null) {
return converter.apply(source); return converter.apply(source);
} }
} }
throw new IllegalArgumentException( throw new IllegalArgumentException(String.format("No converter found capable of converting GeoJson type %s.", type));
String.format("No converter found capable of converting GeoJson type %s.", type));
} }
private static double toPrimitiveDoubleValue(Object value) { private static double toPrimitiveDoubleValue(Object value) {
Assert.isInstanceOf(Number.class, value, "Argument must be a Number."); Assert.isInstanceOf(Number.class, value, "Argument must be a Number.");
return NumberUtils.convertNumberToTargetClass((Number) value, Double.class).doubleValue(); return NumberUtils.convertNumberToTargetClass((Number) value, Double.class);
} }
} }

Loading…
Cancel
Save