From 3c558d642798f3f977f7466463649bbfdc65b8a6 Mon Sep 17 00:00:00 2001 From: Sandip Singh Date: Tue, 6 Nov 2018 14:21:19 +0530 Subject: [PATCH] BAEL-2303 Updated the code as per the review comments. --- .../baeldung/reducingIfElse/AddCommand.java | 14 ++++---- .../com/baeldung/reducingIfElse/AddRule.java | 4 +-- .../baeldung/reducingIfElse/Calculator.java | 5 ++- .../com/baeldung/reducingIfElse/Command.java | 8 ++--- .../com/baeldung/reducingIfElse/Result.java | 13 ++++++++ .../com/baeldung/reducingIfElse/Rule.java | 2 +- .../baeldung/reducingIfElse/RuleEngine.java | 10 ++++-- .../reduceIfelse/CalculatorUnitTest.java | 32 +++++++++++++++++++ .../reduceIfelse/RuleEngineUnitTest.java | 12 +++---- 9 files changed, 70 insertions(+), 30 deletions(-) create mode 100644 core-java-8/src/main/java/com/baeldung/reducingIfElse/Result.java create mode 100644 core-java-8/src/test/java/com/baeldung/reduceIfelse/CalculatorUnitTest.java diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddCommand.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddCommand.java index 5aa0de7adc..279a3b2c55 100644 --- a/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddCommand.java +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddCommand.java @@ -1,19 +1,17 @@ package com.baeldung.reducingIfElse; -public class AddCommand implements Command { +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 takeInput(Integer a, Integer b) { - this.a = a; - this.b = b; - return this; - } } diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddRule.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddRule.java index 871ff1f2d1..f24c973ead 100644 --- a/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddRule.java +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/AddRule.java @@ -15,7 +15,7 @@ public class AddRule implements Rule { } @Override - public int getResult() { - return result; + public Result getResult() { + return new Result(result); } } diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Calculator.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Calculator.java index 9b8cce130f..550d92e183 100644 --- a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Calculator.java +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Calculator.java @@ -77,8 +77,7 @@ public class Calculator { return targetOperation.apply(a, b); } - public int calculate(int a, int b, Command command) { - return command.takeInput(a, b) - .execute(); + public int calculate(Command command) { + return command.execute(); } } diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Command.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Command.java index d9f00e31b4..c084fcc6a0 100644 --- a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Command.java +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Command.java @@ -1,7 +1,5 @@ package com.baeldung.reducingIfElse; -public interface Command { - R execute(); - - Command takeInput(A a, B b); -} +public interface Command { + Integer execute(); +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Result.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Result.java new file mode 100644 index 0000000000..d5ed12202e --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Result.java @@ -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; + } +} diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Rule.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Rule.java index 202072dd66..5a6c84b0f9 100644 --- a/core-java-8/src/main/java/com/baeldung/reducingIfElse/Rule.java +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/Rule.java @@ -4,5 +4,5 @@ public interface Rule { boolean evaluate(Expression expression); - int getResult(); + Result getResult(); } diff --git a/core-java-8/src/main/java/com/baeldung/reducingIfElse/RuleEngine.java b/core-java-8/src/main/java/com/baeldung/reducingIfElse/RuleEngine.java index 3af67aff11..ac56915dee 100644 --- a/core-java-8/src/main/java/com/baeldung/reducingIfElse/RuleEngine.java +++ b/core-java-8/src/main/java/com/baeldung/reducingIfElse/RuleEngine.java @@ -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 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(); } } diff --git a/core-java-8/src/test/java/com/baeldung/reduceIfelse/CalculatorUnitTest.java b/core-java-8/src/test/java/com/baeldung/reduceIfelse/CalculatorUnitTest.java new file mode 100644 index 0000000000..fa351930d8 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/reduceIfelse/CalculatorUnitTest.java @@ -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); + } +} diff --git a/core-java-8/src/test/java/com/baeldung/reduceIfelse/RuleEngineUnitTest.java b/core-java-8/src/test/java/com/baeldung/reduceIfelse/RuleEngineUnitTest.java index 227dd12f0d..4a30b3efac 100644 --- a/core-java-8/src/test/java/com/baeldung/reduceIfelse/RuleEngineUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/reduceIfelse/RuleEngineUnitTest.java @@ -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 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()); } }