This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 6f8c006837313884f4751b0fb9e9a266464bbc12
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Wed Sep 14 16:29:37 2022 +0200

    Make `GridExtent.intersect(…)` and `union(…)` methods public.
---
 .../org/apache/sis/coverage/grid/GridExtent.java   | 42 ++++++++++++++++------
 .../apache/sis/coverage/grid/GridExtentTest.java   | 18 ++++++++++
 .../java/org/apache/sis/util/resources/Errors.java |  5 +++
 .../apache/sis/util/resources/Errors.properties    |  1 +
 .../apache/sis/util/resources/Errors_fr.properties |  1 +
 5 files changed, 57 insertions(+), 10 deletions(-)

diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
index ce9832b8fa..d7cbff64fe 100644
--- 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
+++ 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java
@@ -31,6 +31,7 @@ import org.opengis.util.FactoryException;
 import org.opengis.util.InternationalString;
 import org.opengis.geometry.Envelope;
 import org.opengis.geometry.DirectPosition;
+import org.opengis.geometry.MismatchedDimensionException;
 import org.opengis.metadata.spatial.DimensionNameType;
 import org.opengis.referencing.cs.AxisDirection;
 import org.opengis.referencing.cs.CoordinateSystem;
@@ -1731,29 +1732,35 @@ public class GridExtent implements GridEnvelope, 
LenientComparable, Serializable
 
     /**
      * Returns the intersection of this grid extent with to the given grid 
extent.
-     * The given extent shall have the same number of dimensions.
-     *
-     * <p>This method is not public because we do not yet have a policy
-     * about whether we should verify if axis {@link #types} match.</p>
+     * The given extent shall have the same number of dimensions than this 
extent.
+     * The {@linkplain #getAxisType(int) axis types} (vertical, temporal, …) 
must
+     * be the same in all dimensions, ignoring types that are absent.
      *
      * @param  other  the grid to intersect with.
      * @return the intersection result. May be one of the existing instances.
+     * @throws MismatchedDimensionException if the two extents do not have the 
same number of dimensions.
+     * @throws IllegalArgumentException} if axis types are specified but 
inconsistent for at least one dimension.
+     *
+     * @since 1.3
      */
