Skip to content

Make use of Java 17 pattern matching switch statements#17909

Open
JaySabva wants to merge 6 commits intoopensearch-project:mainfrom
JaySabva:main
Open

Make use of Java 17 pattern matching switch statements#17909
JaySabva wants to merge 6 commits intoopensearch-project:mainfrom
JaySabva:main

Conversation

@JaySabva
Copy link
Copy Markdown

Description

Changed if-else instaceof check to JAVA 17 pattern matching switch

Related Issues

Resolves #17874

Check List

  • Functionality includes testing.
  • API changes companion pull request created, if applicable.
  • Public documentation issue/PR created, if applicable.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 38eeb9d: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

@github-actions
Copy link
Copy Markdown
Contributor

❕ Gradle check result for f3e2454: UNSTABLE

Please review all flaky tests that succeeded after retry and create an issue if one does not already exist to track the flaky failure.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 12, 2025

Codecov Report

Attention: Patch coverage is 79.10448% with 14 lines in your changes missing coverage. Please review.

Project coverage is 72.44%. Comparing base (1628152) to head (f3e2454).
Report is 25 commits behind head on main.

Files with missing lines Patch % Lines
...java/org/opensearch/index/search/NestedHelper.java 76.92% 5 Missing and 7 partials ⚠️
...src/main/java/org/opensearch/ExceptionsHelper.java 86.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##               main   #17909   +/-   ##
=========================================
  Coverage     72.43%   72.44%           
- Complexity    66789    66809   +20     
=========================================
  Files          5449     5452    +3     
  Lines        309085   309181   +96     
  Branches      44979    44963   -16     
=========================================
+ Hits         223899   223998   +99     
+ Misses        66906    66843   -63     
- Partials      18280    18340   +60     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for f2ae0fa: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

Copy link
Copy Markdown
Member

@owaiskazi19 owaiskazi19 left a comment

Choose a reason for hiding this comment

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

Thanks for raising a PR @JaySabva! There are other classes as well. For ex:

  1. https://github.com/opensearch-project/OpenSearch/blob/main/plugins/repository-s3/src/internalClusterTest/java/org/opensearch/repositories/s3/S3BlobStoreRepositoryTests.java#L357-L368
  2. https://github.com/opensearch-project/OpenSearch/blob/main/buildSrc/src/main/java/org/opensearch/gradle/util/Util.java#L135-L145
    To name a few.
    Also, DCO check is failing. Can you sign your commit with git commit -s --amend and push again? To sign the commits you need to include -s flag

return true;
}
case OpenSearchToParentBlockJoinQuery ostpbjq -> ostpbjq.getPath() != null;
case null -> true;
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.

Wouldn't this map to default case?

Copy link
Copy Markdown
Author

@JaySabva JaySabva Apr 14, 2025

Choose a reason for hiding this comment

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

I think that will raise null pointer exception I tried that in my scratch file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In this and the other cases with case null, I believe you can collapse them both into a single branch, like:

case null, default -> true

} else {
return true;
}
case null -> true;
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.

Here too

@JaySabva
Copy link
Copy Markdown
Author

I thought only instance of if else.

Sure I will make changes.

…witch statements

Signed-off-by: Jay Sabva <202101224@daiict.ac.in>
…witch statements

Signed-off-by: Jay Sabva <202101224@daiict.ac.in>
…witch statements

Signed-off-by: Jay Sabva <202101224@daiict.ac.in>
…witch statements

Signed-off-by: Jay Sabva <202101224@daiict.ac.in>
…witch statements

Signed-off-by: Jay Sabva <202101224@daiict.ac.in>
…witch statements

Signed-off-by: Jay Sabva <202101224@daiict.ac.in>
@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 5f4340f: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

Copy link
Copy Markdown
Member

@owaiskazi19 owaiskazi19 left a comment

Choose a reason for hiding this comment

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

We can combine the cases

Comment on lines +96 to +104
return switch (t) {
case OpenSearchException ose -> ose.status();
case IllegalArgumentException iae -> RestStatus.BAD_REQUEST;
case JsonParseException jpe -> RestStatus.BAD_REQUEST;
case OpenSearchRejectedExecutionException osre -> RestStatus.TOO_MANY_REQUESTS;
case NotXContentException nxce -> RestStatus.BAD_REQUEST;
case null -> RestStatus.INTERNAL_SERVER_ERROR;
default -> RestStatus.INTERNAL_SERVER_ERROR;
};
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.

Suggested change
return switch (t) {
case OpenSearchException ose -> ose.status();
case IllegalArgumentException iae -> RestStatus.BAD_REQUEST;
case JsonParseException jpe -> RestStatus.BAD_REQUEST;
case OpenSearchRejectedExecutionException osre -> RestStatus.TOO_MANY_REQUESTS;
case NotXContentException nxce -> RestStatus.BAD_REQUEST;
case null -> RestStatus.INTERNAL_SERVER_ERROR;
default -> RestStatus.INTERNAL_SERVER_ERROR;
};
return switch (t) {
case OpenSearchException ose -> ose.status();
case IllegalArgumentException || JsonParseException || NotXContentException -> RestStatus.BAD_REQUEST;
case OpenSearchRejectedExecutionException -> RestStatus.TOO_MANY_REQUESTS;
case null, Throwable _ -> RestStatus.INTERNAL_SERVER_ERROR;
};

@opensearch-trigger-bot
Copy link
Copy Markdown
Contributor

This PR is stalled because it has been open for 30 days with no activity.

@opensearch-trigger-bot opensearch-trigger-bot bot added the stalled Issues that have stalled label Jun 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Enhancement or improvement to existing feature or request good first issue Good for newcomers stalled Issues that have stalled

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Make use of Java 17 pattern matching switch statements where possible

3 participants