Skip to content

Commit d7ff6b1

Browse files
Initialize Lists holding GeoJson coordinates with size where possible.
See: #4904
1 parent 532dd28 commit d7ff6b1

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class GeoJsonGeometryCollection implements GeoJson<Iterable<GeoJson<?>>>
3434

3535
private static final String TYPE = "GeometryCollection";
3636

37-
private final List<GeoJson<?>> geometries = new ArrayList<GeoJson<?>>();
37+
private final List<GeoJson<?>> geometries;
3838

3939
/**
4040
* Creates a new {@link GeoJsonGeometryCollection} for the given {@link GeoJson} instances.
@@ -45,7 +45,7 @@ public GeoJsonGeometryCollection(List<GeoJson<?>> geometries) {
4545

4646
Assert.notNull(geometries, "Geometries must not be null");
4747

48-
this.geometries.addAll(geometries);
48+
this.geometries = new ArrayList<>(geometries);
4949
}
5050

5151
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class GeoJsonMultiLineString implements GeoJson<Iterable<GeoJsonLineStrin
3535

3636
private static final String TYPE = "MultiLineString";
3737

38-
private List<GeoJsonLineString> coordinates = new ArrayList<GeoJsonLineString>();
38+
private final List<GeoJsonLineString> coordinates;
3939

4040
/**
4141
* Creates new {@link GeoJsonMultiLineString} for the given {@link Point}s.
@@ -46,6 +46,7 @@ public GeoJsonMultiLineString(List<Point>... lines) {
4646

4747
Assert.notEmpty(lines, "Points for MultiLineString must not be null");
4848

49+
this.coordinates = new ArrayList<>(lines.length);
4950
for (List<Point> line : lines) {
5051
this.coordinates.add(new GeoJsonLineString(line));
5152
}
@@ -60,7 +61,7 @@ public GeoJsonMultiLineString(List<GeoJsonLineString> lines) {
6061

6162
Assert.notNull(lines, "Lines for MultiLineString must not be null");
6263

63-
this.coordinates.addAll(lines);
64+
this.coordinates = new ArrayList<>(lines);
6465
}
6566

6667
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public GeoJsonMultiPoint(Point point) {
4949

5050
Assert.notNull(point, "Point must not be null");
5151

52-
this.points = new ArrayList<>();
53-
this.points.add(point);
52+
this.points = List.of(point);
5453
}
5554

5655
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class GeoJsonMultiPolygon implements GeoJson<Iterable<GeoJsonPolygon>> {
3333

3434
private static final String TYPE = "MultiPolygon";
3535

36-
private List<GeoJsonPolygon> coordinates = new ArrayList<GeoJsonPolygon>();
36+
private final List<GeoJsonPolygon> coordinates;
3737

3838
/**
3939
* Creates a new {@link GeoJsonMultiPolygon} for the given {@link GeoJsonPolygon}s.
@@ -44,7 +44,7 @@ public GeoJsonMultiPolygon(List<GeoJsonPolygon> polygons) {
4444

4545
Assert.notNull(polygons, "Polygons for MultiPolygon must not be null");
4646

47-
this.coordinates.addAll(polygons);
47+
this.coordinates = new ArrayList<>(polygons);
4848
}
4949

5050
@Override

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class GeoJsonPolygon extends Polygon implements GeoJson<List<GeoJsonLineS
4242
private static final long serialVersionUID = 3936163018187247185L;
4343
private static final String TYPE = "Polygon";
4444

45-
private List<GeoJsonLineString> coordinates = new ArrayList<GeoJsonLineString>();
45+
private final List<GeoJsonLineString> coordinates;
4646

4747
/**
4848
* Creates new {@link GeoJsonPolygon} from the given {@link Point}s.
@@ -65,6 +65,8 @@ public GeoJsonPolygon(Point first, Point second, Point third, Point fourth, Poin
6565
public GeoJsonPolygon(List<Point> points) {
6666

6767
super(points);
68+
69+
this.coordinates = new ArrayList<>(1);
6870
this.coordinates.add(new GeoJsonLineString(points));
6971
}
7072

0 commit comments

Comments
 (0)