DATAMONGO-1466 - Polishing.
Switch conditionals to Map-based Function registry to pick the appropriate converter. Fix typos in method names. Original pull request: #561.
This commit is contained in:
@@ -15,10 +15,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.mongodb.core.convert;
|
package org.springframework.data.mongodb.core.convert;
|
||||||
|
|
||||||
|
import java.text.Collator;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
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;
|
||||||
@@ -47,6 +50,7 @@ import org.springframework.util.NumberUtils;
|
|||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
import com.mongodb.BasicDBList;
|
import com.mongodb.BasicDBList;
|
||||||
|
import com.mongodb.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper class to contain useful geo structure converters for the usage with Mongo.
|
* Wrapper class to contain useful geo structure converters for the usage with Mongo.
|
||||||
@@ -59,6 +63,26 @@ import com.mongodb.BasicDBList;
|
|||||||
*/
|
*/
|
||||||
abstract class GeoConverters {
|
abstract class GeoConverters {
|
||||||
|
|
||||||
|
|
||||||
|
private final static Map<String, Function<Document, GeoJson<?>>> converters;
|
||||||
|
|
||||||
|
static {
|
||||||
|
|
||||||
|
Collator caseInsensitive = Collator.getInstance();
|
||||||
|
caseInsensitive.setStrength(Collator.PRIMARY);
|
||||||
|
|
||||||
|
Map<String, Function<Document, GeoJson<?>>> geoConverters = new TreeMap<>(caseInsensitive);
|
||||||
|
geoConverters.put("point", DocumentToGeoJsonPointConverter.INSTANCE::convert);
|
||||||
|
geoConverters.put("multipoint", DocumentToGeoJsonMultiPointConverter.INSTANCE::convert);
|
||||||
|
geoConverters.put("linestring", DocumentToGeoJsonLineStringConverter.INSTANCE::convert);
|
||||||
|
geoConverters.put("multilinestring", DocumentToGeoJsonMultiLineStringConverter.INSTANCE::convert);
|
||||||
|
geoConverters.put("polygon", DocumentToGeoJsonPolygonConverter.INSTANCE::convert);
|
||||||
|
geoConverters.put("multipolygon", DocumentToGeoJsonMultiPolygonConverter.INSTANCE::convert);
|
||||||
|
geoConverters.put("geometrycollection", DocumentToGeoJsonGeometryCollectionConverter.INSTANCE::convert);
|
||||||
|
|
||||||
|
converters = geoConverters;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor to prevent instantiation.
|
* Private constructor to prevent instantiation.
|
||||||
*/
|
*/
|
||||||
@@ -836,6 +860,7 @@ abstract class GeoConverters {
|
|||||||
enum DocumentToGeoJsonConverter implements Converter<Document, GeoJson> {
|
enum DocumentToGeoJsonConverter implements Converter<Document, GeoJson> {
|
||||||
INSTANCE;
|
INSTANCE;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
|
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
|
||||||
@@ -851,36 +876,17 @@ abstract class GeoConverters {
|
|||||||
|
|
||||||
String type = source.get("type", String.class);
|
String type = source.get("type", String.class);
|
||||||
|
|
||||||
if ("point".equalsIgnoreCase(type)) {
|
if(type != null) {
|
||||||
return DocumentToGeoJsonPointConverter.INSTANCE.convert(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("multipoint".equalsIgnoreCase(type)) {
|
Function<Document, GeoJson<?>> converter = converters.get(type);
|
||||||
return DocumentToGeoJsonMultiPointConverter.INSTANCE.convert(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("linestring".equalsIgnoreCase(type)) {
|
if(converter != null){
|
||||||
return DocumentToGeoJsonLineStringConverter.INSTANCE.convert(source);
|
return converter.apply(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("multilinestring".equalsIgnoreCase(type)) {
|
|
||||||
return DocumentToGeoJsonMultiLineStringConverter.INSTANCE.convert(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("polygon".equalsIgnoreCase(type)) {
|
|
||||||
return DocumentToGeoJsonPolygonConverter.INSTANCE.convert(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("multipolygon".equalsIgnoreCase(type)) {
|
|
||||||
return DocumentToGeoJsonMultiPolygonConverter.INSTANCE.convert(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("geometrycollection".equalsIgnoreCase(type)) {
|
|
||||||
return DocumentToGeoJsonGeometryCollectionConverter.INSTANCE.convert(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) {
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ public class GeoJsonTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAMONGO-1137
|
@Test // DATAMONGO-1137
|
||||||
public void shouleSaveAndRetrieveDocumentWithGeoJsonPointTypeCorrectly() {
|
public void shouldSaveAndRetrieveDocumentWithGeoJsonPointTypeCorrectly() {
|
||||||
|
|
||||||
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
||||||
obj.id = "geoJsonPoint";
|
obj.id = "geoJsonPoint";
|
||||||
@@ -157,7 +157,7 @@ public class GeoJsonTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAMONGO-1137
|
@Test // DATAMONGO-1137
|
||||||
public void shouleSaveAndRetrieveDocumentWithGeoJsonPolygonTypeCorrectly() {
|
public void shouldSaveAndRetrieveDocumentWithGeoJsonPolygonTypeCorrectly() {
|
||||||
|
|
||||||
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
||||||
obj.id = "geoJsonPolygon";
|
obj.id = "geoJsonPolygon";
|
||||||
@@ -173,7 +173,7 @@ public class GeoJsonTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAMONGO-1137
|
@Test // DATAMONGO-1137
|
||||||
public void shouleSaveAndRetrieveDocumentWithGeoJsonLineStringTypeCorrectly() {
|
public void shouldSaveAndRetrieveDocumentWithGeoJsonLineStringTypeCorrectly() {
|
||||||
|
|
||||||
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
||||||
obj.id = "geoJsonLineString";
|
obj.id = "geoJsonLineString";
|
||||||
@@ -188,7 +188,7 @@ public class GeoJsonTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAMONGO-1137
|
@Test // DATAMONGO-1137
|
||||||
public void shouleSaveAndRetrieveDocumentWithGeoJsonMultiLineStringTypeCorrectly() {
|
public void shouldSaveAndRetrieveDocumentWithGeoJsonMultiLineStringTypeCorrectly() {
|
||||||
|
|
||||||
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
||||||
obj.id = "geoJsonMultiLineString";
|
obj.id = "geoJsonMultiLineString";
|
||||||
@@ -205,7 +205,7 @@ public class GeoJsonTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAMONGO-1137
|
@Test // DATAMONGO-1137
|
||||||
public void shouleSaveAndRetrieveDocumentWithGeoJsonMultiPointTypeCorrectly() {
|
public void shouldSaveAndRetrieveDocumentWithGeoJsonMultiPointTypeCorrectly() {
|
||||||
|
|
||||||
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
||||||
obj.id = "geoJsonMultiPoint";
|
obj.id = "geoJsonMultiPoint";
|
||||||
@@ -220,7 +220,7 @@ public class GeoJsonTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAMONGO-1137
|
@Test // DATAMONGO-1137
|
||||||
public void shouleSaveAndRetrieveDocumentWithGeoJsonMultiPolygonTypeCorrectly() {
|
public void shouldSaveAndRetrieveDocumentWithGeoJsonMultiPolygonTypeCorrectly() {
|
||||||
|
|
||||||
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
||||||
obj.id = "geoJsonMultiPolygon";
|
obj.id = "geoJsonMultiPolygon";
|
||||||
@@ -236,7 +236,7 @@ public class GeoJsonTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test // DATAMONGO-1137
|
@Test // DATAMONGO-1137
|
||||||
public void shouleSaveAndRetrieveDocumentWithGeoJsonGeometryCollectionTypeCorrectly() {
|
public void shouldSaveAndRetrieveDocumentWithGeoJsonGeometryCollectionTypeCorrectly() {
|
||||||
|
|
||||||
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
|
||||||
obj.id = "geoJsonGeometryCollection";
|
obj.id = "geoJsonGeometryCollection";
|
||||||
|
|||||||
Reference in New Issue
Block a user