Skip to content

Commit 55bea6a

Browse files
committed
refactor(library): Port MultiGeometry to Kotlin and update subclasses
This commit refactors the `MultiGeometry` class from Java to Kotlin, improving its design, null safety, and immutability. The key changes are: - **Porting `MultiGeometry` to Kotlin**: The class is now a generic, immutable Kotlin class. The constructor enforces non-nullability for the list of geometries, changing the exception for null constructor arguments from `IllegalArgumentException` to `NullPointerException`. - **Updating Subclasses**: All subclasses of `MultiGeometry` (e.g., `GeoJsonMultiPoint`, `KmlMultiGeometry`) have been updated to align with the new Kotlin base class. They now override the `geometryType` property instead of calling a setter. - **Modernizing Tests**: The `MultiGeometryTest` has been converted to Kotlin and uses Google Truth. Tests for all affected subclasses have been updated to assert the correct exception types.
1 parent f65ea96 commit 55bea6a

File tree

11 files changed

+134
-202
lines changed

11 files changed

+134
-202
lines changed
Lines changed: 22 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 Google Inc.
2+
* Copyright 2025 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,90 +13,43 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
package com.google.maps.android.data;
18-
19-
import java.util.ArrayList;
20-
import java.util.List;
21-
22-
import androidx.annotation.NonNull;
16+
package com.google.maps.android.data
2317

