Your list of if-then-else statements reminds me of a rule-based system. The basis of the rule is essentially a database containing the rules of the form Condition -> Action . An implementation of a condition in Java 8 might look like this:
@FunctionalInterface interface Condition { boolean matches(A a, B b, C c, D d); }
For Action you can just use Runnable . Based on your code, a rulebase definition can be defined as follows:
static final Map<Condition, Runnable> BASE = ImmutableMap.<Condition, Runnable>builder() .put((a, b, c, d) -> a == A1, () -> doSomethingA1()) .put((a, b, c, d) -> a == A1 && b == B1, () -> doSomethingB1()) .put((a, b, c, d) -> a == A1 && b == B1 && c == C2, () -> doSomethingC2()) .put((a, b, c, d) -> a == A1 && b == B2, () -> doSomethingB2()) .put((a, b, c, d) -> c == C2, () -> doSomethingC2()) .put((a, b, c, d) -> c == C2 && d == D1, () -> doSomethingD1()) .put((a, b, c, d) -> c == C2 && d == D3, () -> doSomethingD3()) .put((a, b, c, d) -> a == A2, () -> doSomethingA2()) .put((a, b, c, d) -> a == A3, () -> doSomethingA3()) .build();
To use this rule base, you can simply define the following method:
static void evaluate(A a, B b, C c, D d) { BASE.entrySet().stream() .filter((entry) -> entry.getKey().matches(a, b, c, d)) .forEach((e) -> e.getValue().run()); }
Lets name it as follows:
evaluate(A1, B1, null, null); // executes doSomethingA1() and doSomethingB1()