Author: erans
Date: Thu Jan  6 15:30:23 2011
New Revision: 1055911

URL: http://svn.apache.org/viewvc?rev=1055911&view=rev
Log:
MATH-468
Utility methods to check that an argument is not NaN nor infinite.

Added:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/NotFiniteNumberException.java
   (with props)
Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java
    
commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties
    
commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java

Added: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/NotFiniteNumberException.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/NotFiniteNumberException.java?rev=1055911&view=auto
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/NotFiniteNumberException.java
 (added)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/NotFiniteNumberException.java
 Thu Jan  6 15:30:23 2011
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.exception;
+
+import org.apache.commons.math.exception.util.Localizable;
+import org.apache.commons.math.exception.util.LocalizedFormats;
+
+/**
+ * Exception to be thrown when a number is not finite.
+ *
+ * @since 3.0
+ * @version $Revision$ $Date$
+ */
+public class NotFiniteNumberException extends MathIllegalNumberException {
+    /** Serializable version Id. */
+    private static final long serialVersionUID = -6100997100383932834L;
+
+    /**
+     * Construct the exception.
+     *
+     * @param wrong Value that is infinite or NaN.
+     * @param args Optional arguments.
+     */
+    public NotFiniteNumberException(Number wrong,
+                                    Object ... arguments) {
+        this(null, wrong, arguments);
+    }
+
+    /**
+     * Construct the exception with a specific context.
+     *
+     * @param specific Specific contexte pattern.
+     * @param wrong Value that is infinite or NaN.
+     * @param args Optional arguments.
+     */
+    public NotFiniteNumberException(Localizable specific,
+                                    Number wrong,
+                                    Object ... arguments) {
+        super(specific, LocalizedFormats.NOT_FINITE_NUMBER,
+              wrong, arguments);
+    }
+}

Propchange: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/NotFiniteNumberException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java?rev=1055911&r1=1055910&r2=1055911&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java
 Thu Jan  6 15:30:23 2011
@@ -114,6 +114,8 @@ public enum LocalizedFormats implements 
     INDEX_NOT_POSITIVE("index ({0}) is not positive"),
     INDEX_OUT_OF_RANGE("index {0} out of allowed range [{1}, {2}]"),
     INDEX("index ({0})"), /* keep */
+    NOT_FINITE_NUMBER("{0} is not a finite number"), /* keep */
+    ARRAY_ELEMENT("value {0} at index {1}"), /* keep */
     INFINITE_ARRAY_ELEMENT("Array contains an infinite element, {0} at index 
{1}"),
     INFINITE_VALUE_CONVERSION("cannot convert infinite value"),
     INITIAL_CAPACITY_NOT_POSITIVE("initial capacity ({0}) is not positive"),

Modified: 
commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties?rev=1055911&r1=1055910&r2=1055911&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties
 (original)
+++ 
commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties
 Thu Jan  6 15:30:23 2011
@@ -86,6 +86,8 @@ INDEX_LARGER_THAN_MAX = l''index sp\u00e
 INDEX_NOT_POSITIVE = l''indice ({0}) n''est pas positif
 INDEX_OUT_OF_RANGE = l''indice ({0}) est hors du domaine autoris\u00e9 [{1}, 
{2}]
 INDEX = indice ({0})
+NOT_FINITE_NUMBER = {0} n''est pas un nombre fini
+ARRAY_ELEMENT = valeur {0} à l''indice {1}
 INFINITE_ARRAY_ELEMENT = le tableau contient l''\u00e9l\u00e9ment infini {0} 
\u00e0 l''index {1}
 INFINITE_VALUE_CONVERSION = les valeurs infinies ne peuvent \u00eatre 
converties
 INITIAL_CAPACITY_NOT_POSITIVE = la capacit\u00e9 initiale ({0}) n''est pas 
positive

Modified: 
commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java?rev=1055911&r1=1055910&r2=1055911&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
 (original)
+++ 
commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
 Thu Jan  6 15:30:23 2011
@@ -27,6 +27,7 @@ import org.apache.commons.math.exception
 import org.apache.commons.math.exception.MathIllegalArgumentException;
 import org.apache.commons.math.exception.MathArithmeticException;
 import org.apache.commons.math.exception.MathRuntimeException;
+import org.apache.commons.math.exception.NotFiniteNumberException;
 import org.apache.commons.math.random.RandomDataImpl;
 
 /**
@@ -1515,6 +1516,46 @@ public final class MathUtilsTest extends
         }
     }
 
+    public void testCheckFinite() {
+        try {
+            MathUtils.checkFinite(Double.POSITIVE_INFINITY);
+            fail("an exception should have been thrown");
+        } catch (NotFiniteNumberException e) {
+            // Expected
+        }
+        try {
+            MathUtils.checkFinite(Double.NEGATIVE_INFINITY);
+            fail("an exception should have been thrown");
+        } catch (NotFiniteNumberException e) {
+            // Expected
+        }
+        try {
+            MathUtils.checkFinite(Double.NaN);
+            fail("an exception should have been thrown");
+        } catch (NotFiniteNumberException e) {
+            // Expected
+        }
+
+        try {
+            MathUtils.checkFinite(new double[] {0, -1, 
Double.POSITIVE_INFINITY, -2, 3});
+            fail("an exception should have been thrown");
+        } catch (NotFiniteNumberException e) {
+            // Expected
+        }
+        try {
+            MathUtils.checkFinite(new double[] {1, Double.NEGATIVE_INFINITY, 
-2, 3});
+            fail("an exception should have been thrown");
+        } catch (NotFiniteNumberException e) {
+            // Expected
+        }
+        try {
+            MathUtils.checkFinite(new double[] {4, 3, -1, Double.NaN, -2, 1});
+            fail("an exception should have been thrown");
+        } catch (NotFiniteNumberException e) {
+            // Expected
+        }
+    }
+
     public void testSortInPlace() {
         final double[] x1 = {2,   5,  -3, 1,  4};
         final double[] x2 = {4,  25,   9, 1, 16};


Reply via email to