Skip to content

Commit 3c558d6

Browse files
committed
BAEL-2303 Updated the code as per the review comments.
1 parent e1b47c7 commit 3c558d6

File tree

9 files changed

+70
-30
lines changed

9 files changed

+70
-30
lines changed
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.baeldung.reducingIfElse;
22

3-
public class AddCommand implements Command<Integer, Integer, Integer> {
3+
public class AddCommand implements Command {
44

55
private int a;
66
private int b;
77

8-
@Override
9-
public Integer execute() {
10-
return a + b;
8+
public AddCommand(int a, int b) {
9+
this.a = a;
10+
this.b = b;
1111
}
1212

1313
@Override
14-
public Command<Integer, Integer, Integer> takeInput(Integer a, Integer b) {
15-
this.a = a;
16-
this.b = b;
17-
return this;
14+
public Integer execute() {
15+
return a + b;
1816
}
1917
}

core-java-8/src/main/java/com/baeldung/reducingIfElse/AddRule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public boolean evaluate(Expression expression) {
1515
}
1616

1717
@Override
18-
public int getResult() {
19-
return result;
18+
public Result getResult() {
19+
return new Result(result);
2020
}
2121
}

core-java-8/src/main/java/com/baeldung/reducingIfElse/Calculator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public int calculateUsingFactory(int a, int b, String operation) {
7777
return targetOperation.apply(a, b);
7878
}
7979

80-
public int calculate(int a, int b, Command<Integer, Integer, Integer> command) {
81-
return command.takeInput(a, b)
82-
.execute();
80+
public int calculate(Command command) {
81+
return command.execute();
8382
}
8483
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.baeldung.reducingIfElse;
22

3-
public interface Command<A, B, R> {
4-
R execute();
5-
6-
Command<A, B, R> takeInput(A a, B b);
7-
}
3+
public interface Command {
4+
Integer execute();
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.reducingIfElse;
2+
3+
public class Result {
4+
int value;
5+
6+
public Result(int value) {
7+
this.value = value;
8+
}
9+
10+
public int getValue() {
11+
return value;
12+
}
13+
}

core-java-8/src/main/java/com/baeldung/reducingIfElse/Rule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ public interface Rule {
44

55
boolean evaluate(Expression expression);
66

7-
int getResult();
7+
Result getResult();
88
}

core-java-8/src/main/java/com/baeldung/reducingIfElse/RuleEngine.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.Optional;
56
import java.util.stream.Collectors;
67

78
public class RuleEngine {
@@ -12,9 +13,12 @@ public class RuleEngine {
1213
rules.add(new AddRule());
1314
}
1415

15-
public List<Rule> process(Expression expression) {
16-
return rules.stream()
16+
public Result process(Expression expression) {
17+
18+
Rule rule = rules.stream()
1719
.filter(r -> r.evaluate(expression))
18-
.collect(Collectors.toList());
20+
.findFirst()
21+
.orElseThrow(() -> new IllegalArgumentException("Expression does not matches any Rule"));
22+
return rule.getResult();
1923
}
2024
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.reduceIfelse;
2+
3+
import com.baeldung.reducingIfElse.AddCommand;
4+
import com.baeldung.reducingIfElse.Calculator;
5+
import com.baeldung.reducingIfElse.Operator;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class CalculatorUnitTest {
11+
12+
@Test
13+
public void whenCalculateUsingStringOperator_thenReturnCorrectResult() {
14+
Calculator calculator = new Calculator();
15+
int result = calculator.calculate(3, 4, "add");
16+
assertEquals(7, result);
17+
}
18+
19+
@Test
20+
public void whenCalculateUsingEnumOperator_thenReturnCorrectResult() {
21+
Calculator calculator = new Calculator();
22+
int result = calculator.calculate(3, 4, Operator.valueOf("ADD"));
23+
assertEquals(7, result);
24+
}
25+
26+
@Test
27+
public void whenCalculateUsingCommand_thenReturnCorrectResult() {
28+
Calculator calculator = new Calculator();
29+
int result = calculator.calculate(new AddCommand(3, 7));
30+
assertEquals(10, result);
31+
}
32+
}

core-java-8/src/test/java/com/baeldung/reduceIfelse/RuleEngineUnitTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import com.baeldung.reducingIfElse.Expression;
44
import com.baeldung.reducingIfElse.Operator;
5-
import com.baeldung.reducingIfElse.Rule;
5+
import com.baeldung.reducingIfElse.Result;
66
import com.baeldung.reducingIfElse.RuleEngine;
77
import org.junit.Test;
88

9-
import java.util.List;
10-
119
import static org.junit.Assert.assertEquals;
1210
import static org.junit.Assert.assertNotNull;
1311

@@ -17,11 +15,9 @@ public class RuleEngineUnitTest {
1715
public void whenNumbersGivenToRuleEngine_thenReturnCorrectResult() {
1816
Expression expression = new Expression(5, 5, Operator.ADD);
1917
RuleEngine engine = new RuleEngine();
20-
List<Rule> rules = engine.process(expression);
18+
Result result = engine.process(expression);
2119

22-
assertNotNull(rules);
23-
assertEquals(1, rules.size());
24-
assertEquals(10, rules.get(0)
25-
.getResult());
20+
assertNotNull(result);
21+
assertEquals(10, result.getValue());
2622
}
2723
}

0 commit comments

Comments
 (0)