Merge pull request #5603 from sandy03934/master

BAEL-2303 Added code to demonstrate how to replace multiple if statements in Java
This commit is contained in:
Eric Martin 2018-11-07 21:21:06 -06:00 committed by GitHub
commit 91ec114e9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 355 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.reducingIfElse;
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;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.reducingIfElse;
public class AddRule implements Rule {
private int result;
@Override
public boolean evaluate(Expression expression) {
boolean evalResult = false;
if (expression.getOperator() == Operator.ADD) {
this.result = expression.getX() + expression.getY();
evalResult = true;
}
return evalResult;
}
@Override
public Result getResult() {
return new Result(result);
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.reducingIfElse;
public class Addition implements Operation {
@Override
public int apply(int a, int b) {
return a + b;
}
}

View File

@ -0,0 +1,83 @@
package com.baeldung.reducingIfElse;
public class Calculator {
public int calculate(int a, int b, String operator) {
int result = Integer.MIN_VALUE;
if ("add".equals(operator)) {
result = a + b;
} else if ("multiply".equals(operator)) {
result = a * b;
} else if ("divide".equals(operator)) {
result = a / b;
} else if ("subtract".equals(operator)) {
result = a - b;
} else if ("modulo".equals(operator)) {
result = a % b;
}
return result;
}
public int calculateUsingSwitch(int a, int b, String operator) {
int result = 0;
switch (operator) {
case "add":
result = a + b;
break;
case "multiply":
result = a * b;
break;
case "divide":
result = a / b;
break;
case "subtract":
result = a - b;
break;
case "modulo":
result = a % b;
break;
default:
result = Integer.MIN_VALUE;
}
return result;
}
public int calculateUsingSwitch(int a, int b, Operator operator) {
int result = 0;
switch (operator) {
case ADD:
result = a + b;
break;
case MULTIPLY:
result = a * b;
break;
case DIVIDE:
result = a / b;
break;
case SUBTRACT:
result = a - b;
break;
case MODULO:
result = a % b;
break;
default:
result = Integer.MIN_VALUE;
}
return result;
}
public int calculate(int a, int b, Operator operator) {
return operator.apply(a, b);
}
public int calculateUsingFactory(int a, int b, String operation) {
Operation targetOperation = OperatorFactory.getOperation(operation)
.orElseThrow(() -> new IllegalArgumentException("Invalid Operator"));
return targetOperation.apply(a, b);
}
public int calculate(Command command) {
return command.execute();
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.reducingIfElse;
public interface Command {
Integer execute();
}

View File

@ -0,0 +1,7 @@
package com.baeldung.reducingIfElse;
public class Division implements Operation {
@Override public int apply(int a, int b) {
return a / b;
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.reducingIfElse;
public class Expression {
private Integer x;
private Integer y;
private Operator operator;
public Expression(Integer x, Integer y, Operator operator) {
this.x = x;
this.y = y;
this.operator = operator;
}
public Integer getX() {
return x;
}
public Integer getY() {
return y;
}
public Operator getOperator() {
return operator;
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.reducingIfElse;
public class Modulo implements Operation {
@Override public int apply(int a, int b) {
return a % b;
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.reducingIfElse;
public class Multiplication implements Operation {
@Override public int apply(int a, int b) {
return 0;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.reducingIfElse;
public interface Operation {
int apply(int a, int b);
}

View File

@ -0,0 +1,41 @@
package com.baeldung.reducingIfElse;
public enum Operator {
ADD {
@Override
public int apply(int a, int b) {
return a + b;
}
},
MULTIPLY {
@Override
public int apply(int a, int b) {
return a * b;
}
},
SUBTRACT {
@Override
public int apply(int a, int b) {
return a - b;
}
},
DIVIDE {
@Override
public int apply(int a, int b) {
return a / b;
}
},
MODULO {
@Override
public int apply(int a, int b) {
return a % b;
}
};
public abstract int apply(int a, int b);
}

View File

@ -0,0 +1,21 @@
package com.baeldung.reducingIfElse;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class OperatorFactory {
static Map<String, Operation> operationMap = new HashMap<>();
static {
operationMap.put("add", new Addition());
operationMap.put("divide", new Division());
operationMap.put("multiply", new Multiplication());
operationMap.put("subtract", new Subtraction());
operationMap.put("modulo", new Modulo());
}
public static Optional<Operation> getOperation(String operation) {
return Optional.ofNullable(operationMap.get(operation));
}
}

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

@ -0,0 +1,8 @@
package com.baeldung.reducingIfElse;
public interface Rule {
boolean evaluate(Expression expression);
Result getResult();
}

View File

@ -0,0 +1,24 @@
package com.baeldung.reducingIfElse;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class RuleEngine {
private static List<Rule> rules = new ArrayList<>();
static {
rules.add(new AddRule());
}
public Result process(Expression expression) {
Rule rule = rules.stream()
.filter(r -> r.evaluate(expression))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Expression does not matches any Rule"));
return rule.getResult();
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.reducingIfElse;
public class Subtraction implements Operation {
@Override public int apply(int a, int b) {
return a - b;
}
}

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

@ -0,0 +1,23 @@
package com.baeldung.reduceIfelse;
import com.baeldung.reducingIfElse.Expression;
import com.baeldung.reducingIfElse.Operator;
import com.baeldung.reducingIfElse.Result;
import com.baeldung.reducingIfElse.RuleEngine;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class RuleEngineUnitTest {
@Test
public void whenNumbersGivenToRuleEngine_thenReturnCorrectResult() {
Expression expression = new Expression(5, 5, Operator.ADD);
RuleEngine engine = new RuleEngine();
Result result = engine.process(expression);
assertNotNull(result);
assertEquals(10, result.getValue());
}
}