Skip to content

refactor: modernize Java example code with Java 17 features#171

Closed
He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:refactor/java17-modernization-examples
Closed

refactor: modernize Java example code with Java 17 features#171
He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:refactor/java17-modernization-examples

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 18, 2026

Copy link
Copy Markdown
Member

Motivation

Java 17 introduced records, pattern matching for instanceof, and other features that make Java code more concise and readable. The example code contained many verbose data carrier classes and instanceof + explicit cast patterns that can be significantly simplified.

Modification

  • Convert data carrier classes (commands, events, messages) to Java records across sample applications
  • Use pattern matching for instanceof to eliminate explicit casts
  • Update all field accesses from direct field access to record accessor methods
  • Remove redundant equals/hashCode/constructor boilerplate

Files changed:

  • pekko-sample-persistence-java/: ShoppingCart
  • pekko-sample-distributed-data-java/: ShoppingCart, VotingService, ReplicatedCache
  • pekko-sample-cluster-client-grpc-java/: ClusterClient
  • pekko-sample-persistence-dc-java/: ThumbsUpCounter

Result

Example Java code is more concise and idiomatic for Java 17+, reducing boilerplate while maintaining the same behavior. Net reduction of ~350 lines of code across 6 files.

Tests

Not run - sample applications without automated test suite

References

None - code modernization

Motivation:
Java 17 introduced records, pattern matching for instanceof, and other
features that make Java code more concise and readable. The example code
contained many verbose data carrier classes and instanceof patterns that
can be significantly simplified.

Modification:
- Convert data carrier classes (commands, events, messages) to Java records
  in sample applications: ShoppingCart, ClusterClient, ReplicatedCache,
  VotingService, ThumbsUpCounter
- Use pattern matching for instanceof to eliminate explicit casts
- Update all field accesses from direct field access to record accessor
  methods
- Remove redundant equals/hashCode/constructor boilerplate

Result:
Example Java code is more concise and idiomatic for Java 17+, reducing
boilerplate while maintaining the same behavior. Net reduction of ~350
lines of code across 6 files.

Tests:
Not run - sample applications without automated test suite

References:
None - code modernization

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our samples are still pekko 1 based and that probably means also supporting java 8

@He-Pin He-Pin marked this pull request as draft June 18, 2026 21:31
@He-Pin

He-Pin commented Jun 19, 2026

Copy link
Copy Markdown
Member Author

Closing this PR as pekko-samples still needs to support Java 8 (pekko 1.x based), which means Java 17 records cannot be used. Thanks @pjfanning for the review.

@He-Pin He-Pin closed this Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants