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 9340e1e5d27674f7c675dbfd80174e723372ffd3
Author: jsorel <[email protected]>
AuthorDate: Fri Nov 27 14:16:01 2026 +0100

    fix(Math): report vector dimension mismatches with the same message
---
 .../main/org/apache/sis/maths/Vector1D.java        | 16 +++--------
 .../main/org/apache/sis/maths/Vector2D.java        | 24 ++++------------
 .../main/org/apache/sis/maths/Vector3D.java        | 32 ++++++----------------
 .../main/org/apache/sis/maths/Vector4D.java        | 24 ++++------------
 .../test/org/apache/sis/maths/VectorTest.java      | 19 +++++++++++++
 5 files changed, 43 insertions(+), 72 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java
index 5c03897be1..ab74950e1a 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector1D.java
@@ -1122,9 +1122,7 @@ public abstract sealed class Vector1D<T extends 
Vector1D<T>> extends AbstractVec
 
         @Override
         public double dot(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 1);
             return x * other.get(0);
         }
 
@@ -1237,9 +1235,7 @@ public abstract sealed class Vector1D<T extends 
Vector1D<T>> extends AbstractVec
 
         @Override
         public double distanceSquare(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 1);
             final double dx = x - other.get(0);
             return dx*dx;
         }
@@ -1298,9 +1294,7 @@ public abstract sealed class Vector1D<T extends 
Vector1D<T>> extends AbstractVec
 
         @Override
         public double dot(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 1);
             return x * other.get(0);
         }
 
@@ -1413,9 +1407,7 @@ public abstract sealed class Vector1D<T extends 
Vector1D<T>> extends AbstractVec
 
         @Override
         public double distanceSquare(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 1);
             final double dx = x - other.get(0);
             return dx*dx;
         }
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java
index 4f0234cf0b..1b2c692904 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector2D.java
@@ -1375,18 +1375,14 @@ public abstract sealed class Vector2D<T extends 
Vector2D<T>> extends AbstractVec
 
         @Override
         public double dot(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 2);
             return x * other.get(0)
                  + y * other.get(1);
         }
 
         @Override
         public double dotAccurate(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 2);
             return LinearCombination.of(x, other.get(0), y, other.get(1));
         }
 
@@ -1524,9 +1520,7 @@ public abstract sealed class Vector2D<T extends 
Vector2D<T>> extends AbstractVec
 
         @Override
         public double distanceSquare(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 2);
             final double dx = x - other.get(0);
             final double dy = y - other.get(1);
             return dx*dx + dy*dy;
@@ -1593,18 +1587,14 @@ public abstract sealed class Vector2D<T extends 
Vector2D<T>> extends AbstractVec
 
         @Override
         public double dot(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 2);
             return x * other.get(0)
                  + y * other.get(1);
         }
 
         @Override
         public double dotAccurate(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 2);
             return LinearCombination.of(x, other.get(0), y, other.get(1));
         }
 
@@ -1742,9 +1732,7 @@ public abstract sealed class Vector2D<T extends 
Vector2D<T>> extends AbstractVec
 
         @Override
         public double distanceSquare(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 2);
             final double dx = x - other.get(0);
             final double dy = y - other.get(1);
             return dx*dx + dy*dy;
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java
index 875c1b41cc..6e1dada076 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector3D.java
@@ -1611,9 +1611,7 @@ public abstract sealed class Vector3D<T extends 
Vector3D<T>> extends AbstractVec
 
         @Override
         public double dot(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 3);
             return x * other.get(0)
                  + y * other.get(1)
                  + z * other.get(2);
@@ -1621,9 +1619,7 @@ public abstract sealed class Vector3D<T extends 
Vector3D<T>> extends AbstractVec
 
         @Override
         public double dotAccurate(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 3);
             return LinearCombination.of(x, other.get(0), y, other.get(1), z, 
other.get(2));
         }
 
@@ -1774,9 +1770,7 @@ public abstract sealed class Vector3D<T extends 
Vector3D<T>> extends AbstractVec
 
         @Override
         public double distanceSquare(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 3);
             final double dx = x - other.get(0);
             final double dy = y - other.get(1);
             final double dz = z - other.get(2);
@@ -1851,9 +1845,7 @@ public abstract sealed class Vector3D<T extends 
Vector3D<T>> extends AbstractVec
 
         @Override
         public double dot(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 3);
             return x * other.get(0)
                  + y * other.get(1)
                  + z * other.get(2);
@@ -1861,17 +1853,13 @@ public abstract sealed class Vector3D<T extends 
Vector3D<T>> extends AbstractVec
 
         @Override
         public double dotAccurate(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 3);
             return LinearCombination.of(x, other.get(0), y, other.get(1), z, 
other.get(2));
         }
 
         @Override
         public Double cross(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 3);
             double ox = other.get(0);
             double oy = other.get(1);
             double oz = other.get(2);
