BAEL-2303 Updated the code as per the review comments.

This commit is contained in:
Sandip Singh 2018-11-06 14:21:19 +05:30
parent e1b47c72a0
commit 3c558d6427
9 changed files with 70 additions and 30 deletions

View File

@ -1,19 +1,17 @@
package com.baeldung.reducingIfElse;
public class AddCommand implements Command<Integer, Integer, Integer> {
public class AddCommand implements Command {
private int a;
private int b;
public AddCommand(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public Integer execute() {
return a + b;
}
@Override
public Command<Integer, Integer, Integer> takeInput(Integer a, Integer b) {
this.a = a;
this.b = b;
return this;
}
}

View File

@ -15,7 +15,7 @@ public class AddRule implements Rule {
}
@Override
public int getResult() {
return result;
public Result getResult() {
return new Result(result);
}
}

View File

@ -77,8 +77,7 @@ public class Calculator {
return targetOperation.apply(a, b);
}
public int calculate(int a, int b, Command<Integer, Integer, Integer> command) {
return command.takeInput(a, b)
.execute();
public int calculate(Command command) {
return command.execute();
}
}

View File

@ -1,7 +1,5 @@
package com.baeldung.reducingIfElse;
public interface Command<A, B, R> {
R execute();
Command<A, B, R> takeInput(A a, B b);
}
public interface Command {
Integer execute();
}

View File

@ -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;
}
}

View File

@ -4,5 +4,5 @@ public interface Rule {
boolean evaluate(Expression expression);
int getResult();
Result getResult();
}

View File

@ -2,6 +2,7 @@ package com.baeldung.reducingIfElse;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class RuleEngine {
@ -12,9 +13,12 @@ public class RuleEngine {
rules.add(new AddRule());
}
public List<Rule> process(Expression expression) {
return rules.stream()
public Result process(Expression expression) {
Rule rule = rules.stream()
.filter(r -> r.evaluate(expression))
.collect(Collectors.toList());
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Expression does not matches any Rule"));
return rule.getResult();
}
}

View File

@ -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);
}
}

View File

@ -2,12 +2,10 @@ package com.baeldung.reduceIfelse;
import com.baeldung.reducingIfElse.Expression;
import com.baeldung.reducingIfElse.Operator;
import com.baeldung.reducingIfElse.Rule;
import com.baeldung.reducingIfElse.Result;
import com.baeldung.reducingIfElse.RuleEngine;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -17,11 +15,9 @@ public class RuleEngineUnitTest {
public void whenNumbersGivenToRuleEngine_thenReturnCorrectResult() {
Expression expression = new Expression(5, 5, Operator.ADD);
RuleEngine engine = new RuleEngine();
List<Rule> rules = engine.process(expression);
Result result = engine.process(expression);
assertNotNull(rules);
assertEquals(1, rules.size());
assertEquals(10, rules.get(0)
.getResult());
assertNotNull(result);
assertEquals(10, result.getValue());
}
}