Skip to content

Commit 12c5a3b

Browse files
dkhawkkikoso
andauthored
build: Port geometry and quadtree packages to Kotlin (#1569)
* Rename .java to .kt * feat(library): Port PointQuadTree and tests to Kotlin This commit migrates the `PointQuadTree` class and its corresponding test, `PointQuadTreeTest`, from Java to idiomatic Kotlin. This conversion modernizes the codebase, improving its conciseness, readability, and null safety. The key changes are: - **`PointQuadTree` to Kotlin**: The core `PointQuadTree` class has been rewritten in Kotlin, leveraging features like primary constructors, companion objects, and stricter nullability. - **`PointQuadTreeTest` to Kotlin**: The associated test class has also been ported to Kotlin, resulting in more streamlined test code. - **Copyright Header Update**: The copyright year has been updated in the converted files. * Rename .java to .kt * refactor(geometry): Port Point and Bounds classes to Kotlin This commit modernizes the `Point` and `Bounds` classes by converting them from Java to idiomatic Kotlin. This change improves code conciseness, readability, and null safety. Key changes include: - The `Point` and `Bounds` classes are now written in Kotlin. - `@JvmField` annotations are used on properties to maintain binary compatibility for Java consumers. - New unit tests, `PointTest.kt` and `BoundsTest.kt`, have been added to ensure the correctness of the ported classes. * feat: added headers --------- Co-authored-by: Enrique López Mañas <[email protected]>
1 parent 3471b4d commit 12c5a3b

File tree

10 files changed

+685
-557
lines changed

10 files changed

+685
-557
lines changed

library/src/main/java/com/google/maps/android/geometry/Bounds.java

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package com.google.maps.android.geometry
19+
20+
/**
21+
* Represents an area in the cartesian plane.
22+
*/
23+
class Bounds(
24+
@JvmField val minX: Double,
25+
@JvmField val maxX: Double,
26+
@JvmField val minY: Double,
27+
@JvmField val maxY: Double
28+
) {
29+
@JvmField
30+
val midX: Double = (minX + maxX) / 2
31+
32+
@JvmField
33+
val midY: Double = (minY + maxY) / 2
34+
35+
fun contains(x: Double, y: Double): Boolean {
36+
return minX <= x && x <= maxX && minY <= y && y <= maxY
37+
}
38+
39+
fun contains(point: Point): Boolean {
40+
return contains(point.x, point.y)
41+
}
42+
43+
fun intersects(minX: Double, maxX: Double, minY: Double, maxY: Double): Boolean {
44+
return minX < this.maxX && this.minX < maxX && minY < this.maxY && this.minY < maxY
45+
}
46+
47+
fun intersects(bounds: Bounds): Boolean {
48+
return intersects(bounds.minX, bounds.maxX, bounds.minY, bounds.maxY)
49+
}
50+
51+
fun contains(bounds: Bounds): Boolean {
52+
return bounds.minX >= minX && bounds.maxX <= maxX && bounds.minY >= minY && bounds.maxY <= maxY
53+
}
54+
}

library/src/main/java/com/google/maps/android/geometry/Point.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2025 Google LLC.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.maps.android.geometry
18+
19+
open class Point(@JvmField val x: Double, @JvmField val y: Double) {
20+
override fun toString(): String {
21+
return "Point(x=$x, y=$y)"
22+
}
23+
24+
override fun equals(other: Any?): Boolean {
25+
if (this === other) return true
26+
if (javaClass != other?.javaClass) return false
27+
28+
other as Point
29+
30+
if (x != other.x) return false
31+
if (y != other.y) return false
32+
33+
return true
34+
}
35+
36+
override fun hashCode(): Int {
37+
var result = x.hashCode()
38+
result = 31 * result + y.hashCode()
39+
return result
40+
}
41+
42+
fun copy(x: Double = this.x, y: Double = this.y): Point {
43+
return Point(x, y)
44+
}
45+
}

0 commit comments

Comments
 (0)