@@ -1884,9 +1872,7 @@ public abstract sealed class Vector3D<T extends 
Vector3D<T>> extends AbstractVec
 
         @Override
         public Double crossAccurate(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 3);
             double ox = other.get(0);
             double oy = other.get(1);
             double oz = other.get(2);
@@ -2044,9 +2030,7 @@ public abstract sealed class Vector3D<T extends 
Vector3D<T>> extends AbstractVec
 
         @Override
         public double distanceSquare(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 3);
             final double dx = x - other.get(0);
             final double dy = y - other.get(1);
             final double dz = z - other.get(2);
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java
index 54d586b25c..220ac188aa 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Vector4D.java
@@ -1620,9 +1620,7 @@ public abstract sealed class Vector4D<T extends 
Vector4D<T>> extends AbstractVec
 
         @Override
         public double dot(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 4);
             return x * other.get(0)
                  + y * other.get(1)
                  + z * other.get(2)
@@ -1631,9 +1629,7 @@ public abstract sealed class Vector4D<T extends 
Vector4D<T>> extends AbstractVec
 
         @Override
         public double dotAccurate(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 4);
             return LinearCombination.of(x, other.get(0), y, other.get(1), z, 
other.get(2), w, other.get(3));
         }
 
@@ -1792,9 +1788,7 @@ public abstract sealed class Vector4D<T extends 
Vector4D<T>> extends AbstractVec
 
         @Override
         public double distanceSquare(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 4);
             final double dx = x - other.get(0);
             final double dy = y - other.get(1);
             final double dz = z - other.get(2);
@@ -1877,9 +1871,7 @@ public abstract sealed class Vector4D<T extends 
Vector4D<T>> extends AbstractVec
 
         @Override
         public double dot(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 4);
             return x * other.get(0)
                  + y * other.get(1)
                  + z * other.get(2)
@@ -1888,9 +1880,7 @@ public abstract sealed class Vector4D<T extends 
Vector4D<T>> extends AbstractVec
 
         @Override
         public double dotAccurate(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 4);
             return LinearCombination.of(x, other.get(0), y, other.get(1), z, 
other.get(2), w, other.get(3));
         }
 
@@ -2049,9 +2039,7 @@ public abstract sealed class Vector4D<T extends 
Vector4D<T>> extends AbstractVec
 
         @Override
         public double distanceSquare(Vector<?> other) {
-            if (getDimension() != other.getDimension()) {
-                throw new IllegalArgumentException("Vectors size are different 
: "+getDimension()+" and "+other.getDimension());
-            }
+            ensureDimension(other, 4);
             final double dx = x - other.get(0);
             final double dy = y - other.get(1);
             final double dz = z - other.get(2);
diff --git 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/VectorTest.java
 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/VectorTest.java
index 0999b05087..bdfbd358ae 100644
--- 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/VectorTest.java
+++ 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/maths/VectorTest.java
@@ -136,4 +136,23 @@ public class VectorTest {
         assertEquals(16785409.0, Vector.of(0f, 0f, 0f, 4097f).lengthSquare(), 
TOLERANCE);
         assertEquals(4097.0,     Vector.of(0f, 0f, 0f, 4097f).length(), 
TOLERANCE);
     }
+
+    /**
+     * Float and double vectors report a dimension mismatch with the same 
message as the other vectors.
+     */
+    @Test
+    public void testDimensionMismatch() {
+        final Vector<?> wrong = Vector.of(1.0, 2.0);
+        final Vector3D.Double d = Vector.of(1.0, 2.0, 3.0);
+        final Vector3D.Float  f = Vector.of(1f, 2f, 3f);
+        assertEquals("Vector dimension must be 3", 
assertThrows(IllegalArgumentException.class, () -> d.dot(wrong)).getMessage());
+        assertEquals("Vector dimension must be 3", 
assertThrows(IllegalArgumentException.class, () -> 
d.dotAccurate(wrong)).getMessage());
+        assertEquals("Vector dimension must be 3", 
assertThrows(IllegalArgumentException.class, () -> 
d.distanceSquare(wrong)).getMessage());
+        assertEquals("Vector dimension must be 3", 
assertThrows(IllegalArgumentException.class, () -> 
d.cross(wrong)).getMessage());
+        assertEquals("Vector dimension must be 3", 
assertThrows(IllegalArgumentException.class, () -> 
d.crossAccurate(wrong)).getMessage());
+        assertEquals("Vector dimension must be 3", 
assertThrows(IllegalArgumentException.class, () -> f.dot(wrong)).getMessage());
+        assertEquals("Vector dimension must be 3", 
assertThrows(IllegalArgumentException.class, () -> 
f.distanceSquare(wrong)).getMessage());
+        assertEquals("Vector dimension must be 1", 
assertThrows(IllegalArgumentException.class, () -> 
Vector.of(1.0).dot(wrong)).getMessage());
+        assertEquals("Vector dimension must be 4", 
assertThrows(IllegalArgumentException.class, () -> Vector.of(1f, 2f, 3f, 
4f).dotAccurate(wrong)).getMessage());
+    }
 }

Reply via email to