Use actual MathContext instead of null in Pattern replace
This commit is contained in:
parent
0afacf7ddc
commit
21de6349f4
@ -1,6 +1,7 @@
|
||||
package it.cavallium.warppi.math.rules.dsl;
|
||||
|
||||
import it.cavallium.warppi.math.Function;
|
||||
import it.cavallium.warppi.math.MathContext;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@ -21,8 +22,10 @@ public interface Pattern {
|
||||
/**
|
||||
* 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(Map<String, Function> subFunctions);
|
||||
Function replace(MathContext mathContext, Map<String, Function> subFunctions);
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package it.cavallium.warppi.math.rules.dsl.patterns;
|
||||
|
||||
import it.cavallium.warppi.math.Function;
|
||||
import it.cavallium.warppi.math.MathContext;
|
||||
import it.cavallium.warppi.math.functions.Negative;
|
||||
import it.cavallium.warppi.math.rules.dsl.Pattern;
|
||||
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
|
||||
@ -24,10 +25,10 @@ public class NegativePattern extends VisitorPattern {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(Map<String, Function> subFunctions) {
|
||||
public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
|
||||
return new Negative(
|
||||
null,
|
||||
inner.replace(subFunctions)
|
||||
mathContext,
|
||||
inner.replace(mathContext, subFunctions)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package it.cavallium.warppi.math.rules.dsl.patterns;
|
||||
|
||||
import it.cavallium.warppi.math.Function;
|
||||
import it.cavallium.warppi.math.MathContext;
|
||||
import it.cavallium.warppi.math.functions.Number;
|
||||
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
|
||||
|
||||
@ -29,7 +30,7 @@ public class NumberPattern extends VisitorPattern {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(Map<String, Function> subFunctions) {
|
||||
return new Number(null, value);
|
||||
public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
|
||||
return new Number(mathContext, value);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package it.cavallium.warppi.math.rules.dsl.patterns;
|
||||
|
||||
import it.cavallium.warppi.math.Function;
|
||||
import it.cavallium.warppi.math.MathContext;
|
||||
import it.cavallium.warppi.math.rules.dsl.Pattern;
|
||||
|
||||
import java.util.HashMap;
|
||||
@ -25,7 +26,7 @@ public class SubFunctionPattern implements Pattern {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(Map<String, Function> subFunctions) {
|
||||
public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
|
||||
return subFunctions.get(name);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package it.cavallium.warppi.math.rules.dsl.patterns;
|
||||
|
||||
import it.cavallium.warppi.math.Function;
|
||||
import it.cavallium.warppi.math.MathContext;
|
||||
import it.cavallium.warppi.math.functions.Sum;
|
||||
import it.cavallium.warppi.math.rules.dsl.Pattern;
|
||||
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
|
||||
@ -29,11 +30,11 @@ public class SumPattern extends VisitorPattern {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(Map<String, Function> subFunctions) {
|
||||
public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
|
||||
return new Sum(
|
||||
null,
|
||||
left.replace(subFunctions),
|
||||
right.replace(subFunctions)
|
||||
mathContext,
|
||||
left.replace(mathContext, subFunctions),
|
||||
right.replace(mathContext, subFunctions)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package it.cavallium.warppi.math.rules.dsl;
|
||||
|
||||
import it.cavallium.warppi.math.Function;
|
||||
import it.cavallium.warppi.math.MathContext;
|
||||
import it.cavallium.warppi.math.functions.Negative;
|
||||
import it.cavallium.warppi.math.functions.Number;
|
||||
import it.cavallium.warppi.math.functions.Subtraction;
|
||||
@ -18,20 +19,22 @@ import java.util.Optional;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PatternTest {
|
||||
private final MathContext mathContext = new MathContext();
|
||||
|
||||
@Test
|
||||
public void subFunctionPattern() {
|
||||
final Pattern pattern = new SubFunctionPattern("x");
|
||||
|
||||
final Function func = new Sum(
|
||||
null,
|
||||
new Number(null, 1),
|
||||
new Number(null, 2)
|
||||
mathContext,
|
||||
new Number(mathContext, 1),
|
||||
new Number(mathContext, 2)
|
||||
);
|
||||
|
||||
final Optional<Map<String, Function>> subFunctions = pattern.match(func);
|
||||
assertTrue(subFunctions.isPresent());
|
||||
|
||||
assertEquals(func, pattern.replace(subFunctions.get()));
|
||||
assertEquals(func, pattern.replace(mathContext, subFunctions.get()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -42,20 +45,20 @@ public class PatternTest {
|
||||
);
|
||||
|
||||
final Function shouldNotMatch = new Subtraction(
|
||||
null,
|
||||
new Number(null, 1),
|
||||
new Number(null, 2)
|
||||
mathContext,
|
||||
new Number(mathContext, 1),
|
||||
new Number(mathContext, 2)
|
||||
);
|
||||
assertFalse(pattern.match(shouldNotMatch).isPresent());
|
||||
|
||||
final Function shouldMatch = new Sum(
|
||||
null,
|
||||
new Number(null, 1),
|
||||
new Number(null, 2)
|
||||
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(subFunctions.get()));
|
||||
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -66,18 +69,18 @@ public class PatternTest {
|
||||
);
|
||||
|
||||
final Function shouldMatch = new Sum(
|
||||
null,
|
||||
new Number(null, 1),
|
||||
new Number(null, 1)
|
||||
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(subFunctions.get()));
|
||||
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
|
||||
|
||||
final Function shouldNotMatch = new Sum(
|
||||
null,
|
||||
new Number(null, 1),
|
||||
new Number(null, 2)
|
||||
mathContext,
|
||||
new Number(mathContext, 1),
|
||||
new Number(mathContext, 2)
|
||||
);
|
||||
assertFalse(pattern.match(shouldNotMatch).isPresent());
|
||||
}
|
||||
@ -86,13 +89,13 @@ public class PatternTest {
|
||||
public void numberPattern() {
|
||||
final Pattern pattern = new NumberPattern(BigDecimal.valueOf(Math.PI));
|
||||
|
||||
final Function shouldNotMatch = new Number(null, 2);
|
||||
final Function shouldNotMatch = new Number(mathContext, 2);
|
||||
assertFalse(pattern.match(shouldNotMatch).isPresent());
|
||||
|
||||
final Function shouldMatch = new Number(null, Math.PI);
|
||||
final Function shouldMatch = new Number(mathContext, Math.PI);
|
||||
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
|
||||
assertTrue(subFunctions.isPresent());
|
||||
assertEquals(shouldMatch, pattern.replace(subFunctions.get()));
|
||||
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -101,15 +104,15 @@ public class PatternTest {
|
||||
new SubFunctionPattern("x")
|
||||
);
|
||||
|
||||
final Function shouldNotMatch = new Number(null, 1);
|
||||
final Function shouldNotMatch = new Number(mathContext, 1);
|
||||
assertFalse(pattern.match(shouldNotMatch).isPresent());
|
||||
|
||||
final Function shouldMatch = new Negative(
|
||||
null,
|
||||
new Number(null, 2)
|
||||
mathContext,
|
||||
new Number(mathContext, 2)
|
||||
);
|
||||
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
|
||||
assertTrue(subFunctions.isPresent());
|
||||
assertEquals(shouldMatch, pattern.replace(subFunctions.get()));
|
||||
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user