BAEL-2303 Updated the code as per the review comments.
This commit is contained in:
parent
e1b47c72a0
commit
3c558d6427
|
@ -1,19 +1,17 @@
|
||||||
package com.baeldung.reducingIfElse;
|
package com.baeldung.reducingIfElse;
|
||||||
|
|
||||||
public class AddCommand implements Command<Integer, Integer, Integer> {
|
public class AddCommand implements Command {
|
||||||
|
|
||||||
private int a;
|
private int a;
|
||||||
private int b;
|
private int b;
|
||||||
|
|
||||||
|
public AddCommand(int a, int b) {
|
||||||
|
this.a = a;
|
||||||
|
this.b = b;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer execute() {
|
public Integer execute() {
|
||||||
return a + b;
|
return a + b;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Command<Integer, Integer, Integer> takeInput(Integer a, Integer b) {
|
|
||||||
this.a = a;
|
|
||||||
this.b = b;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ public class AddRule implements Rule {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getResult() {
|
public Result getResult() {
|
||||||
return result;
|
return new Result(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,8 +77,7 @@ public class Calculator {
|
||||||
return targetOperation.apply(a, b);
|
return targetOperation.apply(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int calculate(int a, int b, Command<Integer, Integer, Integer> command) {
|
public int calculate(Command command) {
|
||||||
return command.takeInput(a, b)
|
return command.execute();
|
||||||
.execute();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package com.baeldung.reducingIfElse;
|
package com.baeldung.reducingIfElse;
|
||||||
|
|
||||||
public interface Command<A, B, R> {
|
public interface Command {
|
||||||
R execute();
|
Integer execute();
|
||||||
|
}
|
||||||
Command<A, B, R> takeInput(A a, B b);
|
|
||||||
}
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.reducingIfElse;
|
||||||
|
|
||||||
|
public class Result {
|
||||||
|
int value;
|
||||||
|
|
||||||
|
public Result(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,5 +4,5 @@ public interface Rule {
|
||||||
|
|
||||||
boolean evaluate(Expression expression);
|
boolean evaluate(Expression expression);
|
||||||
|
|
||||||
int getResult();
|
Result getResult();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.baeldung.reducingIfElse;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class RuleEngine {
|
public class RuleEngine {
|
||||||
|
@ -12,9 +13,12 @@ public class RuleEngine {
|
||||||
rules.add(new AddRule());
|
rules.add(new AddRule());
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Rule> process(Expression expression) {
|
public Result process(Expression expression) {
|
||||||
return rules.stream()
|
|
||||||
|
Rule rule = rules.stream()
|
||||||
.filter(r -> r.evaluate(expression))
|
.filter(r -> r.evaluate(expression))
|
||||||
.collect(Collectors.toList());
|
.findFirst()
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Expression does not matches any Rule"));
|
||||||
|
return rule.getResult();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.reduceIfelse;
|
||||||
|
|
||||||
|
import com.baeldung.reducingIfElse.AddCommand;
|
||||||
|
import com.baeldung.reducingIfElse.Calculator;
|
||||||
|
import com.baeldung.reducingIfElse.Operator;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class CalculatorUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalculateUsingStringOperator_thenReturnCorrectResult() {
|
||||||
|
Calculator calculator = new Calculator();
|
||||||
|
int result = calculator.calculate(3, 4, "add");
|
||||||
|
assertEquals(7, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalculateUsingEnumOperator_thenReturnCorrectResult() {
|
||||||
|
Calculator calculator = new Calculator();
|
||||||
|
int result = calculator.calculate(3, 4, Operator.valueOf("ADD"));
|
||||||
|
assertEquals(7, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalculateUsingCommand_thenReturnCorrectResult() {
|
||||||
|
Calculator calculator = new Calculator();
|
||||||
|
int result = calculator.calculate(new AddCommand(3, 7));
|
||||||
|
assertEquals(10, result);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,12 +2,10 @@ package com.baeldung.reduceIfelse;
|
||||||
|
|
||||||
import com.baeldung.reducingIfElse.Expression;
|
import com.baeldung.reducingIfElse.Expression;
|
||||||
import com.baeldung.reducingIfElse.Operator;
|
import com.baeldung.reducingIfElse.Operator;
|
||||||
import com.baeldung.reducingIfElse.Rule;
|
import com.baeldung.reducingIfElse.Result;
|
||||||
import com.baeldung.reducingIfElse.RuleEngine;
|
import com.baeldung.reducingIfElse.RuleEngine;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
@ -17,11 +15,9 @@ public class RuleEngineUnitTest {
|
||||||
public void whenNumbersGivenToRuleEngine_thenReturnCorrectResult() {
|
public void whenNumbersGivenToRuleEngine_thenReturnCorrectResult() {
|
||||||
Expression expression = new Expression(5, 5, Operator.ADD);
|
Expression expression = new Expression(5, 5, Operator.ADD);
|
||||||
RuleEngine engine = new RuleEngine();
|
RuleEngine engine = new RuleEngine();
|
||||||
List<Rule> rules = engine.process(expression);
|
Result result = engine.process(expression);
|
||||||
|
|
||||||
assertNotNull(rules);
|
assertNotNull(result);
|
||||||
assertEquals(1, rules.size());
|
assertEquals(10, result.getValue());
|
||||||
assertEquals(10, rules.get(0)
|
|
||||||
.getResult());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue