Author: sebb
Date: Sun Mar 14 01:26:13 2010
New Revision: 922713

URL: http://svn.apache.org/viewvc?rev=922713&view=rev
Log:
MATH-337 Equals methods rely on catching ClassCastException rather than using 
instanceof check

Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/Fraction.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3D.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearConstraint.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearObjectiveFunction.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/TransformerMap.java

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java?rev=922713&r1=922712&r2=922713&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
 Sun Mar 14 01:26:13 2010
@@ -253,27 +253,18 @@ public class Complex implements FieldEle
      */
     @Override
     public boolean equals(Object other) {
-        boolean ret;
-
         if (this == other) {
-            ret = true;
-        } else if (other == null) {
-            ret = false;
-        } else  {
-            try {
-                Complex rhs = (Complex)other;
-                if (rhs.isNaN()) {
-                    ret = this.isNaN();
-                } else {
-                    ret = (real == rhs.real) && (imaginary == rhs.imaginary);
-                }
-            } catch (ClassCastException ex) {
-                // ignore exception
-                ret = false;
+            return true;
+        }
+        if (other instanceof Complex){
+            Complex rhs = (Complex)other;
+            if (rhs.isNaN()) {
+                return this.isNaN();
+            } else {
+                return (real == rhs.real) && (imaginary == rhs.imaginary);
             }
         }
-
-        return ret;
+        return false;
     }
 
     /**

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/Fraction.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/Fraction.java?rev=922713&r1=922712&r2=922713&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/Fraction.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/Fraction.java
 Sun Mar 14 01:26:13 2010
@@ -338,26 +338,17 @@ public class Fraction
      */
     @Override
     public boolean equals(Object other) {
-        boolean ret;
-
         if (this == other) {
-            ret = true;
-        } else if (other == null) {
-            ret = false;
-        } else {
-            try {
-                // since fractions are always in lowest terms, numerators and
-                // denominators can be compared directly for equality.
-                Fraction rhs = (Fraction)other;
-                ret = (numerator == rhs.numerator) &&
-                    (denominator == rhs.denominator);
-            } catch (ClassCastException ex) {
-                // ignore exception
-                ret = false;
-            }
+            return true;
         }
-
-        return ret;
+        if (other instanceof Fraction) {
+            // since fractions are always in lowest terms, numerators and
+            // denominators can be compared directly for equality.
+            Fraction rhs = (Fraction)other;
+            return (numerator == rhs.numerator) &&
+                (denominator == rhs.denominator);
+        }
+        return false;
     }
 
     /**

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3D.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3D.java?rev=922713&r1=922712&r2=922713&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3D.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3D.java
 Sun Mar 14 01:26:13 2010
@@ -415,24 +415,15 @@ public class Vector3D
       return true;
     }
 
-    if (other == null) {
-      return false;
-    }
-
-    try {
-
+    if (other instanceof Vector3D) {
       final Vector3D rhs = (Vector3D)other;
       if (rhs.isNaN()) {
           return this.isNaN();
       }
 
       return (x == rhs.x) && (y == rhs.y) && (z == rhs.z);
-
-    } catch (ClassCastException ex) {
-        // ignore exception
-        return false;
     }
-
+    return false;
   }
 
   /**

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearConstraint.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearConstraint.java?rev=922713&r1=922712&r2=922713&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearConstraint.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearConstraint.java
 Sun Mar 14 01:26:13 2010
@@ -192,22 +192,13 @@ public class LinearConstraint implements
         return true;
       }
 
-      if (other == null) {
-        return false;
-      }
-
-      try {
-
+      if (other instanceof LinearConstraint) {
           LinearConstraint rhs = (LinearConstraint) other;
           return (relationship == rhs.relationship) &&
                  (value        == rhs.value) &&
                  coefficients.equals(rhs.coefficients);
-
-      } catch (ClassCastException ex) {
-          // ignore exception
-          return false;
       }
-
+      return false;
     }
 
     /** {...@inheritdoc} */

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearObjectiveFunction.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearObjectiveFunction.java?rev=922713&r1=922712&r2=922713&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearObjectiveFunction.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/LinearObjectiveFunction.java
 Sun Mar 14 01:26:13 2010
@@ -109,20 +109,12 @@ public class LinearObjectiveFunction imp
         return true;
       }
 
-      if (other == null) {
-        return false;
-      }
-
-      try {
-
+      if (other instanceof LinearObjectiveFunction) {
           LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
           return (constantTerm == rhs.constantTerm) && 
coefficients.equals(rhs.coefficients);
-
-      } catch (ClassCastException ex) {
-          // ignore exception
-          return false;
       }
 
+      return false;
     }
 
     /** {...@inheritdoc} */

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java?rev=922713&r1=922712&r2=922713&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
 Sun Mar 14 01:26:13 2010
@@ -538,12 +538,7 @@ class SimplexTableau implements Serializ
         return true;
       }
 
-      if (other == null) {
-        return false;
-      }
-
-      try {
-
+      if (other instanceof SimplexTableau) {
           SimplexTableau rhs = (SimplexTableau) other;
           return (restrictToNonNegative  == rhs.restrictToNonNegative) &&
                  (numDecisionVariables   == rhs.numDecisionVariables) &&
@@ -553,12 +548,8 @@ class SimplexTableau implements Serializ
                  f.equals(rhs.f) &&
                  constraints.equals(rhs.constraints) &&
                  tableau.equals(rhs.tableau);
-
-      } catch (ClassCastException ex) {
-          // ignore exception
-          return false;
       }
-
+      return false;
     }
 
     /** {...@inheritdoc} */

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java?rev=922713&r1=922712&r2=922713&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java
 Sun Mar 14 01:26:13 2010
@@ -264,14 +264,14 @@ public class BigReal implements FieldEle
     /** {...@inheritdoc} */
     @Override
     public boolean equals(Object other) {
-        try {
-            if (other == null) {
-                return false;
-            }
-            return d.equals(((BigReal) other).d);
-        } catch (ClassCastException cce) {
-            return false;
+        if (this == other){
+            return true;
         }
+ 
+        if (other instanceof BigReal){
+            return d.equals(((BigReal) other).d);            
+        }
+        return false;
     }
 
     /** {...@inheritdoc} */

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/TransformerMap.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/TransformerMap.java?rev=922713&r1=922712&r2=922713&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/TransformerMap.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/TransformerMap.java
 Sun Mar 14 01:26:13 2010
@@ -158,10 +158,7 @@ public class TransformerMap implements N
         if (this == other) {
             return true;
         }
-        if (other == null) {
-            return false;
-        }
-        try {
+        if (other instanceof TransformerMap) {
             TransformerMap rhs = (TransformerMap) other;
             if (! defaultTransformer.equals(rhs.defaultTransformer)) {
                 return false;
@@ -175,9 +172,8 @@ public class TransformerMap implements N
                 }
             }
             return true;
-        } catch (ClassCastException cce) {
-            return false;
         }
+        return false;
     }
 
     /** {...@inheritdoc} */


Reply via email to