<i18n dev> RFR: 8311188: Simplify and modernize equals and hashCode in java.text

Roger Riggs rriggs at openjdk.org
Wed Jul 5 15:57:57 UTC 2023


On Mon, 3 Jul 2023 11:12:32 GMT, Pavel Rappo <prappo at openjdk.org> wrote:

> Please review this PR to use modern APIs and language features to simplify `equals` and `hashCode` in the java.text area.
> 
> * Some changes to `equals` and `hashCode` are refactoring rather than modernization. Such changes can be as trivial as rearranging, adding, or commenting checks.
> 
> * java.text area contains more classes whose `equals` and `hashCode` could be simplified; for example: sun.text.CompactByteArray or java.text.DigitList. However, I found no evidence of `equals` and `hashCode` in those classes being used in source or tests. Since writing new tests in this case seems unreasonable, I **excluded** those classes from this PR.

Marked as reviewed by rriggs (Reviewer).

src/java.base/share/classes/java/text/CompactNumberFormat.java line 2364:

> 2362:             return true;
> 2363:         }
> 2364: 

Moving this before the `super.equals` call  performance-wise would favor the equals case. (statistics not available guess)

The code style in these files favors `if (this == obj)`... over the swapped order of arguments.

src/java.base/share/classes/java/text/DecimalFormat.java line 2926:

> 2924:         if (obj == this) { // disambiguate super equality from ref equality
> 2925:             return true;
> 2926:         }

Ditto comment in CompactNumberFormat.

src/java.base/share/classes/java/text/DecimalFormat.java line 2930:

> 2928:         DecimalFormat other = (DecimalFormat) obj;
> 2929:         return ((posPrefixPattern == other.posPrefixPattern &&
> 2930:                  positivePrefix.equals(other.positivePrefix))

This might also be:

return (obj instanceof DecimalFormat other) && 
                 posPrefixPattern == other.posPrefixPattern &&
                 positivePrefix.equals(other.positivePrefix));

-------------

PR Review: https://git.openjdk.org/jdk/pull/14752#pullrequestreview-1514810525
PR Review Comment: https://git.openjdk.org/jdk/pull/14752#discussion_r1253309018
PR Review Comment: https://git.openjdk.org/jdk/pull/14752#discussion_r1253310120
PR Review Comment: https://git.openjdk.org/jdk/pull/14752#discussion_r1253317064


More information about the i18n-dev mailing list