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

8347949: Currency method to stream available Currencies #23165

Conversation

justin-curtis-lu
Copy link
Member

@justin-curtis-lu justin-curtis-lu commented Jan 16, 2025

Please review this PR and CSR which adds a method to java.util.Currency which returns a stream of the available Currencies.

The motivation can be found in the CSR.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires CSR request JDK-8347950 to be approved
  • Change must be properly reviewed (2 reviews required, with at least 2 Reviewers)

Issues

  • JDK-8347949: Currency method to stream available Currencies (Enhancement - P4)
  • JDK-8347950: Currency method to stream available Currencies (CSR)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 23165

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 16, 2025

👋 Welcome back jlu! 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 Jan 16, 2025

@justin-curtis-lu 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:

8347949: Currency method to stream available Currencies

Reviewed-by: naoto, liach, rriggs

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 17 new commits pushed to the master branch:

  • cbe9ec5: 8348905: Add support to specify the JDK for compiling Jtreg tests
  • 6b581d2: 8347997: assert(false) failed: EA: missing memory path
  • 4662363: 8348687: [BACKOUT] C2: Non-fluid StringBuilder pattern bails out in OptoStringConcat
  • d266ca9: 8348752: Enable -XX:+AOTClassLinking by default when -XX:AOTMode is specified
  • cbc89a7: 8348898: Remove unused OctalDigits to clean up code
  • 96fefed: 8319850: PrintInlining should print which methods are late inlines
  • 51cce6e: 8318577: Windows Look-and-Feel JProgressBarUI does not render correctly on 2x UI scale
  • 6bfae3a: 8333386: TestAbortOnVMOperationTimeout test fails for client VM
  • f98d9a3: 8348870: Eliminate array bound checks in DecimalDigits
  • fe6d9ab: 8348582: Set -fstack-protector when building with clang
  • ... and 7 more: https://git.openjdk.org/jdk/compare/3a564ed1019c66c28afa729973948d3a6e6c4c41...master

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 csr Pull request needs approved CSR before integration rfr Pull request is ready for review labels Jan 16, 2025
@openjdk
Copy link

openjdk bot commented Jan 16, 2025

@justin-curtis-lu 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.

@mlbridge
Copy link

mlbridge bot commented Jan 16, 2025

Copy link
Member

@naotoj naotoj left a comment

Choose a reason for hiding this comment

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

Looks good.

