New Switch in JDK 12

Roman Iovlev Roman_Iovlev at epam.com
Thu Apr 4 10:43:52 UTC 2019


Hi guys,

It is good to see and update on the switch usage in new JDK 12.
As I get now you can

  1.  Assign result of switch
  2.  Use -> interface to avoid break
  3.  Use more than one option in case
In my library for Java I also implement new Switch based on Lambda functions (see code here https://github.com/jdi-testing/jdi-lightsaber) or in maven
        <dependency>
            <groupId>com.epam.jdi.tools</groupId>
            <artifactId>jdi-light-saber</artifactId>
            <version>2.0.48</version>
        </dependency>
And it has all mentioned functionalities and few additional:

  1.  Expressions in case
        return Switch(osName).get(
            Case(os -> os.contains("mac"), MAC),
            Case(os -> os.contains("win") || os.contains("ms"), WIN),
            Default(LINUX)
        );

If Switch return value it will return result for first condition that meet requirement

  1.  Multiple Case executions

If Switch doesn't return value it execute all result functions that meet Case condition

  1.  Switch without input parameter
        return Switch().get(
            Case(() -> state != null, "State"),
            Case(() -> os.equals("mac"), "Mac"),
            Default("Linux")
        );
This examples are for JDK 8
You can find this examples of using lightsaber in our testing library https://github.com/jdi-testing/jdi-light
In new switch this can look like:

Suggestion:
Access rights = switch (number) {
    case n -> n == 0 -> OK;
    case n-> n % 2 == 0 && n > 0 -> EVEN;
    case n-> n % 2 > 0 && n > 0 -> ODD;
    default -> null;
}
or
Access rights = switch (number) {
    case n == 0 -> OK;
    case n % 2 == 0 && n > 0 -> EVEN;
    case n % 2 > 0 && n > 0 -> ODD;
    default -> null;
}
or
Access rights = switch () {
    case number == 0 -> OK;
    case number % 2 == 0 && number >0 -> EVEN;
    case number % 2 > 0 && number >0 -> ODD;
    default -> null;
}
And same syntax for switch without result

It would be great to introduce this features in next versions of switch in following releases of JDK

P.S. I'm new to openjdk community, if you have any questions feel free to ask
P.P.S in lightsaber library you can find more interesting ideas:
1) MapArray - Map that implements Collection interface and can be used as list with integrated stream capabilities
2) DataClass - that allow to have good toString based on class fields, equal by fields and not by reference and ability to avoid a lot of constructors for entity with set method
public class User extends DataClass<User> {
              public String name, lastName;
              public Type type;
}
Can be initialized:
User user = new User.set(u-> {u.name = "Roman"; u.type = VIP;})
3) JFunc and JAction - lambda interfaces for functions/procedures with 0-9 input parameters with same name


Best regards,

Roman Iovlev
Chief Software Test Automation Engineer

Office: +7 812 611 10 94<tel:+7%20812%20611%2010%2094> x 61604<tel:61604>   Email: roman_iovlev at epam.com<mailto:roman_iovlev at epam.com>
Saint-Petersburg, Russia (GMT+3)   epam.com<http://www.epam.com/>

Skype: roman.iovlev | Mobile: +7(953)155-73-82



More information about the amber-dev mailing list