Fix indentation

This commit is contained in:
Riccardo Azzolini 2018-10-16 21:55:24 +02:00
parent 4e5a77eb3e
commit 7e394a84bf
25 changed files with 768 additions and 769 deletions

View File

@ -13,29 +13,29 @@ import it.cavallium.warppi.math.functions.trigonometry.*;
* @param <T> The return type of all <code>visit</code> method overloads.
*/
public interface FunctionVisitor<T> {
T visit(ArcCosine arcCosine);
T visit(ArcSine arcSine);
T visit(ArcTangent arcTangent);
T visit(Cosine cosine);
T visit(Division division);
T visit(EmptyNumber emptyNumber);
T visit(Equation equation);
T visit(EquationsSystem equationsSystem);
T visit(EquationsSystemPart equationsSystemPart);
T visit(Expression expression);
T visit(Joke joke);
T visit(Logarithm logarithm);
T visit(Multiplication multiplication);
T visit(Negative negative);
T visit(Number number);
T visit(Power power);
T visit(Root root);
T visit(RootSquare rootSquare);
T visit(Sine sine);
T visit(Subtraction subtraction);
T visit(SumSubtraction sumSubtraction);
T visit(Sum sum);
T visit(Tangent tangent);
T visit(Undefined undefined);
T visit(Variable variable);
T visit(ArcCosine arcCosine);
T visit(ArcSine arcSine);
T visit(ArcTangent arcTangent);
T visit(Cosine cosine);
T visit(Division division);
T visit(EmptyNumber emptyNumber);
T visit(Equation equation);
T visit(EquationsSystem equationsSystem);
T visit(EquationsSystemPart equationsSystemPart);
T visit(Expression expression);
T visit(Joke joke);
T visit(Logarithm logarithm);
T visit(Multiplication multiplication);
T visit(Negative negative);
T visit(Number number);
T visit(Power power);
T visit(Root root);
T visit(RootSquare rootSquare);
T visit(Sine sine);
T visit(Subtraction subtraction);
T visit(SumSubtraction sumSubtraction);
T visit(Sum sum);
T visit(Tangent tangent);
T visit(Undefined undefined);
T visit(Variable variable);
}

View File

@ -10,22 +10,21 @@ import java.util.Optional;
* Recognizes and generates functions of some specific shape.
*/
public interface Pattern {
/**
* Tries to match this pattern against a function and capture sub-functions.
*
* @param function The function to test the pattern against.
* @return The captured sub-functions, or an empty <code>Optional</code> if
* the pattern doesn't match.
*/
Optional<Map<String, Function>> match(Function function);
/**
* Tries to match this pattern against a function and capture sub-functions.
*
* @param function The function to test the pattern against.
* @return The captured sub-functions, or an empty <code>Optional</code> if
* the pattern doesn't match.
*/
Optional<Map<String, Function>> match(Function function);
/**
* Creates a new function by filling in sub-functions within this pattern.
*
*
* @param mathContext The <code>MathContext</code> used to construct <code>Function</code>s.
* @param subFunctions A map of named sub-functions to be inserted into this pattern.
* @return The resulting function.
*/
Function replace(MathContext mathContext, Map<String, Function> subFunctions);
/**
* Creates a new function by filling in sub-functions within this pattern.
*
* @param mathContext The <code>MathContext</code> used to construct <code>Function</code>s.
* @param subFunctions A map of named sub-functions to be inserted into this pattern.
* @return The resulting function.
*/
Function replace(MathContext mathContext, Map<String, Function> subFunctions);
}

View File

