Skip to content

Commit 1c6ab25

Browse files
committed
DATAMONGO-1135 - Polishing.
A few polishing changes to the GeoConverters.
1 parent 1c43a3d commit 1c6ab25

File tree

10 files changed

+165
-71
lines changed

10 files changed

+165
-71
lines changed

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

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ private GeoConverters() {}
6767
*
6868
* @return
6969
*/
70+
@SuppressWarnings("unchecked")
7071
public static Collection<? extends Object> getConvertersToRegister() {
7172
return Arrays.asList( //
7273
BoxToDbObjectConverter.INSTANCE //
@@ -421,6 +422,7 @@ static enum GeoCommandToDbObjectConverter implements Converter<GeoCommand, DBObj
421422
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
422423
*/
423424
@Override
425+
@SuppressWarnings("rawtypes")
424426
public DBObject convert(GeoCommand source) {
425427

426428
if (source == null) {
@@ -472,8 +474,13 @@ public DBObject convert(GeoCommand source) {
472474
*/
473475
@SuppressWarnings("rawtypes")
474476
static enum GeoJsonToDbObjectConverter implements Converter<GeoJson, DBObject> {
477+
475478
INSTANCE;
476479

480+
/*
481+
* (non-Javadoc)
482+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
483+
*/
477484
@Override
478485
public DBObject convert(GeoJson source) {
479486

@@ -484,11 +491,15 @@ public DBObject convert(GeoJson source) {
484491
DBObject dbo = new BasicDBObject("type", source.getType());
485492

486493
if (source instanceof GeoJsonGeometryCollection) {
494+
487495
BasicDBList dbl = new BasicDBList();
496+
488497
for (GeoJson geometry : ((GeoJsonGeometryCollection) source).getCoordinates()) {
489498
dbl.add(convert(geometry));
490499
}
500+
491501
dbo.put("geometries", dbl);
502+
492503
} else {
493504
dbo.put("coordinates", convertIfNecessarry(source.getCoordinates()));
494505
}
@@ -503,10 +514,13 @@ private Object convertIfNecessarry(Object candidate) {
503514
}
504515

505516
if (candidate instanceof Iterable) {
517+
506518
BasicDBList dbl = new BasicDBList();
519+
507520
for (Object element : (Iterable) candidate) {
508521
dbl.add(convertIfNecessarry(element));
509522
}
523+
510524
return dbl;
511525
}
512526

@@ -523,8 +537,13 @@ private Object convertIfNecessarry(Object candidate) {
523537
* @since 1.7
524538
*/
525539
static enum GeoJsonPointToDbObjectConverter implements Converter<GeoJsonPoint, DBObject> {
540+
526541
INSTANCE;
527542

543+
/*
544+
* (non-Javadoc)
545+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
546+
*/
528547
@Override
529548
public DBObject convert(GeoJsonPoint source) {
530549
return GeoJsonToDbObjectConverter.INSTANCE.convert(source);
@@ -536,23 +555,33 @@ public DBObject convert(GeoJsonPoint source) {
536555
* @since 1.7
537556
*/
538557
static enum GeoJsonPolygonToDbObjectConverter implements Converter<GeoJsonPolygon, DBObject> {
558+
539559
INSTANCE;
540560

561+
/*
562+
* (non-Javadoc)
563+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
564+
*/
541565
@Override
542566
public DBObject convert(GeoJsonPolygon source) {
543567
return GeoJsonToDbObjectConverter.INSTANCE.convert(source);
544568
}
545-
546569
}
547570

548571
/**
549572
* @author Christoph Strobl
550573
* @since 1.7
551574
*/
552575
static enum DbObjectToGeoJsonPointConverter implements Converter<DBObject, GeoJsonPoint> {
576+
553577
INSTANCE;
554578

579+
/*
580+
* (non-Javadoc)
581+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
582+
*/
555583
@Override
584+
@SuppressWarnings("unchecked")
556585
public GeoJsonPoint convert(DBObject source) {
557586

558587
if (source == null) {
@@ -572,9 +601,13 @@ public GeoJsonPoint convert(DBObject source) {
572601
* @since 1.7
573602
*/
574603
static enum DbObjectToGeoJsonPolygonConverter implements Converter<DBObject, GeoJsonPolygon> {
604+
575605
INSTANCE;
576606

577-
@SuppressWarnings("rawtypes")
607+
/*
608+
* (non-Javadoc)
609+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
610+
*/
578611
@Override
579612
public GeoJsonPolygon convert(DBObject source) {
580613

@@ -594,8 +627,13 @@ public GeoJsonPolygon convert(DBObject source) {
594627
* @since 1.7
595628
*/
596629
static enum DbObjectToGeoJsonMultiPolygonConverter implements Converter<DBObject, GeoJsonMultiPolygon> {
630+
597631
INSTANCE;
598632

633+
/*
634+
* (non-Javadoc)
635+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
636+
*/
599637
@Override
600638
public GeoJsonMultiPolygon convert(DBObject source) {
601639

@@ -608,11 +646,12 @@ public GeoJsonMultiPolygon convert(DBObject source) {
608646

609647
BasicDBList dbl = (BasicDBList) source.get("coordinates");
610648
List<GeoJsonPolygon> polygones = new ArrayList<GeoJsonPolygon>();
649+
611650
for (Object polygon : dbl) {
612651
polygones.add(toGeoJsonPolygon((BasicDBList) polygon));
613652
}
614-
return new GeoJsonMultiPolygon(polygones);
615653

654+
return new GeoJsonMultiPolygon(polygones);
616655
}
617656
}
618657

@@ -621,8 +660,13 @@ public GeoJsonMultiPolygon convert(DBObject source) {
621660
* @since 1.7
622661
*/
623662
static enum DbObjectToGeoJsonLineStringConverter implements Converter<DBObject, GeoJsonLineString> {
663+
624664
INSTANCE;
625665

666+
/*
667+
* (non-Javadoc)
668+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
669+
*/
626670
@Override
627671
public GeoJsonLineString convert(DBObject source) {
628672

@@ -634,6 +678,7 @@ public GeoJsonLineString convert(DBObject source) {
634678
String.format("Cannot convert type '%s' to LineString.", source.get("type")));
635679

636680
BasicDBList cords = (BasicDBList) source.get("coordinates");
681+
637682
return new GeoJsonLineString(toListOfPoint(cords));
638683
}
639684
}
@@ -643,8 +688,13 @@ public GeoJsonLineString convert(DBObject source) {
643688
* @since 1.7
644689
*/
645690
static enum DbObjectToGeoJsonMultiPointConverter implements Converter<DBObject, GeoJsonMultiPoint> {
691+
646692
INSTANCE;
647693

694+
/*
695+
* (non-Javadoc)
696+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
697+
*/
648698
@Override
649699
public GeoJsonMultiPoint convert(DBObject source) {
650700

@@ -656,6 +706,7 @@ public GeoJsonMultiPoint convert(DBObject source) {
656706
String.format("Cannot convert type '%s' to MultiPoint.", source.get("type")));
657707

658708
BasicDBList cords = (BasicDBList) source.get("coordinates");
709+
659710
return new GeoJsonMultiPoint(toListOfPoint(cords));
660711
}
661712
}
@@ -665,8 +716,13 @@ public GeoJsonMultiPoint convert(DBObject source) {
665716
* @since 1.7
666717
*/
667718
static enum DbObjectToGeoJsonMultiLineStringConverter implements Converter<DBObject, GeoJsonMultiLineString> {
719+
668720
INSTANCE;
669721

722+
/*
723+
* (non-Javadoc)
724+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
725+
*/
670726
@Override
671727
public GeoJsonMultiLineString convert(DBObject source) {
672728

@@ -679,6 +735,7 @@ public GeoJsonMultiLineString convert(DBObject source) {
679735

680736
List<GeoJsonLineString> lines = new ArrayList<GeoJsonLineString>();
681737
BasicDBList cords = (BasicDBList) source.get("coordinates");
738+
682739
for (Object line : cords) {
683740
lines.add(new GeoJsonLineString(toListOfPoint((BasicDBList) line)));
684741
}
@@ -691,8 +748,14 @@ public GeoJsonMultiLineString convert(DBObject source) {
691748
* @since 1.7
692749
*/
693750
static enum DbObjectToGeoJsonGeometryCollectionConverter implements Converter<DBObject, GeoJsonGeometryCollection> {
751+
694752
INSTANCE;
695753

754+
/*
755+
* (non-Javadoc)
756+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
757+
*/
758+
@SuppressWarnings("rawtypes")
696759
@Override
697760
public GeoJsonGeometryCollection convert(DBObject source) {
698761

@@ -711,7 +774,7 @@ public GeoJsonGeometryCollection convert(DBObject source) {
711774

712775
}
713776

714-
private GeoJson<?> convertGeometries(DBObject source) {
777+
private static GeoJson<?> convertGeometries(DBObject source) {
715778

716779
Object type = source.get("type");
717780
if (ObjectUtils.nullSafeEquals(type, "Point")) {
@@ -756,11 +819,13 @@ static List<Double> toList(Point point) {
756819
static List<Point> toListOfPoint(BasicDBList listOfCoordinatePairs) {
757820

758821
List<Point> points = new ArrayList<Point>();
822+
759823
for (Object point : listOfCoordinatePairs) {
760824

761825
Assert.isInstanceOf(List.class, point);
762826

763827
List<Double> coordinatesList = (List<Double>) point;
828+
764829
points.add(new GeoJsonPoint(coordinatesList.get(0).doubleValue(), coordinatesList.get(1).doubleValue()));
765830
}
766831
return points;
@@ -769,14 +834,11 @@ static List<Point> toListOfPoint(BasicDBList listOfCoordinatePairs) {
769834
/**
770835
* Converts a coordinate pairs nested in in {@link BasicDBList} into {@link GeoJsonPolygon}.
771836
*
772-
* @param dbl
837+
* @param dbList
773838
* @return
774839
* @since 1.7
775840
*/
776-
static GeoJsonPolygon toGeoJsonPolygon(BasicDBList dbl) {
777-
778-
List<Point> outer = toListOfPoint((BasicDBList) dbl.get(0));
779-
780-
return new GeoJsonPolygon(outer);
841+
static GeoJsonPolygon toGeoJsonPolygon(BasicDBList dbList) {
842+
return new GeoJsonPolygon(toListOfPoint((BasicDBList) dbList.get(0)));
781843
}
782844
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJson.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public interface GeoJson<T extends Iterable<?>> {
2626
/**
2727
* String value representing the type of the {@link GeoJson} object.
2828
*
29-
* @return never {@literal null}.
29+
* @return will never be {@literal null}.
3030
* @see http://geojson.org/geojson-spec.html#geojson-objects
3131
*/
3232
String getType();
@@ -35,7 +35,7 @@ public interface GeoJson<T extends Iterable<?>> {
3535
* The value of the coordinates member is always an {@link Iterable}. The structure for the elements within is
3636
* determined by {@link #getType()} of geometry.
3737
*
38-
* @return never {@literal null}.
38+
* @return will never be {@literal null}.
3939
* @see http://geojson.org/geojson-spec.html#geometry-objects
4040
*/
4141
T getCoordinates();

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonGeometryCollection.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@
3232
public class GeoJsonGeometryCollection implements GeoJson<Iterable<GeoJson<?>>> {
3333

3434
private static final String TYPE = "GeometryCollection";
35+
3536
private final List<GeoJson<?>> geometries = new ArrayList<GeoJson<?>>();
3637

3738
/**
39+
* Creates a new {@link GeoJsonGeometryCollection} for the given {@link GeoJson} instances.
40+
*
3841
* @param geometries
3942
*/
4043
public GeoJsonGeometryCollection(List<GeoJson<?>> geometries) {
4144

42-
Assert.notNull(geometries);
45+
Assert.notNull(geometries, "Geometries must not be null!");
46+
4347
this.geometries.addAll(geometries);
4448
}
4549

@@ -76,17 +80,17 @@ public int hashCode() {
7680
*/
7781
@Override
7882
public boolean equals(Object obj) {
83+
7984
if (this == obj) {
8085
return true;
8186
}
82-
if (obj == null) {
83-
return false;
84-
}
87+
8588
if (!(obj instanceof GeoJsonGeometryCollection)) {
8689
return false;
8790
}
91+
8892
GeoJsonGeometryCollection other = (GeoJsonGeometryCollection) obj;
93+
8994
return ObjectUtils.nullSafeEquals(this.geometries, other.geometries);
9095
}
91-
9296
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonLineString.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,23 @@ public class GeoJsonLineString extends GeoJsonMultiPoint {
3131
private static final String TYPE = "LineString";
3232

3333
/**
34+
* Creates a new {@link GeoJsonLineString} for the given {@link Point}s.
35+
*
3436
* @param points must not be {@literal null} and have at least 2 entries.
3537
*/
3638
public GeoJsonLineString(List<Point> points) {
3739
super(points);
3840
}
3941

4042
/**
41-
* @param p0 must not be {@literal null}
42-
* @param p1 must not be {@literal null}
43+
* Creates a new {@link GeoJsonLineString} for the given {@link Point}s.
44+
*
45+
* @param first must not be {@literal null}
46+
* @param second must not be {@literal null}
4347
* @param others can be {@literal null}
4448
*/
45-
public GeoJsonLineString(Point p0, Point p1, Point... others) {
46-
super(p0, p1, others);
49+
public GeoJsonLineString(Point first, Point second, Point... others) {
50+
super(first, second, others);
4751
}
4852

4953
/*
@@ -54,5 +58,4 @@ public GeoJsonLineString(Point p0, Point p1, Point... others) {
5458
public String getType() {
5559
return TYPE;
5660
}
57-
5861
}

0 commit comments

Comments
 (0)