You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/12/13 13:39:07 UTC

[commons-geometry] 04/07: GEOMETRY-51: adding check for invalid epsilon value in EpsilonDoublePrecisionContext constructor; also making Serializable per Spotbugs report

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

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-geometry.git

commit feed702f3266e947835e7685f7b9af30d7849b86
Author: Matt Juntunen <ma...@hotmail.com>
AuthorDate: Fri Dec 13 00:49:16 2019 -0500

    GEOMETRY-51: adding check for invalid epsilon value in EpsilonDoublePrecisionContext constructor; also making Serializable per Spotbugs report
---
 .../precision/EpsilonDoublePrecisionContext.java    | 12 +++++++++++-
 .../EpsilonDoublePrecisionContextTest.java          | 21 +++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/precision/EpsilonDoublePrecisionContext.java b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/precision/EpsilonDoublePrecisionContext.java
index dfc9b34..cc45d27 100644
--- a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/precision/EpsilonDoublePrecisionContext.java
+++ b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/precision/EpsilonDoublePrecisionContext.java
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.geometry.core.precision;
 
+import java.io.Serializable;
+
 import org.apache.commons.numbers.core.Precision;
 
 /** Simple {@link DoublePrecisionContext} subclass that uses an absolute epsilon value to
@@ -28,7 +30,11 @@ import org.apache.commons.numbers.core.Precision;
  *
  * @see Precision#compareTo(double, double, double)
  */
-public class EpsilonDoublePrecisionContext extends DoublePrecisionContext {
+public class EpsilonDoublePrecisionContext extends DoublePrecisionContext implements Serializable {
+
+    /** Serializable UID. */
+    private static final long serialVersionUID = 20191212L;
+
     /** Epsilon value. */
     private final double epsilon;
 
@@ -36,8 +42,12 @@ public class EpsilonDoublePrecisionContext extends DoublePrecisionContext {
      * @param eps Epsilon value. Numbers are considered equal if there is no
      *      floating point value strictly between them or if their difference is less
      *      than or equal to this value.
+     * @throws IllegalArgumentException if the given epsilon value is infinite, NaN, or negative
      */
     public EpsilonDoublePrecisionContext(final double eps) {
+        if (!Double.isFinite(eps) || eps < 0.0) {
+            throw new IllegalArgumentException("Invalid epsilon value: " + eps);
+        }
         this.epsilon = eps;
     }
 
diff --git a/commons-geometry-core/src/test/java/org/apache/commons/geometry/core/precision/EpsilonDoublePrecisionContextTest.java b/commons-geometry-core/src/test/java/org/apache/commons/geometry/core/precision/EpsilonDoublePrecisionContextTest.java
index ff75ee8..224a2a8 100644
--- a/commons-geometry-core/src/test/java/org/apache/commons/geometry/core/precision/EpsilonDoublePrecisionContextTest.java
+++ b/commons-geometry-core/src/test/java/org/apache/commons/geometry/core/precision/EpsilonDoublePrecisionContextTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.geometry.core.precision;
 
+import org.apache.commons.geometry.core.GeometryTestUtils;
 import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
 import org.junit.Assert;
 import org.junit.Test;
@@ -36,6 +37,26 @@ public class EpsilonDoublePrecisionContextTest {
     }
 
     @Test
+    public void testInvalidEpsilonValues() {
+        // act/assert
+        GeometryTestUtils.assertThrows(() -> {
+            new EpsilonDoublePrecisionContext(-1.0);
+        }, IllegalArgumentException.class);
+
+        GeometryTestUtils.assertThrows(() -> {
+            new EpsilonDoublePrecisionContext(Double.NaN);
+        }, IllegalArgumentException.class, "Invalid epsilon value: NaN");
+
+        GeometryTestUtils.assertThrows(() -> {
+            new EpsilonDoublePrecisionContext(Double.POSITIVE_INFINITY);
+        }, IllegalArgumentException.class, "Invalid epsilon value: Infinity");
+
+        GeometryTestUtils.assertThrows(() -> {
+            new EpsilonDoublePrecisionContext(Double.NEGATIVE_INFINITY);
+        }, IllegalArgumentException.class, "Invalid epsilon value: -Infinity");
+    }
+
+    @Test
     public void testSign() {
         // arrange
         double eps = 1e-2;