@ -12,61 +12,61 @@ import java.util.Optional;
* Contains helper methods which are useful for writing patterns.
*/
public class PatternUtils {
/**
* Gathers captured sub-functions from two matches, checking for equality
* of ones with the same name.
*
* @param match1 Sub-functions from one match.
* @param match2 Sub-functions from the other match.
* @return A <code>Map</code> containing all sub-functions, or an empty
* <code>Optional</code> if the same name is used to refer to
* non-equal sub-functions in the two matches.
*/
public static Optional<Map<String, Function>> mergeMatches(
final Map<String, Function> match1,
final Map<String, Function> match2
) {
if (!checkSubFunctionEquality(match1, match2)) {
return Optional.empty();
}
/**
* Gathers captured sub-functions from two matches, checking for equality
* of ones with the same name.
*
* @param match1 Sub-functions from one match.
* @param match2 Sub-functions from the other match.
* @return A <code>Map</code> containing all sub-functions, or an empty
* <code>Optional</code> if the same name is used to refer to
* non-equal sub-functions in the two matches.
*/
public static Optional<Map<String, Function>> mergeMatches(
final Map<String, Function> match1,
final Map<String, Function> match2
) {
if (!checkSubFunctionEquality(match1, match2)) {
return Optional.empty();
}
final Map<String, Function> merged = new HashMap<>();
merged.putAll(match1);
merged.putAll(match2);
return Optional.of(merged);
}
final Map<String, Function> merged = new HashMap<>();
merged.putAll(match1);
merged.putAll(match2);
return Optional.of(merged);
}
private static boolean checkSubFunctionEquality(
final Map<String, Function> match1,
final Map<String, Function> match2
) {
for (final Map.Entry<String, Function> leftSubFunction : match1.entrySet()) {
final String key = leftSubFunction.getKey();
if (match2.containsKey(key)
&& !match2.get(key).equals(leftSubFunction.getValue())) {
return false;
}
}
return true;
}
private static boolean checkSubFunctionEquality(
final Map<String, Function> match1,
final Map<String, Function> match2
) {
for (final Map.Entry<String, Function> leftSubFunction : match1.entrySet()) {
final String key = leftSubFunction.getKey();
if (match2.containsKey(key)
&& !match2.get(key).equals(leftSubFunction.getValue())) {
return false;
}
}
return true;
}
/**
* Tries to match the given patterns against the two parameters of a <code>FunctionOperator</code>.
*
* @param functionOperator The <code>FunctionOperator</code> to be matched.
* @param pattern1 The <code>Pattern</code> used to match <code>functionOperator.parameter1</code>.
* @param pattern2 The <code>Pattern</code> used to match <code>functionOperator.parameter2</code>.
* @return The combined result of the two matches.
* @see #mergeMatches(Map, Map)
*/
public static Optional<Map<String, Function>> matchFunctionOperatorParameters(
final FunctionOperator functionOperator,
final Pattern pattern1,
final Pattern pattern2
) {
return pattern1.match(functionOperator.getParameter1())
.flatMap(match1 -> pattern2.match(functionOperator.getParameter2())
.flatMap(match2 -> mergeMatches(match1, match2))
);
}
/**
* Tries to match the given patterns against the two parameters of a <code>FunctionOperator</code>.
*
* @param functionOperator The <code>FunctionOperator</code> to be matched.
* @param pattern1 The <code>Pattern</code> used to match <code>functionOperator.parameter1</code>.
* @param pattern2 The <code>Pattern</code> used to match <code>functionOperator.parameter2</code>.
* @return The combined result of the two matches.
* @see #mergeMatches(Map, Map)
*/
public static Optional<Map<String, Function>> matchFunctionOperatorParameters(
final FunctionOperator functionOperator,
final Pattern pattern1,
final Pattern pattern2
) {
return pattern1.match(functionOperator.getParameter1())
.flatMap(match1 -> pattern2.match(functionOperator.getParameter2())
.flatMap(match2 -> mergeMatches(match1, match2))
);
}
}

View File

@ -16,133 +16,133 @@ import java.util.Optional;
* A <code>Pattern</code> which implements <code>match</code> as a visitor.
*/
public abstract class VisitorPattern implements Pattern, FunctionVisitor<Optional<Map<String, Function>>> {
@Override
public Optional<Map<String, Function>> match(final Function function) {
return function.accept(this);
}
@Override
public Optional<Map<String, Function>> match(final Function function) {
return function.accept(this);
}
@Override
public Optional<Map<String, Function>> visit(final ArcCosine arcCosine) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final ArcCosine arcCosine) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final ArcSine arcSine) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final ArcSine arcSine) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final ArcTangent arcTangent) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final ArcTangent arcTangent) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Cosine cosine) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Cosine cosine) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Division division) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Division division) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final EmptyNumber emptyNumber) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final EmptyNumber emptyNumber) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Equation equation) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Equation equation) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final EquationsSystem equationsSystem) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final EquationsSystem equationsSystem) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final EquationsSystemPart equationsSystemPart) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final EquationsSystemPart equationsSystemPart) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Expression expression) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Expression expression) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Joke joke) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Joke joke) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Logarithm logarithm) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Logarithm logarithm) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Multiplication multiplication) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Multiplication multiplication) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Negative negative) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Negative negative) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Number number) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Number number) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Power power) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Power power) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Root root) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Root root) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final RootSquare rootSquare) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final RootSquare rootSquare) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Sine sine) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Sine sine) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Subtraction subtraction) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Subtraction subtraction) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final SumSubtraction sumSubtraction) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final SumSubtraction sumSubtraction) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Sum sum) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Sum sum) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Tangent tangent) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Tangent tangent) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Undefined undefined) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Undefined undefined) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Variable variable) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final Variable variable) {
return Optional.empty();
}
}

View File

