Skip to content

Commit

Permalink
chore: Move java 21 code to a dedicated folder and including code sni… (
Browse files Browse the repository at this point in the history
#1023)

* chore: Move java 21 code to a dedicated folder and including code snippe with tab.

* chore: Update OptimizedActorWithJava21.java with new line.
  • Loading branch information
He-Pin authored Jan 23, 2024
1 parent 80a177b commit 6df4b88
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

// #pattern-matching

static class OptimizedActorWithJava21 extends UntypedAbstractActor {
public static class Msg1 {}

public static class Msg2 {}

public static class Msg3 {}

@Override
public void onReceive(Object msg) throws Exception {
switch(msg) {
case Msg1 msg -> receiveMsg1((Msg1) msg);
case Msg2 msg -> receiveMsg2((Msg2) msg);
case Msg3 msg -> receiveMsg3((Msg3) msg);
default _ -> unhandled(msg);
}
}

private void receiveMsg1(Msg1 msg) {
// actual work
}

private void receiveMsg2(Msg2 msg) {
// actual work
}

private void receiveMsg3(Msg3 msg) {
// actual work
}
}
// #pattern-matching
40 changes: 6 additions & 34 deletions docs/src/main/paradox/actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -838,42 +838,14 @@ that the JVM can have problems optimizing and the resulting code might not be as
untyped version. When extending `UntypedAbstractActor` each message is received as an untyped
`Object` and you have to inspect and cast it to the actual message type in other ways, like this:

@@snip [ActorDocTest.java](/docs/src/test/java/jdocs/actor/ActorDocTest.java) { #optimized }
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:

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:

```java
static class PatternMatchedActor extends UntypedAbstractActor {

public static class Msg1 {}

public static class Msg2 {}

public static class Msg3 {}

@Override
public void onReceive(Object msg) throws Exception {
switch(msg) {
case Msg1 msg -> receiveMsg1((Msg1) msg);
case Msg2 msg -> receiveMsg2((Msg2) msg);
case Msg3 msg -> receiveMsg3((Msg3) msg);
default _ -> unhandled(msg);
}
}

private void receiveMsg1(Msg1 msg) {
// actual work
}

private void receiveMsg2(Msg2 msg) {
// actual work
}
Java
: @@snip [ActorDocTest.java](/docs/src/test/java/jdocs/actor/ActorDocTest.java) { #optimized }

private void receiveMsg3(Msg3 msg) {
// actual work
}
}
```
Java 21
: @@snip [OptimizedActorWithJava21.java](/docs/src/main/java-jdk-21/docs/actors/classical/OptimizedActorWithJava21.java) { #pattern-matching }

@@@

Expand Down

0 comments on commit 6df4b88

Please sign in to comment.