Skip to content

Commit 412f8b8

Browse files
markaalvaroMark Alvarojyemin
authored
Add alternate constructor to Polygon and PolygonCoordinates (#692)
Co-authored-by: Mark Alvaro <[email protected]> Co-authored-by: Jeff Yemin <[email protected]>
1 parent 9b55d2b commit 412f8b8

File tree

6 files changed

+66
-11
lines changed

6 files changed

+66
-11
lines changed

driver-core/src/main/com/mongodb/client/model/geojson/Polygon.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ public Polygon(final List<Position> exterior, final List<Position>... holes) {
4242
this(new PolygonCoordinates(exterior, holes));
4343
}
4444

45+
/**
46+
* Construct an instance with the given coordinates.
47+
*
48+
* @param exterior the exterior ring of the polygon
49+
* @param holes optional interior rings of the polygon
50+
* @since 4.3
51+
*/
52+
public Polygon(final List<Position> exterior, final List<List<Position>> holes) {
53+
this(new PolygonCoordinates(exterior, holes));
54+
}
55+
4556
/**
4657
* Construct an instance with the given coordinates.
4758
*

driver-core/src/main/com/mongodb/client/model/geojson/PolygonCoordinates.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.client.model.geojson;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.Collections;
2122
import java.util.List;
2223

@@ -40,15 +41,27 @@ public final class PolygonCoordinates {
4041
* @param holes optional interior rings of the polygon
4142
*/
4243
@SafeVarargs
44+
@SuppressWarnings("varargs")
4345
public PolygonCoordinates(final List<Position> exterior, final List<Position>... holes) {
46+
this(exterior, Arrays.asList(holes));
47+
}
48+
49+
/**
50+
* Construct an instance.
51+
*
52+
* @param exterior the exterior ring of the polygon
53+
* @param holes optional interior rings of the polygon
54+
* @since 4.3
55+
*/
56+
public PolygonCoordinates(final List<Position> exterior, final List<List<Position>> holes) {
4457
notNull("exteriorRing", exterior);
4558
doesNotContainNull("exterior", exterior);
4659
isTrueArgument("ring must contain at least four positions", exterior.size() >= 4);
4760
isTrueArgument("first and last position must be the same", exterior.get(0).equals(exterior.get(exterior.size() - 1)));
4861

4962
this.exterior = Collections.unmodifiableList(exterior);
5063

51-
List<List<Position>> holesList = new ArrayList<List<Position>>(holes.length);
64+
List<List<Position>> holesList = new ArrayList<List<Position>>(holes.size());
5265
for (List<Position> hole : holes) {
5366
notNull("interiorRing", hole);
5467
doesNotContainNull("hole", hole);

driver-core/src/test/unit/com/mongodb/client/model/geojson/PolygonSpecification.groovy

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,37 @@ class PolygonSpecification extends Specification {
7070

7171
then:
7272
thrown(IllegalArgumentException)
73+
74+
when:
75+
new Polygon(exterior, [null])
76+
77+
then:
78+
thrown(IllegalArgumentException)
79+
80+
when:
81+
new Polygon(exterior, [[new Position([40.0d, 18.0d]),
82+
new Position([40.0d, 19.0d]),
83+
null]])
84+
85+
then:
86+
thrown(IllegalArgumentException)
87+
88+
when:
89+
new Polygon(exterior, [[new Position([40.0d, 18.0d]),
90+
new Position([40.0d, 19.0d]),
91+
new Position([41.0d, 19.0d])]])
92+
93+
then:
94+
thrown(IllegalArgumentException)
95+
96+
when:
97+
new Polygon(exterior, [[new Position([40.0d, 18.0d]),
98+
new Position([40.0d, 19.0d]),
99+
new Position([41.0d, 19.0d]),
100+
new Position([1.0, 2.0])]])
101+
102+
then:
103+
thrown(IllegalArgumentException)
73104
}
74105

75106
def 'should get type'() {

driver-core/src/test/unit/com/mongodb/client/model/geojson/codecs/GeometryCodecSpecification.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ class GeometryCodecSpecification extends Specification {
7272
new PolygonCoordinates([new Position(100.0, 0.0), new Position(101.0, 0.0),
7373
new Position(101.0, 1.0), new Position(100.0, 1.0),
7474
new Position(100.0, 0.0)],
75-
[new Position(100.2, 0.2), new Position(100.8, 0.2),
75+
[[new Position(100.2, 0.2), new Position(100.8, 0.2),
7676
new Position(100.8, 0.8), new Position(100.2, 0.8),
77-
new Position(100.2, 0.2)])]),
77+
new Position(100.2, 0.2)]])]),
7878
new GeometryCollection([new Point(new Position(100d, 0d)),
7979
new LineString([new Position(101d, 0d), new Position(102d, 1d)])])
8080
]

