Implement equals (and hashCode) in PatternRule and patterns for testing

This commit is contained in:
Riccardo Azzolini 2018-11-24 20:47:52 +01:00
parent 6c8323daf9
commit b959fac770
22 changed files with 327 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@ -67,4 +68,21 @@ public class PatternRule implements Rule {
.map(replacement -> replacement.replace(mathContext, subFunctions))
.collect(Collectors.toCollection(ObjectArrayList::new));
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof PatternRule)) {
return false;
}
final PatternRule other = (PatternRule) o;
return ruleName.equals(other.ruleName)
&& ruleType == other.ruleType
&& target.equals(other.target)
&& replacements.equals(other.replacements);
}
@Override
public int hashCode() {
return Objects.hash(ruleName, ruleType, target, replacements);
}
}

View File

@ -7,6 +7,7 @@ import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -31,4 +32,18 @@ public class ArcCosinePattern extends VisitorPattern {
argument.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof ArcCosinePattern)) {
return false;
}
final ArcCosinePattern other = (ArcCosinePattern) o;
return argument.equals(other.argument);
}
@Override
public int hashCode() {
return Objects.hash(argument);
}
}

View File

@ -7,6 +7,7 @@ import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -31,4 +32,18 @@ public class ArcSinePattern extends VisitorPattern {
argument.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof ArcSinePattern)) {
return false;
}
final ArcSinePattern other = (ArcSinePattern) o;
return argument.equals(other.argument);
}
@Override
public int hashCode() {
return Objects.hash(argument);
}
}

View File

@ -7,6 +7,7 @@ import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -31,4 +32,18 @@ public class ArcTangentPattern extends VisitorPattern {
argument.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof ArcTangentPattern)) {
return false;
}
final ArcTangentPattern other = (ArcTangentPattern) o;
return argument.equals(other.argument);
}
@Override
public int hashCode() {
return Objects.hash(argument);
}
}

View File

@ -7,6 +7,7 @@ import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -33,4 +34,18 @@ public class ConstantPattern extends VisitorPattern {
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Variable(mathContext, symbol, Variable.V_TYPE.CONSTANT);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof ConstantPattern)) {
return false;
}
final ConstantPattern other = (ConstantPattern) o;
return symbol == other.symbol;
}
@Override
public int hashCode() {
return Objects.hash(symbol);
}
}

View File

@ -7,6 +7,7 @@ import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -31,4 +32,18 @@ public class CosinePattern extends VisitorPattern {
argument.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof CosinePattern)) {
return false;
}
final CosinePattern other = (CosinePattern) o;
return argument.equals(other.argument);
}
@Override
public int hashCode() {
return Objects.hash(argument);
}
}

View File

@ -8,6 +8,7 @@ import it.cavallium.warppi.math.rules.dsl.PatternUtils;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -35,4 +36,18 @@ public class DivisionPattern extends VisitorPattern {
divisor.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof DivisionPattern)) {
return false;
}
final DivisionPattern other = (DivisionPattern) o;
return dividend.equals(other.dividend) && divisor.equals(other.divisor);
}
@Override
public int hashCode() {
return Objects.hash(dividend, divisor);
}
}

View File

@ -9,6 +9,7 @@ import it.cavallium.warppi.math.rules.dsl.PatternUtils;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -36,4 +37,18 @@ public class EquationPattern extends VisitorPattern {
right.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof EquationPattern)) {
return false;
}
final EquationPattern other = (EquationPattern) o;
return left.equals(other.left) && right.equals(other.right);
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
}

View File

@ -47,4 +47,18 @@ public class EquationsSystemPattern extends VisitorPattern {
.toArray(Function[]::new);
return new EquationsSystem(mathContext, functions);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof EquationsSystemPattern)) {
return false;
}
final EquationsSystemPattern other = (EquationsSystemPattern) o;
return Arrays.equals(patterns, other.patterns);
}
@Override
public int hashCode() {
return Arrays.hashCode(patterns);
}
}

View File

@ -8,6 +8,7 @@ import it.cavallium.warppi.math.rules.dsl.PatternUtils;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -35,4 +36,18 @@ public class LogarithmPattern extends VisitorPattern {
argument.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof LogarithmPattern)) {
return false;
}
final LogarithmPattern other = (LogarithmPattern) o;
return base.equals(other.base) && argument.equals(other.argument);
}
@Override
public int hashCode() {
return Objects.hash(base, argument);
}
}

View File

