Make Function.equals test for exact equality instead of equivalence

Actually, testing for equivalence was only partially implemented (by
considering the commutative property for some FunctionOperators).

Additionally, should it be needed in the future, equivalence testing
would probably be better implemented as a separate, specialized method
anyway.
This commit is contained in:
Riccardo Azzolini 2019-10-25 22:30:10 +02:00
parent d8f401cb22
commit 0c316226e6
14 changed files with 37 additions and 50 deletions

View File

@ -21,6 +21,16 @@ public interface Function {
@Override
String toString();
/**
* Indicates whether some other object is a <code>Function</code> which is <em>identical</em> to this one.
* <p>
* Two functions which are not identical, but only equivalent (due to, for example, the commutative property) aren't
* considered equal.
*
* @param o the other object to compare.
* @return <code>true</code> if the other object is identical to this <code>Function</code>, <code>false</code>
* otherwise.
*/
@Override
boolean equals(Object o);

View File

@ -140,7 +140,5 @@ public abstract class FunctionDynamic implements Function {
}
@Override
public boolean equals(final Object o) {
return false;
}
public abstract boolean equals(Object o);
}

View File

@ -21,11 +21,7 @@ public class Multiplication extends FunctionOperator {
public boolean equals(final Object o) {
if (o instanceof Multiplication) {
final FunctionOperator f = (FunctionOperator) o;
if (parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2())) {
return true;
} else if (parameter1.equals(f.getParameter2()) && parameter2.equals(f.getParameter1())) {
return true;
}
return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2());
}
return false;
}

View File

@ -17,7 +17,7 @@ public class RootSquare extends FunctionOperator {
@Override
public boolean equals(final Object o) {
if (o instanceof Root || o instanceof RootSquare) {
if (o instanceof RootSquare) {
final FunctionOperator f = (FunctionOperator) o;
return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2());
}

View File

@ -16,11 +16,7 @@ public class Sum extends FunctionOperator {
public boolean equals(final Object o) {
if (o instanceof Sum) {
final FunctionOperator f = (FunctionOperator) o;
if (parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2())) {
return true;
} else if (parameter1.equals(f.getParameter2()) && parameter2.equals(f.getParameter1())) {
return true;
}
return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2());
}
return false;
}

View File

@ -28,7 +28,7 @@ public class Undefined implements Function {
@Override
public boolean equals(final Object o) {
return false;
return o instanceof Undefined;
}
@Override

View File

@ -60,11 +60,7 @@ public class Equation extends FunctionOperator {
public boolean equals(final Object o) {
if (o instanceof Equation) {
final FunctionOperator f = (FunctionOperator) o;
if (parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2())) {
return true;
} else if (parameter1.equals(f.getParameter2()) && parameter2.equals(f.getParameter1())) {
return true;
}
return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2());
}
return false;
}

View File

@ -24,20 +24,23 @@ public class EquationsSystem extends FunctionDynamic {
@Override
public boolean equals(final Object o) {
if (o instanceof EquationsSystem) {
if (!(o instanceof EquationsSystem)) {
return false;
}
final FunctionDynamic f = (FunctionDynamic) o;
if (functions.length != f.getParametersLength()) {
return false;
}
for (int i = 0; i < functions.length; i++) {
if (!functions[i].equals(f.getParameter(i))) {
return false;
}
}
return true;
}
return false;
}
@Override
public EquationsSystem clone() {

View File

@ -18,9 +18,7 @@ public class ArcCosine extends FunctionSingle {
public boolean equals(final Object o) {
if (o instanceof ArcCosine) {
final FunctionSingle f = (FunctionSingle) o;
if (parameter.equals(f.getParameter())) {
return true;
}
return parameter.equals(f.getParameter());
}
return false;
}

View File

@ -18,9 +18,7 @@ public class ArcSine extends FunctionSingle {
public boolean equals(final Object o) {
if (o instanceof ArcSine) {
final FunctionSingle f = (FunctionSingle) o;
if (parameter.equals(f.getParameter())) {
return true;
}
return parameter.equals(f.getParameter());
}
return false;
}

View File

@ -18,9 +18,7 @@ public class ArcTangent extends FunctionSingle {
public boolean equals(final Object o) {
if (o instanceof ArcTangent) {
final FunctionSingle f = (FunctionSingle) o;
if (parameter.equals(f.getParameter())) {
return true;
}
return parameter.equals(f.getParameter());
}
return false;
}

View File

@ -18,9 +18,7 @@ public class Cosine extends FunctionSingle {
public boolean equals(final Object o) {
if (o instanceof Cosine) {
final FunctionSingle f = (FunctionSingle) o;
if (parameter.equals(f.getParameter())) {
return true;
}
return parameter.equals(f.getParameter());
}
return false;
}

View File

@ -19,9 +19,7 @@ public class Sine extends FunctionSingle {
public boolean equals(final Object o) {
if (o instanceof Sine) {
final FunctionSingle f = (FunctionSingle) o;
if (parameter.equals(f.getParameter())) {
return true;
}
return parameter.equals(f.getParameter());
}
return false;
}

View File

@ -17,9 +17,7 @@ public class Tangent extends FunctionSingle {
public boolean equals(final Object o) {
if (o instanceof Tangent) {
final FunctionSingle f = (FunctionSingle) o;
if (parameter.equals(f.getParameter())) {
return true;
}
return parameter.equals(f.getParameter());
}
return false;
}