driver-core/src/test/unit/com/mongodb/client/model/geojson/codecs/GeometryCollectionCodecSpecification.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ class GeometryCollectionCodecSpecification extends Specification {
7676
new PolygonCoordinates([new Position(100.0, 0.0), new Position(101.0, 0.0),
7777
new Position(101.0, 1.0), new Position(100.0, 1.0),
7878
new Position(100.0, 0.0)],
79-
[new Position(100.2, 0.2), new Position(100.8, 0.2),
79+
[[new Position(100.2, 0.2), new Position(100.8, 0.2),
8080
new Position(100.8, 0.8), new Position(100.2, 0.8),
81-
new Position(100.2, 0.2)])]),
81+
new Position(100.2, 0.2)]])]),
8282
new GeometryCollection([new Point(new Position(100d, 0d)),
8383
new LineString([new Position(101d, 0d), new Position(102d, 1d)])])
8484
])
@@ -124,9 +124,9 @@ class GeometryCollectionCodecSpecification extends Specification {
124124
new PolygonCoordinates([new Position(100.0, 0.0), new Position(101.0, 0.0),
125125
new Position(101.0, 1.0), new Position(100.0, 1.0),
126126
new Position(100.0, 0.0)],
127-
[new Position(100.2, 0.2), new Position(100.8, 0.2),
127+
[[new Position(100.2, 0.2), new Position(100.8, 0.2),
128128
new Position(100.8, 0.8), new Position(100.2, 0.8),
129-
new Position(100.2, 0.2)])]),
129+
new Position(100.2, 0.2)]])]),
130130
new GeometryCollection([new Point(new Position(100d, 0d)),
131131
new LineString([new Position(101d, 0d), new Position(102d, 1d)])])
132132
])

driver-core/src/test/unit/com/mongodb/client/model/geojson/codecs/MultiPolygonCodecSpecification.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ class MultiPolygonCodecSpecification extends Specification {
4545
new PolygonCoordinates([new Position(100.0, 0.0), new Position(101.0, 0.0),
4646
new Position(101.0, 1.0), new Position(100.0, 1.0),
4747
new Position(100.0, 0.0)],
48-
[new Position(100.2, 0.2), new Position(100.8, 0.2),
48+
[[new Position(100.2, 0.2), new Position(100.8, 0.2),
4949
new Position(100.8, 0.8), new Position(100.2, 0.8),
50-
new Position(100.2, 0.2)])])
50+
new Position(100.2, 0.2)]])])
5151

5252
when:
5353
codec.encode(writer, multiMultiPolygon, context)
@@ -75,9 +75,9 @@ class MultiPolygonCodecSpecification extends Specification {
7575
new PolygonCoordinates([new Position(100.0, 0.0), new Position(101.0, 0.0),
7676
new Position(101.0, 1.0), new Position(100.0, 1.0),
7777
new Position(100.0, 0.0)],
78-
[new Position(100.2, 0.2), new Position(100.8, 0.2),
78+
[[new Position(100.2, 0.2), new Position(100.8, 0.2),
7979
new Position(100.8, 0.8), new Position(100.2, 0.8),
80-
new Position(100.2, 0.2)])])
80+
new Position(100.2, 0.2)]])])
8181

8282
when:
8383
codec.encode(writer, multiMultiPolygon, context)

0 commit comments

Comments
 (0)