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 5603926dfbdeefd4482c14a712fa10d9469924fc
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Wed Jun 14 10:30:22 2023 +0200

    Make `SampleDimension.Builder` more robust to cases where the given range 
is empty.
---
 .../org/apache/sis/coverage/SampleDimension.java    | 16 ++++++++--------
 .../java/org/apache/sis/util/ArgumentChecks.java    | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/SampleDimension.java 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/SampleDimension.java
index 4ec24af099..208c5791c8 100644
--- 
a/core/sis-feature/src/main/java/org/apache/sis/coverage/SampleDimension.java
+++ 
b/core/sis-feature/src/main/java/org/apache/sis/coverage/SampleDimension.java
@@ -162,9 +162,9 @@ public class SampleDimension implements Serializable {
      *
      * <p>Note that {@link Builder} provides a more convenient way to create 
sample dimensions.</p>
      *
-     * @param name        an identification for the sample dimension.
-     * @param background  the background value, or {@code null} if none.
-     * @param categories  the list of categories. May be empty if none.
+     * @param  name        an identification for the sample dimension.
+     * @param  background  the background value, or {@code null} if none.
+     * @param  categories  the list of categories. May be empty if none.
      * @throws IllegalSampleDimensionException if two or more categories have 
overlapping sample value range.
      */
     public SampleDimension(final GenericName name, final Number background, 
final Collection<? extends Category> categories) {
@@ -544,7 +544,7 @@ public class SampleDimension implements Serializable {
      *
      * @author  Martin Desruisseaux (IRD, Geomatys)
      * @author  Alexis Manin (Geomatys)
-     * @version 1.2
+     * @version 1.4
      * @since   1.0
      */
     public static class Builder {
@@ -1011,7 +1011,7 @@ public class SampleDimension implements Serializable {
          * @since 1.1
          */
         public Builder mapQualitative(CharSequence name, final NumberRange<?> 
samples, final float converted) {
-            ArgumentChecks.ensureNonNull("samples", samples);
+            ArgumentChecks.ensureNonEmpty("samples", samples);
             final int ordinal = MathFunctions.toNanOrdinal(converted);
             if (!toNaN.add(ordinal)) {
                 throw new 
IllegalArgumentException(Errors.format(Errors.Keys.ValueAlreadyDefined_1, "NaN 
#" + ordinal));
@@ -1050,8 +1050,8 @@ public class SampleDimension implements Serializable {
          * @throws IllegalArgumentException if the range is invalid.
          */
         public Builder addQuantitative(final CharSequence name, final 
NumberRange<?> samples, final NumberRange<?> converted) {
-            ArgumentChecks.ensureNonNull("samples", samples);
-            ArgumentChecks.ensureNonNull("converted", converted);
+            ArgumentChecks.ensureNonEmpty("samples", samples);
+            ArgumentChecks.ensureNonEmpty("converted", converted);
             /*
              * We need to perform calculation using the same "included versus 
excluded" characteristics for sample
              * and converted values. We pickup the characteristics of the 
sample values range (except for avoiding
@@ -1063,7 +1063,7 @@ public class SampleDimension implements Serializable {
             final double Δvalue    = converted.getMaxDouble(isMaxIncluded) - 
minValue;
             final double minSample =   samples.getMinDouble(isMinIncluded);
             final double Δsample   =   samples.getMaxDouble(isMaxIncluded) - 
minSample;
-            final double scale     = Δvalue / Δsample;
+            final double scale     = (Δvalue != 0) ? Δvalue / Δsample : 1;
             final TransferFunction transferFunction = new TransferFunction();
             transferFunction.setScale(scale);
             transferFunction.setOffset(Math.fma(-scale, minSample, minValue));
diff --git 
a/core/sis-utility/src/main/java/org/apache/sis/util/ArgumentChecks.java 
b/core/sis-utility/src/main/java/org/apache/sis/util/ArgumentChecks.java
index 800de42d5d..019a89d421 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/ArgumentChecks.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/ArgumentChecks.java
@@ -215,6 +215,27 @@ public final class ArgumentChecks extends Static {
         }
     }
 
+    /**
+     * Makes sure that given object is non-null and non-empty.
+     * If it is null, then a {@link NullArgumentException} is thrown.
+     * Otherwise if it {@linkplain Emptiable#isEmpty() is empty}, then an 
{@link IllegalArgumentException} is thrown.
+     *
+     * @param  name     the name of the argument to be checked. Used only if 
an exception is thrown.
+     * @param  toCheck  the user argument to check against null value and 
empty object.
+     * @throws NullArgumentException if {@code toCheck} is null.
+     * @throws IllegalArgumentException if {@code toCheck} is empty.
+     *
+     * @since 1.4
+     */
+    public static void ensureNonEmpty(final String name, final Emptiable 
toCheck) {
+        if (toCheck == null) {
+            throw new 
NullArgumentException(Errors.format(Errors.Keys.NullArgument_1, name));
+        }
+        if (toCheck.isEmpty()) {
+            throw new 
IllegalArgumentException(Errors.format(Errors.Keys.EmptyArgument_1, name));
+        }
+    }
+
     /**
      * Ensures that the given {@code values} array is non-null and non-empty. 
This method can also ensures that all values
      * are between the given bounds (inclusive) and are distinct. The distinct 
values requirement is useful for validating

Reply via email to