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

8314169: Combine related RoundingMode logic in j.text.DigitList #15252

Conversation

justin-curtis-lu
Copy link
Member

@justin-curtis-lu justin-curtis-lu commented Aug 11, 2023

Please review this PR which is a broad clean up of the DigitList class (used by Format classes in j.text).

This PR is intended to be a portion of a bigger change (split up to make reviewing easier).

The main change simplifying related Rounding Mode logic in shouldRoundUp() - (CEILING/FLOOR, HALF_UP/DOWN/EVEN)

Other changes include

  • Certain for loops can be replaced with cleaner syntax (E.g. for(;;), empty for loops)
  • Introduce roundInt(int) - For use by Integer representations of DigitList
  • Introduce nonZeroAfterIndex(int) - To reduce code duplication

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-8314169: Combine related RoundingMode logic in j.text.DigitList (Sub-task - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15252

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 11, 2023

👋 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 openjdk bot added the rfr Pull request is ready for review label Aug 11, 2023
@openjdk
Copy link

openjdk bot commented Aug 11, 2023

@justin-curtis-lu The following label will be automatically applied to this pull request:

  • core-libs

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

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Aug 11, 2023
@mlbridge
Copy link

mlbridge bot commented Aug 11, 2023

Webrevs

* Return true if there exists a non-zero digit in the digit list
* from the given index until the end.
*/
private boolean non0AfterIndex(int index) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer spelling out 0 to Zero.

Comment on lines 404 to 409
* This method is reserved for Long and BigInteger representations.
* @param maximumDigits The maximum number of digits to be shown.
*
* Upon return, count will be less than or equal to maximumDigits.
*/
private void round(int maximumDigits) {
Copy link
Member

Choose a reason for hiding this comment

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

If the method is only used for Long and BigInteger, probably use the specific name instead of explaining it in the description and overloading would be more clear to me.

Comment on lines 519 to 521
if (non0AfterIndex(maximumDigits)) {
return (isNegative && roundingMode == RoundingMode.FLOOR)
|| (!isNegative && roundingMode == RoundingMode.CEILING);
Copy link
Member

Choose a reason for hiding this comment

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

roundingMode is checked against FLOOR/CEILING twice. I don't see the need of bundling them up.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the review. Addressed this, and your other comments that I did not explicitly respond to.

Copy link
Member

Choose a reason for hiding this comment

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

I'd think this separate one-liner for each case would be simpler and more readable:

    case CEILING:
        return nonZeroAfterIndex(maximumDigits) && !isNegative;
    case FLOOR:
        return nonZeroAfterIndex(maximumDigits) && isNegative;

// Digit at rounding position is a '5'. Tie cases.
if (maximumDigits != (count - 1)) {
// There are remaining digits. Above tie => must round up
case HALF_EVEN:
Copy link
Member

Choose a reason for hiding this comment

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

Fix the indentation

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 for catching (copy paste error)

Comment on lines 535 to 543
if (alreadyRounded) {
return false;
// When exact, consider specific contract logic
} else if (valueExactAsDecimal) {
return (roundingMode == RoundingMode.HALF_UP) ||
(roundingMode == RoundingMode.HALF_EVEN
&& (maximumDigits > 0) && (digits[maximumDigits - 1] % 2 != 0));
// Not already rounded, and not exact, round up
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure this logic is equivalent to the previous one? Previously, HALF_UP/DOWN checks valueExactAsDecimal before alreadyRounded, but the new one checks alreadyRounded first.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, since alreadyRounded and valueExactAsDecimal are never both true and when either of them are true, it warrants a return without considering other logic.

However, I have adjusted the code so that this is more apparent (and appears more similar to the original HALF_DOWN/UP, which was written more concisely).

Copy link
Member

Choose a reason for hiding this comment

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

OK, thanks for confirming

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.

LGTM

@openjdk
Copy link

openjdk bot commented Aug 15, 2023

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

8314169: Combine related RoundingMode logic in j.text.DigitList

Reviewed-by: naoto

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

  • f239954: 8310134: NMT: thread count in Thread section of VM.native_memory output confusing with virtual threads
  • 2e8a0ab: 8314120: Add tests for FileDescriptor.sync
  • 80809ef: 8314248: Remove HotSpotConstantPool::isResolvedDynamicInvoke
  • 004651d: 8311557: [JVMCI] deadlock with JVMTI thread suspension
  • 9ded868: 8314114: Fix -Wconversion warnings in os code, primarily linux
  • a02d65e: 8310308: IR Framework: check for type and size of vector nodes
  • dff99f7: 8313782: Add user-facing warning if THPs are enabled but cannot be used
  • f4e72c5: 8313949: Missing word in GPLv2 license text in StackMapTableAttribute.java
  • 6338927: 8314197: AttachListener::pd_find_operation always returning nullptr
  • b7dee21: 8314244: Incorrect file headers in new tests from JDK-8312597
  • ... and 27 more: https://git.openjdk.org/jdk/compare/12326770dc4116dd3b374c3a50fabfa1f27249dd...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 the ready Pull request is ready to be integrated label Aug 15, 2023
@justin-curtis-lu
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented Aug 17, 2023

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

  • 808bb1f: 8314246: javax/swing/JToolBar/4529206/bug4529206.java fails intermittently on Linux
  • 6445314: 8314477: Improve definition of "prototypical type"
  • d27daf0: 8314129: Make fields final in java.util.Scanner
  • a8ab3be: 8314261: Make fields final in sun.net.www
  • 3bb8afb: 8314489: Add javadoc index entries for java.lang.Math terms
  • 2505ceb: 8314533: ProblemList runtime/cds/appcds/customLoader/HelloCustom_JFR.java on linux-all with ZGC
  • b33ff30: 8313661: [REDO] Relax prerequisites for java.base-jmod target
  • 62ca001: 8313357: Revisit requiring SA tests on OSX to either run as root or use sudo
  • 388dcff: 8282712: VMConnection.open() does not detect if VM failed to be created, resulting in NPE
  • e8f6b3e: 8314268: Missing include in assembler_riscv.hpp
  • ... and 68 more: https://git.openjdk.org/jdk/compare/12326770dc4116dd3b374c3a50fabfa1f27249dd...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Aug 17, 2023

@justin-curtis-lu Pushed as commit 96778dd.

💡 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 integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

2 participants