@ -13,22 +13,22 @@ import java.util.Optional;
* Matches and generates the arccosine of another pattern.
*/
public class ArcCosinePattern extends VisitorPattern {
private final Pattern argument;
private final Pattern argument;
public ArcCosinePattern(final Pattern argument) {
this.argument = argument;
}
public ArcCosinePattern(final Pattern argument) {
this.argument = argument;
}
@Override
public Optional<Map<String, Function>> visit(final ArcCosine arcCosine) {
return argument.match(arcCosine.getParameter());
}
@Override
public Optional<Map<String, Function>> visit(final ArcCosine arcCosine) {
return argument.match(arcCosine.getParameter());
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new ArcCosine(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new ArcCosine(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
}

View File

@ -13,22 +13,22 @@ import java.util.Optional;
* Matches and generates the arcsine of another pattern.
*/
public class ArcSinePattern extends VisitorPattern {
private final Pattern argument;
private final Pattern argument;
public ArcSinePattern(final Pattern argument) {
this.argument = argument;
}
public ArcSinePattern(final Pattern argument) {
this.argument = argument;
}
@Override
public Optional<Map<String, Function>> visit(final ArcSine arcSine) {
return argument.match(arcSine.getParameter());
}
@Override
public Optional<Map<String, Function>> visit(final ArcSine arcSine) {
return argument.match(arcSine.getParameter());
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new ArcSine(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new ArcSine(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
}

View File

@ -13,22 +13,22 @@ import java.util.Optional;
* Matches and generates the arctangent of another pattern.
*/
public class ArcTangentPattern extends VisitorPattern {
private final Pattern argument;
private final Pattern argument;
public ArcTangentPattern(final Pattern argument) {
this.argument = argument;
}
public ArcTangentPattern(final Pattern argument) {
this.argument = argument;
}
@Override
public Optional<Map<String, Function>> visit(final ArcTangent arcTangent) {
return argument.match(arcTangent.getParameter());
}
@Override
public Optional<Map<String, Function>> visit(final ArcTangent arcTangent) {
return argument.match(arcTangent.getParameter());
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new ArcTangent(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new ArcTangent(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
}

View File

@ -13,22 +13,22 @@ import java.util.Optional;
* Matches and generates the cosine of another pattern.
*/
public class CosinePattern extends VisitorPattern {
private final Pattern argument;
private final Pattern argument;
public CosinePattern(final Pattern argument) {
this.argument = argument;
}
public CosinePattern(final Pattern argument) {
this.argument = argument;
}
@Override
public Optional<Map<String, Function>> visit(final Cosine cosine) {
return argument.match(cosine.getParameter());
}
@Override
public Optional<Map<String, Function>> visit(final Cosine cosine) {
return argument.match(cosine.getParameter());
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Cosine(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Cosine(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
}

View File

@ -14,25 +14,25 @@ import java.util.Optional;
* Matches and generates a division of two other patterns.
*/
public class DivisionPattern extends VisitorPattern {
private final Pattern dividend;
private final Pattern divisor;
private final Pattern dividend;
private final Pattern divisor;
public DivisionPattern(final Pattern dividend, final Pattern divisor) {
this.dividend = dividend;
this.divisor = divisor;
}
public DivisionPattern(final Pattern dividend, final Pattern divisor) {
this.dividend = dividend;
this.divisor = divisor;
}
@Override
public Optional<Map<String, Function>> visit(final Division division) {
return PatternUtils.matchFunctionOperatorParameters(division, dividend, divisor);
}
@Override
public Optional<Map<String, Function>> visit(final Division division) {
return PatternUtils.matchFunctionOperatorParameters(division, dividend, divisor);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Division(
mathContext,
dividend.replace(mathContext, subFunctions),
divisor.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Division(
mathContext,
dividend.replace(mathContext, subFunctions),
divisor.replace(mathContext, subFunctions)
);
}
}

View File

@ -15,25 +15,25 @@ import java.util.Optional;
* Matches and generates an equation of two other patterns.
*/
public class EquationPattern extends VisitorPattern {
private final Pattern left;
private final Pattern right;
private final Pattern left;
private final Pattern right;
public EquationPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
public EquationPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
@Override
public Optional<Map<String, Function>> visit(final Equation equation) {
return PatternUtils.matchFunctionOperatorParameters(equation, left, right);
}
@Override
public Optional<Map<String, Function>> visit(final Equation equation) {
return PatternUtils.matchFunctionOperatorParameters(equation, left, right);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Equation(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Equation(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
}

View File

@ -16,35 +16,35 @@ import java.util.Optional;
* Matches and generates a system of equations of multiple other patterns.
*/
public class EquationsSystemPattern extends VisitorPattern {
final Pattern[] patterns;
final Pattern[] patterns;
public EquationsSystemPattern(final Pattern[] patterns) {
this.patterns = patterns;
}
public EquationsSystemPattern(final Pattern[] patterns) {
this.patterns = patterns;
}
@Override
public Optional<Map<String, Function>> visit(final EquationsSystem equationsSystem) {
if (patterns.length != equationsSystem.getParametersLength()) {
return Optional.empty();
}
@Override
public Optional<Map<String, Function>> visit(final EquationsSystem equationsSystem) {
if (patterns.length != equationsSystem.getParametersLength()) {
return Optional.empty();
}
Optional<Map<String, Function>> subFunctions = Optional.of(new HashMap<>());
for (int i = 0; i < patterns.length && subFunctions.isPresent(); i++) {
final Pattern curPattern = patterns[i];
final Function curFunction = equationsSystem.getParameter(i);
subFunctions = subFunctions
.flatMap(prevMatch -> curPattern.match(curFunction)
.flatMap(curMatch -> PatternUtils.mergeMatches(prevMatch, curMatch))
);
}
return subFunctions;
}
Optional<Map<String, Function>> subFunctions = Optional.of(new HashMap<>());
for (int i = 0; i < patterns.length && subFunctions.isPresent(); i++) {
final Pattern curPattern = patterns[i];
final Function curFunction = equationsSystem.getParameter(i);
subFunctions = subFunctions
.flatMap(prevMatch -> curPattern.match(curFunction)
.flatMap(curMatch -> PatternUtils.mergeMatches(prevMatch, curMatch))
);
}
return subFunctions;
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
final Function[] functions = Arrays.stream(patterns)
.map(pattern -> pattern.replace(mathContext, subFunctions))
.toArray(Function[]::new);
return new EquationsSystem(mathContext, functions);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
final Function[] functions = Arrays.stream(patterns)
.map(pattern -> pattern.replace(mathContext, subFunctions))
.toArray(Function[]::new);
return new EquationsSystem(mathContext, functions);
}
}

View File

@ -14,25 +14,25 @@ import java.util.Optional;
* Matches and generates a logarithm of base and argument patterns.
*/
public class LogarithmPattern extends VisitorPattern {
private final Pattern base;
private final Pattern argument;
private final Pattern base;
private final Pattern argument;
public LogarithmPattern(final Pattern base, final Pattern argument) {
this.base = base;
this.argument = argument;
}
public LogarithmPattern(final Pattern base, final Pattern argument) {
this.base = base;
this.argument = argument;
}
@Override
public Optional<Map<String, Function>> visit(final Logarithm logarithm) {
return PatternUtils.matchFunctionOperatorParameters(logarithm, base, argument);
}
@Override
public Optional<Map<String, Function>> visit(final Logarithm logarithm) {
return PatternUtils.matchFunctionOperatorParameters(logarithm, base, argument);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Logarithm(
mathContext,
base.replace(mathContext, subFunctions),
argument.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Logarithm(
mathContext,
base.replace(mathContext, subFunctions),
argument.replace(mathContext, subFunctions)
);
}
}

View File

@ -14,25 +14,25 @@ import java.util.Optional;
* Matches and generates a multiplication of two other patterns.
*/
public class MultiplicationPattern extends VisitorPattern {
private final Pattern left;
private final Pattern right;
private final Pattern left;
private final Pattern right;
public MultiplicationPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
public MultiplicationPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
@Override
public Optional<Map<String, Function>> visit(final Multiplication multiplication) {
return PatternUtils.matchFunctionOperatorParameters(multiplication, left, right);
}
@Override
public Optional<Map<String, Function>> visit(final Multiplication multiplication) {
return PatternUtils.matchFunctionOperatorParameters(multiplication, left, right);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Multiplication(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Multiplication(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
}

View File

@ -13,22 +13,22 @@ import java.util.Optional;
* Matches and generates the negative of another pattern.
*/
public class NegativePattern extends VisitorPattern {
private final Pattern inner;
private final Pattern inner;
public NegativePattern(final Pattern inner) {
this.inner = inner;
}
public NegativePattern(final Pattern inner) {
this.inner = inner;
}
@Override
public Optional<Map<String, Function>> visit(final Negative negative) {
return inner.match(negative.getParameter());
}
@Override
public Optional<Map<String, Function>> visit(final Negative negative) {
return inner.match(negative.getParameter());
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Negative(
mathContext,
inner.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Negative(
mathContext,
inner.replace(mathContext, subFunctions)
);
}
}

View File

@ -14,23 +14,23 @@ import java.util.Optional;
* Matches and generates a specific number.
*/
public class NumberPattern extends VisitorPattern {
private final BigDecimal value;
private final BigDecimal value;
public NumberPattern(final BigDecimal value) {
this.value = value;
}
public NumberPattern(final BigDecimal value) {
this.value = value;
}
@Override
public Optional<Map<String, Function>> visit(final Number number) {
if (number.getTerm().compareTo(value) == 0) {
return Optional.of(new HashMap<>());
} else {
return Optional.empty();
}
}
@Override
public Optional<Map<String, Function>> visit(final Number number) {
if (number.getTerm().compareTo(value) == 0) {
return Optional.of(new HashMap<>());
} else {
return Optional.empty();
}
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Number(mathContext, value);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Number(mathContext, value);
}
}

View File

@ -14,25 +14,25 @@ import java.util.Optional;
* Matches and generates a power (exponentiation) of base and exponent patterns.
*/
public class PowerPattern extends VisitorPattern {
private final Pattern base;
private final Pattern exponent;
private final Pattern base;
private final Pattern exponent;
public PowerPattern(final Pattern base, final Pattern exponent) {
this.base = base;
this.exponent = exponent;
}
public PowerPattern(final Pattern base, final Pattern exponent) {
this.base = base;
this.exponent = exponent;
}
@Override
public Optional<Map<String, Function>> visit(final Power power) {
return PatternUtils.matchFunctionOperatorParameters(power, base, exponent);
}
@Override
public Optional<Map<String, Function>> visit(final Power power) {
return PatternUtils.matchFunctionOperatorParameters(power, base, exponent);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Power(
mathContext,
base.replace(mathContext, subFunctions),
exponent.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Power(
mathContext,
base.replace(mathContext, subFunctions),
exponent.replace(mathContext, subFunctions)
);
}
}

View File

@ -19,34 +19,34 @@ import java.util.Optional;
* Also matches and generates functions of type <code>RootSquare</code>.
*/
public class RootPattern extends VisitorPattern {
private final Pattern degree;
private final Pattern radicand;
private final Pattern degree;
private final Pattern radicand;
public RootPattern(final Pattern degree, final Pattern radicand) {
this.degree = degree;
this.radicand = radicand;
}
public RootPattern(final Pattern degree, final Pattern radicand) {
this.degree = degree;
this.radicand = radicand;
}
@Override
public Optional<Map<String, Function>> visit(final Root root) {
return PatternUtils.matchFunctionOperatorParameters(root, degree, radicand);
}
@Override
public Optional<Map<String, Function>> visit(final Root root) {
return PatternUtils.matchFunctionOperatorParameters(root, degree, radicand);
}
@Override
public Optional<Map<String, Function>> visit(RootSquare rootSquare) {
return PatternUtils.matchFunctionOperatorParameters(rootSquare, degree, radicand);
}
@Override
public Optional<Map<String, Function>> visit(RootSquare rootSquare) {
return PatternUtils.matchFunctionOperatorParameters(rootSquare, degree, radicand);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
final Function newDegree = degree.replace(mathContext, subFunctions);
final Function newRadicand = radicand.replace(mathContext, subFunctions);
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
final Function newDegree = degree.replace(mathContext, subFunctions);
final Function newRadicand = radicand.replace(mathContext, subFunctions);
if (newDegree instanceof Number
&& ((Number) newDegree).getTerm().compareTo(new BigDecimal(2)) == 0) {
return new RootSquare(mathContext, newRadicand);
} else {
return new Root(mathContext, newDegree, newRadicand);
}
}
if (newDegree instanceof Number
&& ((Number) newDegree).getTerm().compareTo(new BigDecimal(2)) == 0) {
return new RootSquare(mathContext, newRadicand);
} else {
return new Root(mathContext, newDegree, newRadicand);
}
}
}

View File

@ -13,22 +13,22 @@ import java.util.Optional;
* Matches and generates the sine of another pattern.
*/
public class SinePattern extends VisitorPattern {
private final Pattern argument;
private final Pattern argument;
public SinePattern(final Pattern argument) {
this.argument = argument;
}
public SinePattern(final Pattern argument) {
this.argument = argument;
}
@Override
public Optional<Map<String, Function>> visit(final Sine sine) {
return argument.match(sine.getParameter());
}
@Override
public Optional<Map<String, Function>> visit(final Sine sine) {
return argument.match(sine.getParameter());
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Sine(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Sine(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
}

View File

@ -12,21 +12,21 @@ import java.util.Optional;
* Matches and generates any function as a named sub-function.
*/
public class SubFunctionPattern implements Pattern {
private final String name;
private final String name;
public SubFunctionPattern(final String name) {
this.name = name;
}
public SubFunctionPattern(final String name) {
this.name = name;
}
@Override
public Optional<Map<String, Function>> match(final Function function) {
final HashMap<String, Function> subFunctions = new HashMap<>();
subFunctions.put(name, function);
return Optional.of(subFunctions);
}
@Override
public Optional<Map<String, Function>> match(final Function function) {
final HashMap<String, Function> subFunctions = new HashMap<>();
subFunctions.put(name, function);
return Optional.of(subFunctions);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return subFunctions.get(name);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return subFunctions.get(name);
}
}

View File

@ -14,25 +14,25 @@ import java.util.Optional;
* Matches and generates a subtraction of two other patterns.
*/
public class SubtractionPattern extends VisitorPattern {
private final Pattern left;
private final Pattern right;
private final Pattern left;
private final Pattern right;
public SubtractionPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
public SubtractionPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
@Override
public Optional<Map<String, Function>> visit(final Subtraction subtraction) {
return PatternUtils.matchFunctionOperatorParameters(subtraction, left, right);
}
@Override
public Optional<Map<String, Function>> visit(final Subtraction subtraction) {
return PatternUtils.matchFunctionOperatorParameters(subtraction, left, right);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Subtraction(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Subtraction(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
}

View File

@ -14,25 +14,25 @@ import java.util.Optional;
* Matches and generates a sum of two other patterns.
*/
public class SumPattern extends VisitorPattern {
private final Pattern left;
private final Pattern right;
private final Pattern left;
private final Pattern right;
public SumPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
public SumPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
@Override
public Optional<Map<String, Function>> visit(final Sum sum) {
return PatternUtils.matchFunctionOperatorParameters(sum, left, right);
}
@Override
public Optional<Map<String, Function>> visit(final Sum sum) {
return PatternUtils.matchFunctionOperatorParameters(sum, left, right);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Sum(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Sum(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
}

View File

@ -14,25 +14,25 @@ import java.util.Optional;
* Matches and generates a sum/subtraction (±) of two other patterns.
*/
public class SumSubtractionPattern extends VisitorPattern {
private final Pattern left;
private final Pattern right;
private final Pattern left;
private final Pattern right;
public SumSubtractionPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
public SumSubtractionPattern(final Pattern left, final Pattern right) {
this.left = left;
this.right = right;
}
@Override
public Optional<Map<String, Function>> visit(final SumSubtraction sumSubtraction) {
return PatternUtils.matchFunctionOperatorParameters(sumSubtraction, left, right);
}
@Override
public Optional<Map<String, Function>> visit(final SumSubtraction sumSubtraction) {
return PatternUtils.matchFunctionOperatorParameters(sumSubtraction, left, right);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new SumSubtraction(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new SumSubtraction(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
}

View File

@ -13,22 +13,22 @@ import java.util.Optional;
* Matches and generates the tangent of another pattern.
*/
public class TangentPattern extends VisitorPattern {
private final Pattern argument;
private final Pattern argument;
public TangentPattern(final Pattern argument) {
this.argument = argument;
}
public TangentPattern(final Pattern argument) {
this.argument = argument;
}
@Override
public Optional<Map<String, Function>> visit(final Tangent tangent) {
return argument.match(tangent.getParameter());
}
@Override
public Optional<Map<String, Function>> visit(final Tangent tangent) {
return argument.match(tangent.getParameter());
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Tangent(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Tangent(
mathContext,
argument.replace(mathContext, subFunctions)
);
}
}

View File

@ -13,13 +13,13 @@ import java.util.Optional;
* Matches and generates <code>Undefined</code>.
*/
public class UndefinedPattern extends VisitorPattern {
@Override
public Optional<Map<String, Function>> visit(Undefined undefined) {
return Optional.of(new HashMap<>());
}
@Override
public Optional<Map<String, Function>> visit(Undefined undefined) {
return Optional.of(new HashMap<>());
}
@Override
public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
return new Undefined(mathContext);
}
@Override
public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
return new Undefined(mathContext);
}
}

View File

@ -20,271 +20,271 @@ import java.util.Optional;
import static org.junit.Assert.*;
public class PatternTest {
private final MathContext mathContext = new MathContext();
private final MathContext mathContext = new MathContext();
@Test
public void subFunctionPattern() {
final Pattern pattern = new SubFunctionPattern("x");
@Test
public void subFunctionPattern() {
final Pattern pattern = new SubFunctionPattern("x");
final Function func = new Sum(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 2)
);
final Function func = new Sum(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 2)
);
final Optional<Map<String, Function>> subFunctions = pattern.match(func);
assertTrue(subFunctions.isPresent());
final Optional<Map<String, Function>> subFunctions = pattern.match(func);
assertTrue(subFunctions.isPresent());
assertEquals(func, pattern.replace(mathContext, subFunctions.get()));
}
assertEquals(func, pattern.replace(mathContext, subFunctions.get()));
}
@Test
public void sumPattern() {
final Pattern pattern = new SumPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("y")
);
@Test
public void sumPattern() {
final Pattern pattern = new SumPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("y")
);
final Function shouldNotMatch = new Subtraction(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 2)
);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldNotMatch = new Subtraction(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 2)
);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new Sum(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 2)
);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
final Function shouldMatch = new Sum(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 2)
);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
@Test
public void repeatedSubFunction() {
final Pattern pattern = new SumPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("x")
);
@Test
public void repeatedSubFunction() {
final Pattern pattern = new SumPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("x")
);
final Function shouldMatch = new Sum(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 1)
);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
final Function shouldMatch = new Sum(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 1)
);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
final Function shouldNotMatch = new Sum(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 2)
);
assertFalse(pattern.match(shouldNotMatch).isPresent());
}
final Function shouldNotMatch = new Sum(
mathContext,
new Number(mathContext, 1),
new Number(mathContext, 2)
);
assertFalse(pattern.match(shouldNotMatch).isPresent());
}
@Test
public void numberPattern() {
final Pattern pattern = new NumberPattern(BigDecimal.valueOf(Math.PI));
@Test
public void numberPattern() {
final Pattern pattern = new NumberPattern(BigDecimal.valueOf(Math.PI));
final Function shouldNotMatch = new Number(mathContext, 2);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldNotMatch = new Number(mathContext, 2);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new Number(mathContext, Math.PI);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
final Function shouldMatch = new Number(mathContext, Math.PI);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
@Test
public void negativePattern() {
final Pattern pattern = new NegativePattern(
new SubFunctionPattern("x")
);
@Test
public void negativePattern() {
final Pattern pattern = new NegativePattern(
new SubFunctionPattern("x")
);
final Function shouldNotMatch = new Number(mathContext, 1);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldNotMatch = new Number(mathContext, 1);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new Negative(
mathContext,
new Number(mathContext, 2)
);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
final Function shouldMatch = new Negative(
mathContext,
new Number(mathContext, 2)
);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
@Test
public void undefinedPattern() {
final Pattern pattern = new UndefinedPattern();
@Test
public void undefinedPattern() {
final Pattern pattern = new UndefinedPattern();
final Function shouldNotMatch = new Number(mathContext, 0);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldNotMatch = new Number(mathContext, 0);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new Undefined(mathContext);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertTrue(pattern.replace(mathContext, subFunctions.get()) instanceof Undefined);
}
final Function shouldMatch = new Undefined(mathContext);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertTrue(pattern.replace(mathContext, subFunctions.get()) instanceof Undefined);
}
@Test
public void equationsSystemPattern() {
final Pattern pattern = new EquationsSystemPattern(new Pattern[]{
new SubFunctionPattern("x"),
new SubFunctionPattern("y"),
new SubFunctionPattern("z")
});
@Test
public void equationsSystemPattern() {
final Pattern pattern = new EquationsSystemPattern(new Pattern[]{
new SubFunctionPattern("x"),
new SubFunctionPattern("y"),
new SubFunctionPattern("z")
});
final Function shouldNotMatch = new EquationsSystem(
mathContext,
new Function[]{
new Number(mathContext, 1),
new Number(mathContext, 2),
}
);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldNotMatch = new EquationsSystem(
mathContext,
new Function[]{
new Number(mathContext, 1),
new Number(mathContext, 2),
}
);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new EquationsSystem(
mathContext,
new Function[]{
new Number(mathContext, 1),
new Number(mathContext, 2),
new Number(mathContext, 3)
}
);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
final Function shouldMatch = new EquationsSystem(
mathContext,
new Function[]{
new Number(mathContext, 1),
new Number(mathContext, 2),
new Number(mathContext, 3)
}
);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
@Test
public void rootPatternForRootSquare() {
final Pattern pattern = new RootPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("y")
);
@Test
public void rootPatternForRootSquare() {
final Pattern pattern = new RootPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("y")
);
final Function root = new Root(
mathContext,
new Number(mathContext, 2),
new Number(mathContext, 1)
);
final Optional<Map<String, Function>> rootSubFunctions = pattern.match(root);
assertTrue(rootSubFunctions.isPresent());
final Function root = new Root(
mathContext,
new Number(mathContext, 2),
new Number(mathContext, 1)
);
final Optional<Map<String, Function>> rootSubFunctions = pattern.match(root);
assertTrue(rootSubFunctions.isPresent());
final Function rootSquare = new RootSquare(
mathContext,
new Number(mathContext, 1)
);
final Optional<Map<String, Function>> rootSquareSubFunctions = pattern.match(rootSquare);
assertTrue(rootSquareSubFunctions.isPresent());
assertEquals(rootSubFunctions.get(), rootSquareSubFunctions.get());
final Function rootSquare = new RootSquare(
mathContext,
new Number(mathContext, 1)
);
final Optional<Map<String, Function>> rootSquareSubFunctions = pattern.match(rootSquare);
assertTrue(rootSquareSubFunctions.isPresent());
assertEquals(rootSubFunctions.get(), rootSquareSubFunctions.get());
final Function replacement = pattern.replace(mathContext, rootSubFunctions.get());
assertTrue(replacement instanceof RootSquare);
assertEquals(rootSquare, replacement);
}
final Function replacement = pattern.replace(mathContext, rootSubFunctions.get());
assertTrue(replacement instanceof RootSquare);
assertEquals(rootSquare, replacement);
}
@Test
public void otherBinaryPatterns() {
final Number one = new Number(mathContext, 1);
final Number two = new Number(mathContext, 2);
final SubFunctionPattern x = new SubFunctionPattern("x");
final SubFunctionPattern y = new SubFunctionPattern("y");
@Test
public void otherBinaryPatterns() {
final Number one = new Number(mathContext, 1);
final Number two = new Number(mathContext, 2);
final SubFunctionPattern x = new SubFunctionPattern("x");
final SubFunctionPattern y = new SubFunctionPattern("y");
final Function shouldNotMatch = new Sum(mathContext, one, two);
final Function shouldNotMatch = new Sum(mathContext, one, two);
final List<ImmutablePair<Pattern, Function>> patternsAndMatchingFunctions = Arrays.asList(
new ImmutablePair<>(
new DivisionPattern(x, y),
new Division(mathContext, one, two)
),
new ImmutablePair<>(
new EquationPattern(x, y),
new Equation(mathContext, one, two)
),
new ImmutablePair<>(
new LogarithmPattern(x, y),
new Logarithm(mathContext, one, two)
),
new ImmutablePair<>(
new MultiplicationPattern(x, y),
new Multiplication(mathContext, one, two)
),
new ImmutablePair<>(
new PowerPattern(x, y),
new Power(mathContext, one, two)
),
new ImmutablePair<>(
new RootPattern(x, y),
new Root(mathContext, one, two)
),
new ImmutablePair<>(
new SubtractionPattern(x, y),
new Subtraction(mathContext, one, two)
),
new ImmutablePair<>(
new SumSubtractionPattern(x, y),
new SumSubtraction(mathContext, one, two)
)
);
final List<ImmutablePair<Pattern, Function>> patternsAndMatchingFunctions = Arrays.asList(
new ImmutablePair<>(
new DivisionPattern(x, y),
new Division(mathContext, one, two)
),
new ImmutablePair<>(
new EquationPattern(x, y),
new Equation(mathContext, one, two)
),
new ImmutablePair<>(
new LogarithmPattern(x, y),
new Logarithm(mathContext, one, two)
),
new ImmutablePair<>(
new MultiplicationPattern(x, y),
new Multiplication(mathContext, one, two)
),
new ImmutablePair<>(
new PowerPattern(x, y),
new Power(mathContext, one, two)
),
new ImmutablePair<>(
new RootPattern(x, y),
new Root(mathContext, one, two)
),
new ImmutablePair<>(
new SubtractionPattern(x, y),
new Subtraction(mathContext, one, two)
),
new ImmutablePair<>(
new SumSubtractionPattern(x, y),
new SumSubtraction(mathContext, one, two)
)
);
testMultiplePatterns(shouldNotMatch, patternsAndMatchingFunctions);
}
testMultiplePatterns(shouldNotMatch, patternsAndMatchingFunctions);
}
@Test
public void otherUnaryPatterns() {
final Number one = new Number(mathContext, 1);
final SubFunctionPattern x = new SubFunctionPattern("x");
@Test
public void otherUnaryPatterns() {
final Number one = new Number(mathContext, 1);
final SubFunctionPattern x = new SubFunctionPattern("x");
final Function shouldNotMatch = new Negative(mathContext, one);
final Function shouldNotMatch = new Negative(mathContext, one);
final List<ImmutablePair<Pattern, Function>> patternsAndMatchingFunctions = Arrays.asList(
new ImmutablePair<>(
new ArcCosinePattern(x),
new ArcCosine(mathContext, one)
),
new ImmutablePair<>(
new ArcSinePattern(x),
new ArcSine(mathContext, one)
),
new ImmutablePair<>(
new ArcTangentPattern(x),
new ArcTangent(mathContext, one)
),
new ImmutablePair<>(
new CosinePattern(x),
new Cosine(mathContext, one)
),
new ImmutablePair<>(
new SinePattern(x),
new Sine(mathContext, one)
),
new ImmutablePair<>(
new TangentPattern(x),
new Tangent(mathContext, one)
)
);
final List<ImmutablePair<Pattern, Function>> patternsAndMatchingFunctions = Arrays.asList(
new ImmutablePair<>(
new ArcCosinePattern(x),
new ArcCosine(mathContext, one)
),
new ImmutablePair<>(
new ArcSinePattern(x),
new ArcSine(mathContext, one)
),
new ImmutablePair<>(
new ArcTangentPattern(x),
new ArcTangent(mathContext, one)
),
new ImmutablePair<>(
new CosinePattern(x),
new Cosine(mathContext, one)
),
new ImmutablePair<>(
new SinePattern(x),
new Sine(mathContext, one)
),
new ImmutablePair<>(
new TangentPattern(x),
new Tangent(mathContext, one)
)
);
testMultiplePatterns(shouldNotMatch, patternsAndMatchingFunctions);
}
testMultiplePatterns(shouldNotMatch, patternsAndMatchingFunctions);
}
private void testMultiplePatterns(
final Function shouldNotMatch,
final List<ImmutablePair<Pattern, Function>> patternsAndMatchingFunctions
) {
for (final ImmutablePair<Pattern, Function> patternAndMatchingFunction : patternsAndMatchingFunctions) {
final Pattern pattern = patternAndMatchingFunction.getLeft();
final Function shouldMatch = patternAndMatchingFunction.getRight();
private void testMultiplePatterns(
final Function shouldNotMatch,
final List<ImmutablePair<Pattern, Function>> patternsAndMatchingFunctions
) {
for (final ImmutablePair<Pattern, Function> patternAndMatchingFunction : patternsAndMatchingFunctions) {
final Pattern pattern = patternAndMatchingFunction.getLeft();
final Function shouldMatch = patternAndMatchingFunction.getRight();
assertFalse(pattern.match(shouldNotMatch).isPresent());
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
}
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
}
}