@ -8,6 +8,7 @@ import it.cavallium.warppi.math.rules.dsl.PatternUtils;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -35,4 +36,18 @@ public class MultiplicationPattern extends VisitorPattern {
right.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof MultiplicationPattern)) {
return false;
}
final MultiplicationPattern other = (MultiplicationPattern) o;
return left.equals(other.left) && right.equals(other.right);
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
}

View File

@ -7,6 +7,7 @@ import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -31,4 +32,18 @@ public class NegativePattern extends VisitorPattern {
inner.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof NegativePattern)) {
return false;
}
final NegativePattern other = (NegativePattern) o;
return inner.equals(other.inner);
}
@Override
public int hashCode() {
return Objects.hash(inner);
}
}

View File

@ -8,6 +8,7 @@ import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -33,4 +34,18 @@ public class NumberPattern extends VisitorPattern {
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Number(mathContext, value);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof NumberPattern)) {
return false;
}
final NumberPattern other = (NumberPattern) o;
return value.compareTo(other.value) == 0;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@ -8,6 +8,7 @@ import it.cavallium.warppi.math.rules.dsl.PatternUtils;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -35,4 +36,18 @@ public class PowerPattern extends VisitorPattern {
exponent.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof PowerPattern)) {
return false;
}
final PowerPattern other = (PowerPattern) o;
return base.equals(other.base) && exponent.equals(other.exponent);
}
@Override
public int hashCode() {
return Objects.hash(base, exponent);
}
}

View File

@ -11,6 +11,7 @@ import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -49,4 +50,18 @@ public class RootPattern extends VisitorPattern {
return new Root(mathContext, newDegree, newRadicand);
}
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof RootPattern)) {
return false;
}
final RootPattern other = (RootPattern) o;
return degree.equals(other.degree) && radicand.equals(other.radicand);
}
@Override
public int hashCode() {
return Objects.hash(degree, radicand);
}
}

View File

@ -7,6 +7,7 @@ import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -31,4 +32,18 @@ public class SinePattern extends VisitorPattern {
argument.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof SinePattern)) {
return false;
}
final SinePattern other = (SinePattern) o;
return argument.equals(other.argument);
}
@Override
public int hashCode() {
return Objects.hash(argument);
}
}

View File

@ -6,6 +6,7 @@ import it.cavallium.warppi.math.rules.dsl.Pattern;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -29,4 +30,18 @@ public class SubFunctionPattern implements Pattern {
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return subFunctions.get(name);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof SubFunctionPattern)) {
return false;
}
final SubFunctionPattern other = (SubFunctionPattern) o;
return name.equals(other.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}

View File

@ -8,6 +8,7 @@ import it.cavallium.warppi.math.rules.dsl.PatternUtils;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -35,4 +36,18 @@ public class SubtractionPattern extends VisitorPattern {
right.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof SubtractionPattern)) {
return false;
}
final SubtractionPattern other = (SubtractionPattern) o;
return left.equals(other.left) && right.equals(other.right);
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
}

View File

@ -8,6 +8,7 @@ import it.cavallium.warppi.math.rules.dsl.PatternUtils;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -35,4 +36,18 @@ public class SumPattern extends VisitorPattern {
right.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof SumPattern)) {
return false;
}
final SumPattern other = (SumPattern) o;
return left.equals(other.left) && right.equals(other.right);
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
}

View File

@ -8,6 +8,7 @@ import it.cavallium.warppi.math.rules.dsl.PatternUtils;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -35,4 +36,18 @@ public class SumSubtractionPattern extends VisitorPattern {
right.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof SumSubtractionPattern)) {
return false;
}
final SumSubtractionPattern other = (SumSubtractionPattern) o;
return left.equals(other.left) && right.equals(other.right);
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
}

View File

@ -7,6 +7,7 @@ import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
@ -31,4 +32,18 @@ public class TangentPattern extends VisitorPattern {
argument.replace(mathContext, subFunctions)
);
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof TangentPattern)) {
return false;
}
final TangentPattern other = (TangentPattern) o;
return argument.equals(other.argument);
}
@Override
public int hashCode() {
return Objects.hash(argument);
}
}

View File

@ -22,4 +22,14 @@ public class UndefinedPattern extends VisitorPattern {
public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
return new Undefined(mathContext);
}
@Override
public boolean equals(final Object o) {
return o instanceof UndefinedPattern;
}
@Override
public int hashCode() {
return UndefinedPattern.class.hashCode();
}
}