-    final GridExtent intersect(final GridExtent other) {
+    public GridExtent intersect(final GridExtent other) {
         return combine(other, false);
     }
 
     /**
      * Returns the union of this grid extent with to the given grid extent.
-     * The given extent shall have the same number of dimensions.
-     *
-     * <p>This method is not public because we do not yet have a policy
-     * about whether we should verify if axis {@link #types} match.</p>
+     * The given extent shall have the same number of dimensions than this 
extent.
+     * The {@linkplain #getAxisType(int) axis types} (vertical, temporal, …) 
must
+     * be the same in all dimensions, ignoring types that are absent.
      *
      * @param  other  the grid to combine with.
      * @return the union result. May be one of the existing instances.
+     * @throws MismatchedDimensionException if the two extents do not have the 
same number of dimensions.
+     * @throws IllegalArgumentException} if axis types are specified but 
inconsistent for at least one dimension.
+     *
+     * @since 1.3
      */
-    final GridExtent union(final GridExtent other) {
+    public GridExtent union(final GridExtent other) {
         return combine(other, true);
     }
 
@@ -1763,6 +1770,21 @@ public class GridExtent implements GridEnvelope, 
LenientComparable, Serializable
     private GridExtent combine(final GridExtent other, final boolean union) {
         final int n = coordinates.length;
         final int m = n >>> 1;
+        if (n != other.coordinates.length) {
+            throw new MismatchedDimensionException(Errors.format(
+                    Errors.Keys.MismatchedDimension_3, "other", m, 
other.getDimension()));
+        }
+        if (types != null && other.types != null) {
+            for (int i=0; i<m; i++) {
+                final DimensionNameType t1 = types[i];
+                if (t1 != null) {
+                    final DimensionNameType t2 = other.types[i];
+                    if (t2 != null && !t1.equals(t2)) {
+                        throw new 
IllegalArgumentException(Errors.format(Errors.Keys.MismatchedAxes_3, i, t1, 
t2));
+                    }
+                }
+            }
+        }
         final long[] clipped = new long[n];
         int i = 0;
         while (i < m) {clipped[i] = extremum(coordinates[i], 
other.coordinates[i], !union); i++;}
diff --git 
a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java
 
b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java
index 4a7b022bd5..71081a3b2e 100644
--- 
a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java
+++ 
b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java
@@ -297,6 +297,24 @@ public final strictfp class GridExtentTest extends 
TestCase {
         assertSame(extent.union(domain), extent);
     }
 
+    /**
+     * Tests {@link GridExtent#intersect(GridExtent)} with inconsistent axis 
types.
+     * An exception should be thrown.
+     */
+    @Test
+    public void testCombineInvalid() {
+        final GridExtent domain = createOther();
+        final GridExtent other = new GridExtent(
+                new DimensionNameType[] {DimensionNameType.COLUMN, 
DimensionNameType.TRACK, DimensionNameType.TIME},
+                new long[] {100, 200, 40}, new long[] {500, 800, 50}, false);
+        try {
+            domain.intersect(other);
+            fail("Should not be allowed");
+        } catch (IllegalArgumentException e) {
+            assertNotNull(e.getMessage());
+        }
+    }
+
     /**
      * Tests {@link GridExtent#slice(DirectPosition, int[])}.
      */
diff --git 
a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java 
b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
index 195d419288..746408fe44 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
@@ -536,6 +536,11 @@ public final class Errors extends IndexedResourceBundle {
          */
         public static final short MismatchedArrayLengths = 77;
 
+        /**
+         * Mismatched axes “{1}” and “{2}” at dimension {0}.
+         */
+        public static final short MismatchedAxes_3 = 200;
+
         /**
          * The coordinate reference system must be the same for all objects.
          */
diff --git 
a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
 
b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
index 9919d73574..41715c196c 100644
--- 
a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
+++ 
b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
@@ -118,6 +118,7 @@ InvalidVersionIdentifier_1        = \u201c{0}\u201d is an 
invalid version identi
 KeyCollision_1                    = Key \u201c{0}\u201d is associated twice to 
different values.
 MandatoryAttribute_2              = Attribute \u201c{0}\u201d is mandatory for 
an object of type \u2018{1}\u2019.
 MismatchedArrayLengths            = Mismatched array lengths.
+MismatchedAxes_3                  = Mismatched axes \u201c{1}\u201d and 
\u201c{2}\u201d at dimension {0}.
 MismatchedCRS                     = The coordinate reference system must be 
the same for all objects.
 MismatchedDimension_2             = Mismatched object dimensions: {0}D and 
{1}D.
 MismatchedDimension_3             = Argument \u2018{0}\u2019 has {2} 
dimension{2,choice,1#|2#s}, while {1} was expected.
diff --git 
a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
 
b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
index 2f74f10f84..6a056fe34b 100644
--- 
a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
+++ 
b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
@@ -115,6 +115,7 @@ InvalidVersionIdentifier_1        = 
\u00ab\u202f{0}\u202f\u00bb n\u2019est pas u
 KeyCollision_1                    = La cl\u00e9 \u00ab\u202f{0}\u202f\u00bb 
est associ\u00e9e deux fois \u00e0 des valeurs diff\u00e9rentes.
 MandatoryAttribute_2              = L\u2019attribut 
\u00ab\u202f{0}\u202f\u00bb est obligatoire pour un objet de type 
\u2018{1}\u2019.
 MismatchedArrayLengths            = Les dimensions des tableaux ne 
correspondent pas.
+MismatchedAxes_3                  = Les axes \u00ab\u202f{1}\u202f\u00bb et 
\u00ab\u202f{2}\u202f\u00bb \u00e0 la dimension {0} ne correspondent pas.
 MismatchedCRS                     = Le syst\u00e8me de r\u00e9f\u00e9rence des 
coordonn\u00e9es doit \u00eatre le m\u00eame pour tous les objets.
 MismatchedDimension_2             = Les dimensions des objets ({0}D et {1}D) 
ne concordent pas.
 MismatchedDimension_3             = L\u2019argument \u2018{0}\u2019 a {2} 
dimension{2,choice,1#|2#s}, alors qu\u2019on en attendait {1}.

Reply via email to