With J14Switches, you'll be able to use a simili Java 14 for your switches.
public class Main {
public static void main(String[] args) {
String s = "Hi !";
System.out.println(switch (s) {
case "Hi !" -> "This is a praint !";
case "Not Hi !" -> "This is another one";
default -> "This is the default one !";
});
}
}
import org.reddev.j14switches.Case;
import org.reddev.j14switches.Switch;
public class Main {
public static void main(String[] args) {
String s = "Hi !";
System.out.println(Switch.apply(s,
Case.key("Hi !", () -> "This is a print !"),
Case.key("Not Hi !", () -> "This is another one")
).orElse("This is the default one !"));
}
}
<repositories>
...
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
...
<dependency>
<groupId>com.github.RedsTom</groupId>
<artifactId>J14Switches</artifactId>
<version>{VERSION}</version>
</dependency>
</dependencies>
repositories {
...
maven { url 'https://jitpack.io' }
}
dependencies {
...
implementation 'com.github.RedsTom:J14Switches:{VERSION}'
}
You can also use predicates as conditions in your Cases
import org.reddev.j14switches.Case;
import org.reddev.j14switches.Switch;
public class Main {
public static void main(String[] args) {
String s = "Hi !";
System.out.println(Switch.apply(s,
new Case<>((u) -> u.contains("Hi"), () -> "This is a print !"),
new Case<>((u) -> u.equalsIgnoreCase("Not !"), () -> "This is another one")
).orElse("This is the default one !"));
}
}
import org.reddev.j14switches.{ Case, Switch };
object Main extends App {
var s = "Hi !"
print(Switch(s,
Case.key("Hi !", () => "This is a print !"),
Case.key("Not Hi !", () => "This is another one")
).orElse("Default"))
}