Skip to content

8369184: SimpleTimeZone equals() Returns True for Unequal Instances with Different hashCode Values#27660

Closed
naotoj wants to merge 5 commits intoopenjdk:masterfrom
naotoj:JDK-8369184-SimpleTimeZone-hashCode
Closed

8369184: SimpleTimeZone equals() Returns True for Unequal Instances with Different hashCode Values#27660
naotoj wants to merge 5 commits intoopenjdk:masterfrom
naotoj:JDK-8369184-SimpleTimeZone-hashCode

Conversation

@naotoj
Copy link
Member

@naotoj naotoj commented Oct 6, 2025

Fixing the equals/hashCode contract in the SimpleTimeZone class. The current implementation includes DST rule fields in hash code computation even for zones that do not observe DST, while equals() always considers them. Also correcting the example code in the class description, where it used 20-year-old obsolete "America/Los_Angeles" rule.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8369184: SimpleTimeZone equals() Returns True for Unequal Instances with Different hashCode Values (Bug - P3)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27660/head:pull/27660
$ git checkout pull/27660

Update a local copy of the PR:
$ git checkout pull/27660
$ git pull https://git.openjdk.org/jdk.git pull/27660/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27660

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27660.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 6, 2025

👋 Welcome back naoto! 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.

@openjdk
Copy link

openjdk bot commented Oct 6, 2025

@naotoj 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:

8369184: SimpleTimeZone equals() Returns True for Unequal Instances with Different hashCode Values

Reviewed-by: prappo, jlu, rriggs, 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 58 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this 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 core-libs core-libs-dev@openjdk.org i18n i18n-dev@openjdk.org labels Oct 6, 2025
@openjdk
Copy link

openjdk bot commented Oct 6, 2025

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

  • core-libs
  • i18n

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.

@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 6, 2025
@mlbridge
Copy link

mlbridge bot commented Oct 6, 2025

Webrevs

Copy link
Member

@justin-curtis-lu justin-curtis-lu left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

Choose a reason for hiding this comment

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

Include equals in the test name as well? Perhaps HashCodeEqualsTest or something along those lines.

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed. I left SimpleTimeZone as the test is specific for this class.

{
return startMonth ^ startDay ^ startDayOfWeek ^ startTime ^
endMonth ^ endDay ^ endDayOfWeek ^ endTime ^ rawOffset;
int hash = 31 * getID().hashCode() + rawOffset;
Copy link
Member

Choose a reason for hiding this comment

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

int hash = Objects.hash(getID(), rawOffset, useDaylight);
return !useDaylight ? hash : 31 * hash + Objects.hash(
        startMonth, startDay, startDayOfWeek, startTime, endMonth, endDay, endDayOfWeek, endTime);

Seems reasonable to use Objects.hash here. Could save some lines if wanted?

Copy link
Member Author

Choose a reason for hiding this comment

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

Initially, I thought so, but decided to avoid extra array allocation/copy. But on second thought, the instances of this class would seldom be used as hash keys so not that performance critical. Either way is fine with me

Copy link
Contributor

@RogerRiggs RogerRiggs left a comment

Choose a reason for hiding this comment

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

LGTM

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 8, 2025
@@ -112,15 +112,15 @@
* <pre><code>
Copy link
Member

Choose a reason for hiding this comment

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

We can consider replacing <pre><code> with @snippet so that the code example can be copied by readers. Although I understand if leaving it for another time as to not invalidate the existing approvals.

Copy link
Member

Choose a reason for hiding this comment

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

I think, that's out of scope for this PR.

stz.setEndRule(NOVEMBER, 8, -SUNDAY, 7_200_000);
assertNotEquals(STZ_WITH_DST, stz);
// from the contract point, hash codes may be the same
assertNotEquals(STZ_WITH_DST.hashCode(), stz.hashCode());
Copy link
Member

Choose a reason for hiding this comment

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

Like you said, from !a.equals(b) it does not follow that a.hashCode() != b.hashCode(). Unless you want hash code to specifically have this additional property, I'd remove this assertion.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to test the implementation which takes DST related fields into consideration for hash code calculation. Added some comments for clarity

stz.setEndRule(NOVEMBER, 8, -SUNDAY, 7_200_000);
assertTrue(stz.useDaylightTime());
assertNotEquals(STZ_WITHOUT_DST, stz);
assertNotEquals(STZ_WITHOUT_DST.hashCode(), stz.hashCode());
Copy link
Member

Choose a reason for hiding this comment

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

Ditto on equals-hashCode contract.

Co-authored-by: Pavel Rappo <32523691+pavelrappo@users.noreply.github.com>
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Oct 8, 2025
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 8, 2025
@naotoj
Copy link
Member Author

naotoj commented Oct 9, 2025

Thanks for the reviews!
/integrate

@openjdk
Copy link

openjdk bot commented Oct 9, 2025

Going to push as commit 37b725d.
Since your change was applied there have been 82 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 9, 2025
@openjdk openjdk bot closed this Oct 9, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 9, 2025
@openjdk
Copy link

openjdk bot commented Oct 9, 2025

@naotoj Pushed as commit 37b725d.

💡 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

Labels

core-libs core-libs-dev@openjdk.org i18n i18n-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

5 participants