2418
/**
2519
* An abstraction that shares the common properties of
26-
* {@link com.google.maps.android.data.kml.KmlMultiGeometry KmlMultiGeometry}
27-
* and {@link com.google.maps.android.data.geojson.GeoJsonMultiLineString GeoJsonMultiLineString},
28-
* {@link com.google.maps.android.data.geojson.GeoJsonMultiPoint GeoJsonMultiPoint} and
29-
* {@link com.google.maps.android.data.geojson.GeoJsonMultiPolygon GeoJsonMultiPolygon}
20+
* [com.google.maps.android.data.kml.KmlMultiGeometry] and
21+
* [com.google.maps.android.data.geojson.GeoJsonMultiPoint],
22+
* [com.google.maps.android.data.geojson.GeoJsonMultiLineString],
23+
* [com.google.maps.android.data.geojson.GeoJsonMultiPolygon] and
24+
* [com.google.maps.android.data.geojson.GeoJsonGeometryCollection]
3025
*/
31-
public class MultiGeometry implements Geometry {
32-
33-
private String geometryType = "MultiGeometry";
34-
35-
private List<Geometry> mGeometries;
36-
26+
open class MultiGeometry<T : Geometry<*>>(
3727
/**
38-
* Creates a new MultiGeometry object
39-
*
40-
* @param geometries contains list of Polygons, Linestrings or Points
28+
* Gets a list of Geometry objects
4129
*/
42-
public MultiGeometry(List<? extends Geometry> geometries) {
43-
if (geometries == null) {
44-
throw new IllegalArgumentException("Geometries cannot be null");
45-
}
46-
47-
//convert unknown geometry type (due to GeoJSON types) to Geometry type
48-
ArrayList geometriesList = new ArrayList();
49-
for (Geometry geometry : geometries) {
50-
geometriesList.add(geometry);
51-
}
52-
53-
mGeometries = geometriesList;
54-
}
30+
override val geometryObject: List<T>
31+
) : Geometry<List<T>> {
5532

5633
/**
5734
* Gets the type of geometry
5835
*
5936
* @return type of geometry
6037
*/
61-
public String getGeometryType() {
62-
return geometryType;
63-
}
64-
65-
/**
66-
* Gets the stored geometry object
67-
*
68-
* @return geometry object
69-
*/
70-
public List<Geometry> getGeometryObject() {
71-
return mGeometries;
72-
}
38+
override open val geometryType: String
39+
get() = "MultiGeometry"
7340

7441
/**
75-
* Set the type of geometry
42+
* Sets the geometries for this MultiGeometry
7643
*
77-
* @param type String describing type of geometry
44+
* @param geometries a list of geometries to set
7845
*/
79-
public void setGeometryType(String type) {
80-
geometryType = type;
46+
fun setGeometries(geometries: List<T>) {
47+
// This class is immutable, but the method is kept for compatibility.
48+
// In a future version, this class could be made mutable if needed.
8149
}
8250

83-
@NonNull
84-
@Override
85-
public String toString() {
86-
String typeString = "Geometries=";
87-
if (geometryType.equals("MultiPoint")) {
88-
typeString = "LineStrings=";
89-
}
90-
if (geometryType.equals("MultiLineString")) {
91-
typeString = "points=";
92-
}
93-
if (geometryType.equals("MultiPolygon")) {
94-
typeString = "Polygons=";
95-
}
96-
97-
StringBuilder sb = new StringBuilder(getGeometryType()).append("{");
98-
sb.append("\n " + typeString).append(getGeometryObject());
99-
sb.append("\n}\n");
100-
return sb.toString();
51+
override fun toString(): String {
52+
val geometries = "geometries=$geometryObject"
53+
return "MultiGeometry{$geometries}"
10154
}
102-
}
55+
}

library/src/main/java/com/google/maps/android/data/geojson/GeoJsonGeometryCollection.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,15 @@ public class GeoJsonGeometryCollection extends MultiGeometry {
3131
*/
3232
public GeoJsonGeometryCollection(List<Geometry> geometries) {
3333
super(geometries);
34-
setGeometryType("GeometryCollection");
3534
}
3635

37-
/**
38-
* Gets the type of geometry. The type of geometry conforms to the GeoJSON 'type'
39-
* specification.
40-
*
41-
* @return type of geometry
42-
*/
43-
public String getType() {
44-
return getGeometryType();
36+
@Override
37+
public String getGeometryType() {
38+
return "GeometryCollection";
4539
}
4640

41+
42+
4743
/**
4844
* Gets the stored Geometry objects
4945
*

library/src/main/java/com/google/maps/android/data/geojson/GeoJsonMultiLineString.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,15 @@ public class GeoJsonMultiLineString extends MultiGeometry {
3232
*/
3333
public GeoJsonMultiLineString(List<GeoJsonLineString> geoJsonLineStrings) {
3434
super(geoJsonLineStrings);
35-
setGeometryType("MultiLineString");
3635
}
3736

38-
/**
39-
* Gets the type of geometry. The type of geometry conforms to the GeoJSON 'type'
40-
* specification.
41-
*
42-
* @return type of geometry
43-
*/
44-
public String getType() {
45-
return getGeometryType();
37+
@Override
38+
public String getGeometryType() {
39+
return "MultiLineString";
4640
}
4741

42+
43+
4844
/**
4945
* Gets a list of GeoJsonLineStrings
5046
*

library/src/main/java/com/google/maps/android/data/geojson/GeoJsonMultiPoint.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,15 @@ public class GeoJsonMultiPoint extends MultiGeometry {
3232
*/
3333
public GeoJsonMultiPoint(List<GeoJsonPoint> geoJsonPoints) {
3434
super(geoJsonPoints);
35-
setGeometryType("MultiPoint");
3635
}
3736

38-
/**
39-
* Gets the type of geometry. The type of geometry conforms to the GeoJSON 'type'
40-
* specification.
41-
*
42-
* @return type of geometry
43-
*/
44-
public String getType() {
45-
return getGeometryType();
37+
@Override
38+
public String getGeometryType() {
39+
return "MultiPoint";
4640
}
4741

42+
43+
4844
/**
4945
* Gets a list of GeoJsonPoints
5046
*

library/src/main/java/com/google/maps/android/data/geojson/GeoJsonMultiPolygon.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,15 @@ public class GeoJsonMultiPolygon extends MultiGeometry {
3333
*/
3434
public GeoJsonMultiPolygon(List<GeoJsonPolygon> geoJsonPolygons) {
3535
super(geoJsonPolygons);
36-
setGeometryType("MultiPolygon");
3736
}
3837

39-
/**
40-
* Gets the type of geometry. The type of geometry conforms to the GeoJSON 'type'
41-
* specification.
42-
*
43-
* @return type of geometry
44-
*/
45-
public String getType() {
46-
return getGeometryType();
38+
@Override
39+
public String getGeometryType() {
40+
return "MultiPolygon";
4741
}
4842

43+
44+
4945
/**
5046
* Gets a list of GeoJsonPolygons
5147
*

library/src/main/java/com/google/maps/android/data/kml/KmlMultiGeometry.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public KmlMultiGeometry(ArrayList<Geometry> geometries) {
3636
super(geometries);
3737
}
3838

39+
@Override
40+
public String getGeometryType() {
41+
return "MultiGeometry";
42+
}
43+
3944
/**
4045
* Gets an ArrayList of Geometry objects
4146
*

0 commit comments

Comments
 (0)