Skip to content

Instantly share code, notes, and snippets.

@bfu4
Created December 18, 2020 05:23
Show Gist options
  • Save bfu4/c0876bada0347731e39cb5838f920765 to your computer and use it in GitHub Desktop.
Save bfu4/c0876bada0347731e39cb5838f920765 to your computer and use it in GitHub Desktop.
[JAVA] Switch statement using objects

a custom switch statement

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
package main.java.Switch;
/**
* Functional interface which we use to create a lambda and invoke doSomething();
*
* @author bfu4
*/
@FunctionalInterface
public interface Case {
void doSomething();
}
package main.java.Switch;
/**
* Test object
*
* @author bfu4
*/
public class Student {
private String name;
private int grade;
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
public int getGrade() { return grade; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public void setGrade(int grade) { this.grade = grade; }
public void speak(String msg) {
System.out.println("[" + name.toUpperCase() + "] " + msg);
}
@Override
public String toString() {
return "Student{name: \"" + name + "\", grade: \"" + grade + "\"}";
}
}
package main.java.Switch;
/**
* Switch object class to act like a switch statement using booleans and lambdas
*
* @author bfu4
*/
public class Switch {
private final Object parent;
/**
* Create a new switch statement
*
* @param toSwitch the object in which we will switch
*/
public Switch(Object toSwitch) {
this.parent = toSwitch;
}
/**
* Get the parent
*
* @return parent object for the switch object
*/
public Object getParent() { return parent; }
/**
* We can use a "Case" object that we can act upon by using it as a functional interface.
* When we push the lambda in as the case parameter, it will be invoked when we call
* c.doSomething();
*
* @param toCheck boolean that we will check to be true (case)
* @param c the action in which we will invoke (case)
*/
public Switch onCase(boolean toCheck, Case c) {
if (toCheck) {
c.doSomething();
}
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment