Skip to content

Commit 55db3ba

Browse files
committed
first commit
0 parents  commit 55db3ba

6 files changed

+144
-0
lines changed

README

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
when you run.sh the container will run the demo in jshell
2+
===========================================================
3+
to re-execute it while in jshell..
4+
5+
jshell> /open /root/jdkSwitchDemo.jsh
6+
==========================================================
7+
When you want to list the code snippets in jshell script
8+
jshell> /list
9+
10+
1 : System.out.println("MVP Java - JDK 12 Switch Demo")
11+
2 : import java.time.DayOfWeek;
12+
3 : DayOfWeek day = DayOfWeek.MONDAY;
13+
4 : switch (day) {
14+
case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
15+
case TUESDAY -> System.out.println(7);
16+
case THURSDAY, SATURDAY -> System.out.println(8);
17+
case WEDNESDAY -> System.out.println(9);
18+
}
19+
20+
then you can /list 1
21+
for individual snippets

jdk12SwitchDemoNew.jsh

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
System.out.println("MVP Java - JDK 12 Switch Demo - New")
2+
3+
public enum SYSTEM {
4+
POWER, ACS, THERMAL, COMS
5+
}
6+
7+
public double calculatePowerUsgae(SYSTEM system)
8+
{
9+
double power = switch (system)
10+
{
11+
case POWER -> 200.0D;
12+
case ACS -> {
13+
//i.e: Sum Power Usage from all Actuators
14+
double yesThisIsReallyLocalScope = 10 + 20 + 30;
15+
break yesThisIsReallyLocalScope;
16+
}
17+
case THERMAL, COMS -> 110.0D;
18+
};
19+
20+
return power;
21+
}
22+
23+
SYSTEM acs = SYSTEM.ACS;
24+
25+
System.out.printf ("Attitude Control System (ACS) taking %.3f mW of Power",
26+
calculatePowerUsgae(acs))

jdk12SwitchDemoNew_Exhaustiveness.jsh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
System.out.println("MVP Java - JDK 12 Switch Demo - New Exhaustive")
2+
3+
public enum SYSTEM {
4+
POWER, ACS, THERMAL, COMS, PAYLOAD
5+
}
6+
7+
public double calculatePowerUsgae(SYSTEM system)
8+
{
9+
double power = switch (system)
10+
{
11+
case POWER -> 200;
12+
case ACS -> 140;
13+
case THERMAL, COMS -> 110;
14+
// Compile Error: the switch expression does not cover all possible input values
15+
//case PAYLOAD -> 342.67;
16+
};
17+
18+
return power;
19+
}
20+
21+
SYSTEM acs = SYSTEM.ACS;
22+
23+
System.out.printf ("Attitude Control System (ACS) taking %.3f mW of Power", calculatePowerUsgae(acs))

jdk12SwitchDemoOriginalExpression.jsh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
System.out.println("MVP Java - JDK 12 Switch Demo - Original")
2+
3+
public enum SYSTEM {
4+
POWER, ACS, THERMAL, COMS
5+
}
6+
7+
public double calculatePowerUsgae(SYSTEM system)
8+
{
9+
double power = switch (system) {
10+
case POWER:
11+
double thisReallyIsGlobalScope = 10 * 20;
12+
break 200 + thisReallyIsGlobalScope;
13+
//break; compile error: missing break value
14+
15+
case ACS:
16+
thisReallyIsGlobalScope = 1;
17+
break 140 + thisReallyIsGlobalScope;
18+
19+
case THERMAL: case COMS:
20+
break 110;
21+
22+
default:
23+
throw new RuntimeException("Invalid Sub-System <" + system + "> encountered");
24+
};
25+
26+
return power;
27+
}
28+
29+
SYSTEM acs = SYSTEM.ACS
30+
31+
System.out.printf ("Attitude Control System (ACS) taking %.3f mW of Power", calculatePowerUsgae(acs))
32+
33+

run.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
docker run --rm -it -v $PWD:/root openjdk:12-jdk-oracle jshell --enable-preview /root/switchDemoOriginal.jsh
2+

switchDemoOriginal.jsh

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
System.out.println("MVP Java - JDK 12 Switch Demo - Original")
2+
3+
public enum SYSTEM {
4+
POWER, ACS, THERMAL, COMS
5+
}
6+
7+
public double calculatePowerUsgae(SYSTEM system)
8+
{
9+
double power = 0;
10+
11+
switch (system) {
12+
case POWER:
13+
double thisReallyIsGlobalScope = 10 * 20;
14+
power = 200 + thisReallyIsGlobalScope;
15+
break;
16+
17+
case ACS:
18+
thisReallyIsGlobalScope = 1;
19+
power = 140 + thisReallyIsGlobalScope;
20+
break;
21+
22+
case THERMAL: case COMS:
23+
power = 110;
24+
break;
25+
26+
default:
27+
System.out.println ("Invalid Sub-System <" + system + "> encountered");
28+
break;
29+
}
30+
31+
return power;
32+
}
33+
34+
35+
SYSTEM acs = SYSTEM.ACS
36+
37+
System.out.printf ("Attitude Control System (ACS) taking %.3f mW of Power", calculatePowerUsgae(acs))
38+
39+

0 commit comments

Comments
 (0)