. The final effect is as follows:
cIf(cond1, () -> { //The first if //do action 1 }).cElseIf(() -> cond2, () -> { //else if evaluating cond2 condition //do action 2 }).cElseIf(cond3, () -> { //else if for unconditional evaluation of cond3 //do action 3 }).cElse(() -> { //Last else //do action 4 });Realization
/** * Constructor of the whole statement block */ class IfBuilder { /* Hide construction method */ private IfBuilder() { } /** * @param predicate Assertion of unconditional evaluation * @param action Action to perform * @return Same as below cIf */ public static IfBuilder cIf(boolean predicate, Runnable action) { return cIf(() -> predicate, action); //Switch to the assertion of conditional evaluation and call the overloaded method cIf } /** * @param predicate Assertion of conditional evaluation * @param action Action to perform * @return If the assertion evaluates to true, it returns the builder that does nothing by default, otherwise it returns the builder with subsequent elseIf and else functions */ public static IfBuilder cIf(Supplier<Boolean> predicate, Runnable action) { return predicate.get() //Evaluate assertions ? ((Supplier<IfBuilder>) () -> { //The result of the assertion is true, the action is evaluated, and an empty builder without function is returned action.run(); return new IfBuilder(); }).get() : new IfBuilder() { //The result of the assertion is false, no action is evaluated, and a builder with subsequent elseIf and else functions is returned /* Flag variable indicating whether there is an assertion that returns true */ private boolean elseIfFlag = false; @Override public IfBuilder cElseIf(Supplier<Boolean> b, Runnable r) { /* If the assertion is true, evaluate r, then change elseIfFlag to true, and return an empty builder; otherwise, directly return itself */ return b.get() ? ((Supplier<IfBuilder>) () -> { r.run(); elseIfFlag = true; return new IfBuilder(); }).get() : this; } @Override public IfBuilder cElseIf(boolean b, Runnable r) { return cElseIf(() -> b, r); } @Override public void cElse(Runnable r) { /* If and only if elseIf is not satisfied before, evaluate r */ ((Supplier<IfBuilder>) () -> elseIfFlag ? this : ((Supplier<IfBuilder>) () -> { r.run(); return this; }).get()).get(); } }; } /* Default else if for evaluation of assertion condition, no action*/ public IfBuilder cElseIf(Supplier<Boolean> b, Runnable r) { return this; } /* Default else if for unconditional evaluation of assertions, no action*/ public IfBuilder cElseIf(boolean b, Runnable r) { return this; } /* Default else, no action*/ public void cElse(Runnable r) { } }Example
The sample code is as follows, where i and j are generated with random numbers each time
for (int x = 0; x < 10; x++) { int i = new Random().nextInt() % 4; int j = new Random().nextInt() % 4; cIf(i == 0 || j == 0, () -> { System.out.println(i + " " + j + " : i == 0 || j == 0"); }).cElseIf(() -> i < j, () -> { System.out.println(i + " " + j + " : i < j"); }).cElseIf(i > j, () -> { System.out.println(i + " " + j + " : i > j"); }).cElse(() -> { System.out.println(i + " " + j + " : i = j"); }); }
Sample output:
-3 -1 : i < j 3 -3 : i > j 2 -2 : i > j 0 -1 : i == 0 || j == 0 -1 -2 : i > j -1 -1 : i = j -1 -2 : i > j 0 0 : i == 0 || j == 0 2 1 : i > j 2 1 : i > jEnding
Write the code of play, finish writing yourself want to vomit slot. Looking at the use of some high-level writing methods, not only completely did not solve any problems, but also brought a lot of code complexity.