Skip to content

Commit 6df4b88

Browse files
authored
chore: Move java 21 code to a dedicated folder and including code sni… (#1023)
* chore: Move java 21 code to a dedicated folder and including code snippe with tab. * chore: Update OptimizedActorWithJava21.java with new line.
1 parent 80a177b commit 6df4b88

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
// #pattern-matching
3+
4+
static class OptimizedActorWithJava21 extends UntypedAbstractActor {
5+
public static class Msg1 {}
6+
7+
public static class Msg2 {}
8+
9+
public static class Msg3 {}
10+
11+
@Override
12+
public void onReceive(Object msg) throws Exception {
13+
switch(msg) {
14+
case Msg1 msg -> receiveMsg1((Msg1) msg);
15+
case Msg2 msg -> receiveMsg2((Msg2) msg);
16+
case Msg3 msg -> receiveMsg3((Msg3) msg);
17+
default _ -> unhandled(msg);
18+
}
19+
}
20+
21+
private void receiveMsg1(Msg1 msg) {
22+
// actual work
23+
}
24+
25+
private void receiveMsg2(Msg2 msg) {
26+
// actual work
27+
}
28+
29+
private void receiveMsg3(Msg3 msg) {
30+
// actual work
31+
}
32+
}
33+
// #pattern-matching

docs/src/main/paradox/actors.md

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -838,42 +838,14 @@ that the JVM can have problems optimizing and the resulting code might not be as
838838
untyped version. When extending `UntypedAbstractActor` each message is received as an untyped
839839
`Object` and you have to inspect and cast it to the actual message type in other ways, like this:
840840

841-
@@snip [ActorDocTest.java](/docs/src/test/java/jdocs/actor/ActorDocTest.java) { #optimized }
841+
In addition, Java 21 introduces [powerful pattern matching for switch](https://openjdk.org/jeps/441) supporting any
842+
reference type. We can use `switch` instead of `if ... else ...` for conditional branches:
842843

843-
In addition, Java 21 introduces [powerful pattern matching for switch](https://openjdk.org/jeps/441) supporting any reference type. We can use `switch` instead of `if ... else ...` for conditional branches:
844-
845-
```java
846-
static class PatternMatchedActor extends UntypedAbstractActor {
847-
848-
public static class Msg1 {}
849-
850-
public static class Msg2 {}
851-
852-
public static class Msg3 {}
853-
854-
@Override
855-
public void onReceive(Object msg) throws Exception {
856-
switch(msg) {
857-
case Msg1 msg -> receiveMsg1((Msg1) msg);
858-
case Msg2 msg -> receiveMsg2((Msg2) msg);
859-
case Msg3 msg -> receiveMsg3((Msg3) msg);
860-
default _ -> unhandled(msg);
861-
}
862-
}
863-
864-
private void receiveMsg1(Msg1 msg) {
865-
// actual work
866-
}
867-
868-
private void receiveMsg2(Msg2 msg) {
869-
// actual work
870-
}
844+
Java
845+
: @@snip [ActorDocTest.java](/docs/src/test/java/jdocs/actor/ActorDocTest.java) { #optimized }
871846

872-
private void receiveMsg3(Msg3 msg) {
873-
// actual work
874-
}
875-
}
876-
```
847+
Java 21
848+
: @@snip [OptimizedActorWithJava21.java](/docs/src/main/java-jdk-21/docs/actors/classical/OptimizedActorWithJava21.java) { #pattern-matching }
877849

878850
@@@
879851

0 commit comments

Comments
 (0)