Skip to content

Commit 93cf49e

Browse files
committed
Add Point reading from coordinate arrays.
Closes #4997 Signed-off-by: dragonfsky <dragonfsky@gmail.com>
1 parent cbc9273 commit 93cf49e

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @author Oliver Gierke
6161
* @author Christoph Strobl
6262
* @author Thiago Diniz da Silveira
63+
* @author dragonfsky
6364
* @since 1.5
6465
*/
6566
@SuppressWarnings("ConstantConditions")
@@ -105,6 +106,7 @@ public static Collection<? extends Object> getConvertersToRegister() {
105106
, DocumentToCircleConverter.INSTANCE //
106107
, DocumentToSphereConverter.INSTANCE //
107108
, DocumentToPointConverter.INSTANCE //
109+
, ListToPointConverter.INSTANCE //
108110
, PointToDocumentConverter.INSTANCE //
109111
, GeoCommandToDocumentConverter.INSTANCE //
110112
, GeoJsonToDocumentConverter.INSTANCE //
@@ -149,6 +151,31 @@ public Point convert(Document source) {
149151
}
150152
}
151153

154+
/**
155+
* Converts a {@link List} of coordinates into a {@link Point}.
156+
*
157+
* @author dragonfsky
158+
* @since 5.1
159+
*/
160+
@ReadingConverter
161+
enum ListToPointConverter implements Converter<List<Number>, @Nullable Point> {
162+
163+
INSTANCE;
164+
165+
@Override
166+
@SuppressWarnings("NullAway")
167+
public Point convert(List<Number> source) {
168+
169+
if (ObjectUtils.isEmpty(source)) {
170+
return null;
171+
}
172+
173+
Assert.isTrue(source.size() == 2, "Source must contain 2 elements");
174+
175+
return new Point(toPrimitiveDoubleValue(source.get(0)), toPrimitiveDoubleValue(source.get(1)));
176+
}
177+
}
178+
152179
/**
153180
* Converts a {@link Point} into a {@link List} of {@link Double}s.
154181
*

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @author Thomas Darimont
4040
* @author Oliver Gierke
4141
* @author Christoph Strobl
42+
* @author dragonfsky
4243
* @since 1.5
4344
*/
4445
public class GeoConvertersUnitTests {
@@ -152,6 +153,13 @@ public void convertsPointCorrectlyWhenUsingNonDoubleForCoordinates() {
152153
.isEqualTo(new Point(1, 2));
153154
}
154155

156+
@Test // GH-4997
157+
public void convertsCoordinateListToPointCorrectly() {
158+
159+
assertThat(ListToPointConverter.INSTANCE.convert(Arrays.asList(-73.99171, 40.738868)))
160+
.isEqualTo(new Point(-73.99171, 40.738868));
161+
}
162+
155163
@Test // DATAMONGO-1607
156164
public void convertsCircleCorrectlyWhenUsingNonDoubleForCoordinates() {
157165

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
* @author Roman Puchkovskiy
127127
* @author Heesu Jung
128128
* @author Julia Lee
129+
* @author dragonfsky
129130
*/
130131
@ExtendWith(MockitoExtension.class)
131132
class MappingMongoConverterUnitTests {
@@ -1552,6 +1553,16 @@ void shouldReadEntityWithGeoBoxCorrectly() {
15521553
assertThat(result.box).isEqualTo(object.box);
15531554
}
15541555

1556+
@Test // GH-4997
1557+
void shouldReadEntityWithGeoPointFromArrayCoordinates() {
1558+
1559+
org.bson.Document document = new org.bson.Document("point", Arrays.asList(-73.99171, 40.738868));
1560+
1561+
ClassWithGeoPoint result = converter.read(ClassWithGeoPoint.class, document);
1562+
1563+
assertThat(result.point).isEqualTo(new Point(-73.99171, 40.738868));
1564+
}
1565+
15551566
@Test // DATAMONGO-858
15561567
void shouldWriteEntityWithGeoPolygonCorrectly() {
15571568

@@ -4059,6 +4070,11 @@ class ClassWithGeoBox {
40594070
Box box;
40604071
}
40614072

4073+
class ClassWithGeoPoint {
4074+
4075+
Point point;
4076+
}
4077+
40624078
class ClassWithGeoCircle {
40634079

40644080
Circle circle;

0 commit comments

Comments
 (0)