MATH-337 Equals methods rely on catching ClassCastException rather than using instanceof check

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@922713 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2010-03-14 01:26:13 +00:00
parent 4a4ec2c71c
commit 88e4b16f64
8 changed files with 35 additions and 92 deletions

View File

@ -253,27 +253,18 @@ public class Complex implements FieldElement<Complex>, Serializable {
*/
@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;
}
/**

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -192,22 +192,13 @@ public class LinearConstraint implements Serializable {
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} */

View File

@ -109,20 +109,12 @@ public class LinearObjectiveFunction implements Serializable {
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} */

View File

@ -538,12 +538,7 @@ class SimplexTableau implements Serializable {
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 Serializable {
f.equals(rhs.f) &&
constraints.equals(rhs.constraints) &&
tableau.equals(rhs.tableau);
} catch (ClassCastException ex) {
// ignore exception
return false;
}
return false;
}
/** {@inheritDoc} */

View File

@ -264,14 +264,14 @@ public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Seri
/** {@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} */

View File

@ -158,10 +158,7 @@ public class TransformerMap implements NumberTransformer, Serializable {
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 NumberTransformer, Serializable {
}
}
return true;
} catch (ClassCastException cce) {
return false;
}
return false;
}
/** {@inheritDoc} */