Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8267587: Update java.util to use enhanced switch #4161

Closed
wants to merge 9 commits into from

Conversation

@amaembo
Copy link
Contributor

@amaembo amaembo commented May 24, 2021

Inspired by PR#4088. Most of the changes are done automatically using IntelliJ IDEA refactoring. Some manual adjustments are also performed, including indentations, moving comments, extracting common cast out of switch expression branches, etc.

I also noticed that there are some switches having one branch only in JapaneseImperialCalendar.java. Probably it would be better to replace them with if statement?


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/4161/head:pull/4161
$ git checkout pull/4161

Update a local copy of the PR:
$ git checkout pull/4161
$ git pull https://git.openjdk.java.net/jdk pull/4161/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 4161

View PR using the GUI difftool:
$ git pr show -t 4161

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/4161.diff

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented May 24, 2021

👋 Welcome back tvaleev! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

MULTI_RELEASE_ENABLED = true;
MULTI_RELEASE_FORCED = true;
break;
}
default -> {
Copy link
Contributor Author

@amaembo amaembo May 24, 2021

Choose a reason for hiding this comment

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

Here, explicit case "true" disappeared. Not sure if it's important to keep it.

@openjdk openjdk bot added the rfr label May 24, 2021
@openjdk
Copy link

@openjdk openjdk bot commented May 24, 2021

@amaembo The following labels will be automatically applied to this pull request:

  • core-libs
  • i18n
  • security

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@mlbridge
Copy link

@mlbridge mlbridge bot commented May 24, 2021

@@ -1741,8 +1727,7 @@ public int getActualMaximum(int field) {

int value = -1;
switch (field) {
case MONTH:
{
case MONTH -> {
Copy link
Contributor

@pconcannon pconcannon May 25, 2021

Choose a reason for hiding this comment

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

HI @amaembo, thanks for taking a look at this.

I think you should be careful here, and ask is introducing the enchanced switch adding any value? While I think the enchanced switch can be valuable when it makes the code more readable, it shouldn't be introduced just for the sake of using it.

Copy link
Contributor Author

@amaembo amaembo May 25, 2021

Choose a reason for hiding this comment

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

@pconcannon thank you for review!

In this particular place, it makes the code more better in the following points:

  • It removes break; operators at the end of each branch, which add nothing but a visual noise
  • It makes the whole switch shorter by 22 lines
  • Using -> syntax clearly states that no branch in this switch has a fall-through behavior, so the code reader should not check every branch to ensure this. Just see very first -> and you already know that no fallthrough is here.
  • In case if new branches will appear in the future, the new-style switch will protect future maintainers from accidental fall-through, an error that often happens with old-style switches.

// The value should depend on the time zone of this calendar.
LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE,
getZone());
LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE, getZone());
return Math.max(LEAST_MAX_VALUES[YEAR], d.getYear());
}
}
Copy link
Contributor

@briangoetz briangoetz May 25, 2021

Choose a reason for hiding this comment

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

A switch with one element here is kind of weird. I would turn this into "return switch (field) { ... }", with two cases, YEAR and default.

Copy link
Contributor Author

@amaembo amaembo May 26, 2021

Choose a reason for hiding this comment

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

Done, also at line 1169

} else if (date.getYear() == getMinimum(YEAR)) {
CalendarDate d1 = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
long fd1 = jcal.getFixedDate(d1);
d1.addYear(1);
d1.setMonth(BaseCalendar.JANUARY).setDayOfMonth(1);
jcal.normalize(d1);
long fd2 = jcal.getFixedDate(d1);
value = (int)(fd2 - fd1);
value = (int) (fd2 - fd1);
} else {
value = jcal.getYearLength(date);
}
}
}
Copy link
Contributor

@briangoetz briangoetz May 25, 2021

Choose a reason for hiding this comment

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

Pull value assignment out of switch?

Copy link
Contributor Author

@amaembo amaembo May 26, 2021

Choose a reason for hiding this comment

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

This is a much bigger change which is probably harder to review. I did it, please take a look. One point is whether to unwrap the final else after yield:

if (...) {
    ...
    yield ...
} else { // should we remove else?
    ...
}

I prefer unwrapping, as this reduces the indentation, so I did it. Please tell me if this contradicts with the preferred OpenJDK style.

@@ -1313,13 +1300,10 @@ public int getActualMaximum(int field) {

JapaneseImperialCalendar jc = getNormalizedCalendar();
LocalGregorianCalendar.Date date = jc.jdate;
int normalizedYear = date.getNormalizedYear();
Copy link
Contributor Author

@amaembo amaembo May 26, 2021

Choose a reason for hiding this comment

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

This variable was unused

} else {
case WEEK_OF_MONTH -> {
LocalGregorianCalendar.Date jd = jcal.getCalendarDate(Long.MAX_VALUE, getZone());
if (date.getEra() == jd.getEra() && date.getYear() == jd.getYear()) {
Copy link
Contributor Author

@amaembo amaembo May 26, 2021

Choose a reason for hiding this comment

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

I inverted the if condition here, as it was negated and a much shorter branch was in else.

@openjdk
Copy link

@openjdk openjdk bot commented May 26, 2021

@amaembo This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8267587: Update java.util to use enhanced switch

Reviewed-by: iris

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been no new commits pushed to the master branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready label May 26, 2021
Copy link
Member

@dfuch dfuch left a comment

Globally looks good.
I haven't reviewed src/java.base/share/classes/java/util/regex/Pattern.java due to formatting issues. See also my other remark about java.util.concurrent.

@amaembo
Copy link
Contributor Author

@amaembo amaembo commented May 31, 2021

/integrate

@openjdk openjdk bot closed this May 31, 2021
@openjdk openjdk bot added integrated and removed ready rfr labels May 31, 2021
@openjdk
Copy link

@openjdk openjdk bot commented May 31, 2021

@amaembo Since your change was applied there have been 4 commits pushed to the master branch:

  • 35916ed: 8267806: C1: Relax inlining checks for not yet initialized classes
  • 1e29005: 8267842: SIGSEGV in get_current_contended_monitor
  • 236bd89: 8263583: Emoji rendering on macOS
  • 1ab2776: 8247608: Javadoc: CSS margin is not applied consistently

Your commit was automatically rebased without conflicts.

Pushed as commit ab5a7ff.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment