To provide flexibility for testing cases with objects, I drafted this. Right off the bat, however, it is TOO FLEXIBLE. This meaning that any boolean applied in a case can invoke an action. Although this could be dangerous, it is primarily not the purpose and can be changed later. This is simple just to show this can be done, and rather quite simply.
package main.java.Switch;
public class SwitchTest {
public static void main(String[] args) {
// Create a new student object (our test object)
Student cookie = new Student("Cookie", 12);
// Create a new switch
Switch s = new Switch(cookie);
/* invoke onCase from the switch, and we can keep invoking it, providing a type of
switch.case().case().case() format. */
// Using lambda expressions on a functional interface, we can test a boolean (param1)
// and invoke an action applied to the interface (param2)
s.onCase(cookie.getGrade() == 10, () -> System.out.println("Case 1"))
.onCase(cookie.getGrade() == 12, () -> System.out.println("Case 2"))
.onCase(cookie.getName().equalsIgnoreCase("cookie"), () -> System.out.println("Case 3"));
}
}
After executing this, as expected, the output is:
Case 2
Case 3
Process finished with exit code 0