This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit d062c52b91991212a51b52ce931e8b97779bffcb Author: jsorel <[email protected]> AuthorDate: Tue Nov 17 15:52:36 2026 +0100 feat(Geometry): implement hashcode and equals on DefaultLineString and DefaultPolygon --- .../internal/shared/DefaultLineString.java | 22 +++++++++++++++++++++ .../geometries/internal/shared/DefaultPolygon.java | 23 ++++++++++++++++++++++ .../internal/shared/DefaultPolygonTest.java | 22 +++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultLineString.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultLineString.java index 0b0ee133ec..f630cda7d7 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultLineString.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultLineString.java @@ -16,6 +16,7 @@ */ package org.apache.sis.geometries.internal.shared; +import java.util.Objects; import javax.measure.Quantity; import org.apache.sis.geometries.DataPoints; import org.apache.sis.geometries.Geometries; @@ -63,4 +64,25 @@ public non-sealed class DefaultLineString extends AbstractGeometry implements Li return asJTS().isValid(); } + @Override + public int hashCode() { + return getClass().hashCode() + 19 * Objects.hashCode(points); + } + + /** + * Compares this line string with the given object for equality. + * + * @param obj the object to compare with this line string. + * @return whether the given object is a line string of the same class with equal points. + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + return Objects.equals(points, ((DefaultLineString) obj).points); + } } diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultPolygon.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultPolygon.java index bac3c84544..8c7d6ef1a8 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultPolygon.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/internal/shared/DefaultPolygon.java @@ -166,4 +166,27 @@ public non-sealed class DefaultPolygon extends AbstractGeometry implements Polyg return asJTS().isValid(); } + @Override + public int hashCode() { + return getClass().hashCode() + 19 * Objects.hash(exterior, interiors); + } + + /** + * Compares this polygon with the given object for equality. + * + * @param obj the object to compare with this polygon. + * @return whether the given object is a polygon of the same class with equal rings. + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + final DefaultPolygon other = (DefaultPolygon) obj; + return Objects.equals(exterior, other.exterior) + && Objects.equals(interiors, other.interiors); + } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/internal/shared/DefaultPolygonTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/internal/shared/DefaultPolygonTest.java index 41a7d3b9b8..ae464fb5ca 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/internal/shared/DefaultPolygonTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/internal/shared/DefaultPolygonTest.java @@ -24,8 +24,11 @@ import org.apache.sis.geometries.curve.LinearRing; import org.apache.sis.geometries.surface.Polygon; import org.apache.sis.maths.NDArrays; import org.apache.sis.maths.SampleSystem; +import org.apache.sis.referencing.CommonCRS; // Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; import org.apache.sis.geometries.surface.PolygonTest; @@ -47,6 +50,25 @@ public class DefaultPolygonTest extends PolygonTest { return new DefaultPolygon(new DefaultLinearRing(sequence(crs, exterior)), interiors); } + /** + * Tests {@code equals} and {@code hashCode} of polygons and of their rings. + */ + @Test + public void testEquals() { + final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic(); + final double[] exterior = {0,0, 10,0, 10,10, 0,10, 0,0}; + final double[] hole = {2,2, 3,2, 3,3, 2,2}; + final Polygon polygon = createPolygon(crs, exterior, hole); + final Polygon same = createPolygon(crs, exterior, hole); + assertNotSame(polygon, same); + assertEquals(polygon, same); + assertEquals(polygon.hashCode(), same.hashCode()); + assertEquals(polygon.getExteriorRing(), same.getExteriorRing()); + assertNotEquals(polygon, createPolygon(crs, exterior)); + assertNotEquals(new DefaultLineString(sequence(crs, hole)), new DefaultLinearRing(sequence(crs, hole)), + "A line string is not equal to a linear ring."); + } + /** * Creates the positions of a geometry from a flat list of ordinate values. */