// Initialize the set of available currencies if needed
private static synchronized void initAvailableCurrencies() {
if (available == null) {
available = new HashSet<>(256);
Copy link
Member

Choose a reason for hiding this comment

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

Not related to your change, but this should be HashSet.newHashSet(256). In fact 256*0.75 is 192, which is smaller than the current number of currencies (230, as of JDK24)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in c94ba94

* {@return a set of available currencies} The returned set of currencies
* contains all the available currencies, which may include currencies
* that represent obsolete ISO 4217 codes. If there is no currency available
* in the runtime, the returned set is empty. The set can be modified
Copy link
Contributor

Choose a reason for hiding this comment

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

An apiNote might be appropriate to refer/recommend using the new availableCurrencies() API.
And/or an @see link to the method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks Roger, incorporated both your suggestions in dcb4a9b. Note, I intentionally did not add any wording regarding the defensive copy in the apiNote, considering that is an implementation specific caveat.

* available in the runtime, the returned stream is empty.
*
* @implNote Unlike {@link #getAvailableCurrencies()}, this method does
* not create a defensive copy of the {@code Currency} set.
Copy link
Contributor

Choose a reason for hiding this comment

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

An @see link to getAvailableCurrencies might be useful.

@Test
public void streamEqualsSetTest() {
var currencies = Currency.getAvailableCurrencies();
assertEquals(currencies, Currency.availableCurrencies().collect(Collectors.toSet()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Generally, a message describing the failure is more useful than just a stacktrace. (the 3rd arg on assertEquals override.)
It avoids needing to find the source code to create the bug report.

}

// Initialize the set of available currencies if needed
private static synchronized void initAvailableCurrencies() {
Copy link
Member

Choose a reason for hiding this comment

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

Should we make available volatile so we can avoid entering the monitor if the currencies are initialized?

(Note: We may get Stable Values very soon, at that point SV is a much better solution)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks Chen, made available volatile and implemented DCL in 96e86c8.
Also added a duplicate elements test as you suggested.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure how useful it is to optimize the performance of availableCurrencies access to "available", but adding volatile will slow every access down.
The computation of the available currencies is stable, so a race computing it is benign.
Except for the available hashSet being partially filled when read by the thread calling getAvailableCurrencies().
That could be remedied by using a new local for the new HashSet in initAvailableCurrencies and assigning to available only when the set is completely initialized.
(And yes, this looks like a good fit for stable values.)

Copy link
Member Author

Choose a reason for hiding this comment

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

How about I revert the most recent commit and have this PR simply deliver the change to provide the stream of currencies. I'll file another issue to improve available to use stable values when that is available. Does that sound fine?

Copy link
Member Author

Choose a reason for hiding this comment

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

Reverted in ade21c9. Filed JDK-8348351 to implement available as a stable value when applicable.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect that Stable Values would fit well here.

var currencies = Currency.getAvailableCurrencies();
assertEquals(currencies, Currency.availableCurrencies().collect(Collectors.toSet()),
"availableCurrencies() and getAvailableCurrencies() do not have the same elements");
}
Copy link
Member

Choose a reason for hiding this comment

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

Should we add a test to ensure Currency.availableCurrencies() has no duplicate elements?

Currency.availableCurrencies().collect(Collectors.toMap(Function.identity(), Function.identity());

SpecialCaseEntry scEntry = specialCasesList.get(index);

if (scEntry.cutOverTime == Long.MAX_VALUE
|| System.currentTimeMillis() < scEntry.cutOverTime) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be better to obtain the current time once and compare against the same value for all entries rather than potentially using different current times for each comparison.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good point. I filed JDK-8348205 because that change would have its own behavioral concerns, which I don't want to overlap with this issue/CSR.

@openjdk openjdk bot removed the csr Pull request needs approved CSR before integration label Jan 22, 2025
Copy link
Member

@liach liach left a comment

Choose a reason for hiding this comment

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

The PR looks good in the current form. Since I am no professional in locale area, requesting another review from Naoto or Roger.

/reviewers 2 reviewer

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 28, 2025
@openjdk
Copy link

openjdk bot commented Jan 28, 2025

@liach
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 2 Reviewers).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jan 28, 2025
Copy link
Member

@naotoj naotoj left a comment

Choose a reason for hiding this comment

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

We could add @stable to available for now, but forseeing the stable values, not that significant. So fine by me as in its current form

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 28, 2025
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
Copy link

openjdk bot commented Jan 28, 2025

@justin-curtis-lu this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout 8347949-Currency-Stream
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added merge-conflict Pull request has merge conflict with target branch and removed ready Pull request is ready to be integrated labels Jan 28, 2025
@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Jan 29, 2025
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 29, 2025
@justin-curtis-lu
Copy link
Member Author

Thanks all for the reviews.
/integrate

@openjdk
Copy link

openjdk bot commented Jan 30, 2025

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

  • cc775b1: 8348648: Unnecessary Hashtable usage in javax.swing.text.html.CSS.LengthUnit
  • 06ebb17: 8349002: GenShen: Deadlock during shutdown
  • 1ac2d6e: 8349009: JVM fails to start when AOTClassLinking is used with unverifiable old classes
  • 0cae888: 8349003: NativeCallStack::print_on() output is unreadable
  • fdfb68c: 8344168: Change Unsafe base offset from int to long
  • 5d5b294: 8349070: Fix riscv and ppc build errors caused by JDK-8343767
  • 3f8a875: 8348880: Replace ConcurrentMap with AtomicReferenceArray for ZoneOffset.QUARTER_CACHE
  • fac63d4: 8348668: Prevent first resource cleanup in confined arena from escaping
  • 2efb6aa: 8345314: Add a red–black tree as a utility data structure
  • a937f6d: 8343767: Enumerate StubGen blobs, stubs and entries and generate code from declarations
  • ... and 26 more: https://git.openjdk.org/jdk/compare/3a564ed1019c66c28afa729973948d3a6e6c4c41...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Jan 30, 2025

@justin-curtis-lu Pushed as commit f05c53c.

💡 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
Development

Successfully merging this pull request may close these issues.

5 participants