Skip to content

[LANG-980] Use equals() instead of == to compare Token values in DurationFormatUtils - #1853

Closed
u7924412 wants to merge 3 commits into
apache:masterfrom
u7924412:LANG-980
Closed

u7924412 wants to merge 3 commits into
apache:masterfrom
u7924412:LANG-980

Conversation

@u7924412

@u7924412 u7924412 commented Nov 1, 2026

Copy link
Copy Markdown

Fixes LANG-980.

DurationFormatUtils.Token compared token values with == in two places:

  • Token.containsTokenWithValue(Token[], Object)
  • Token.equals(Object)

This only worked because the values being compared were always the same String constant instances (y, M, d, H, m, s, S). A token holding an equal but different String instance was not recognised, which would cause formatDuration/formatPeriod to silently leave that unit out.

This PR changes both comparisons to use equals(), matching how lexx() already compares token values (previous.getValue().equals(value)).

  • Token values are never null (the constructor uses Objects.requireNonNull), so calling equals() is safe.
  • Literal-text tokens hold a StringBuilder, which does not override equals(), so their behaviour is unchanged.
  • No public API changes.

Added testLANG980, which fails before this change and passes after it.

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Claude (Anthropic) was used to help investigate the issue, suggest the fix and test, and set up the build. I reviewed, built and tested the change myself.
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

@u7924412

u7924412 commented Nov 1, 2026

Copy link
Copy Markdown
Author

After opening the pull request, the project’s CI checks failed. Inspecting the logs showed the failure was unrelated to our change: seven cases of FastDatePrinterTimeZonesTest, which formats the current time, failed because the run happened during the US/Mexico daylight-saving change on 1 November 2026. Our own DurationFormatUtilsTest, including testLANG980, passed (54 tests, 0 failures).

@garydgregory
garydgregory marked this pull request as draft November 1, 2026 11:25
@garydgregory

garydgregory commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

@u7924412 Rebase on git master. Set to draft.

@garydgregory

Copy link
Copy Markdown
Member

@u7924412
I think PR #1856 is what the Jira ticket is about. I think we can close this PR.

@garydgregory

Copy link
Copy Markdown
Member

Closing: See above.

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