Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/Locale.java
Original file line number Diff line number Diff line change
Expand Up @@ -3209,12 +3209,12 @@ public LanguageRange(String range) {
* {@code null}
* @throws IllegalArgumentException if the given {@code range} does not
* comply with the syntax of the language range mentioned in RFC 4647
* or if the given {@code weight} is less than {@code MIN_WEIGHT}
* or if the given {@code weight} is {@code Double.NaN}, less than {@code MIN_WEIGHT}
* or greater than {@code MAX_WEIGHT}
*/
public LanguageRange(String range, double weight) {
Objects.requireNonNull(range);
if (weight < MIN_WEIGHT || weight > MAX_WEIGHT) {
if (weight < MIN_WEIGHT || weight > MAX_WEIGHT || Double.isNaN(weight)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since the constructor now throws IllegalArgumentException for NaN weights, this should also be specified in the @throws tag, as NaN does not satisfy either NaN < MIN_WEIGHT or NaN > MAX_WEIGHT.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How about updating the @throws wording to be

... or if the given {@code weight} is not between {@code MIN_WEIGHT} and {@code MAX_WEIGHT}, inclusive.

which would cover the NaN case without having to explicitly call it out.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That wording is technically fine, but I would still prefer to call out NaN explicitly. NaN is unordered, so it is a different case from an ordinary out-of-range value, and it is the specific edge case being fixed here.

How about:

... or if the given {@code weight} is {@code NaN}, less than {@code MIN_WEIGHT}, or greater than {@code MAX_WEIGHT}.

throw new IllegalArgumentException("weight=" + weight);
}

Expand Down
4 changes: 3 additions & 1 deletion test/jdk/java/util/Locale/LocaleMatchingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 7069824 8042360 8032842 8175539 8210443 8242010 8276302 8381644
* @bug 7069824 8042360 8032842 8175539 8210443 8242010 8276302 8381644 8387261
* @summary Verify implementation for Locale matching.
* @run junit/othervm LocaleMatchingTest
*/
Expand Down Expand Up @@ -93,6 +93,7 @@ static Object[][] LRConstructorIAEData() {
{"1996-de-Latn", MAX_WEIGHT},
// Testcase for 8042360
{"en-Latn-1234567890", MAX_WEIGHT},
{"en", Double.NaN},
};
}

Expand Down Expand Up @@ -146,6 +147,7 @@ static Object[][] LRParseIAEData() {
// Ranges
{""},
{"ja;q=3"},
{"en;q=NaN"}
};
}

Expand Down