On Mon, 2 Mar 2026 16:04:12 GMT, Michael Strauß <[email protected]> wrote:

> `CssParser.colorValueOfString()` parses a string into a `Color`. If this 
> fails by throwing an exception, `null` is returned from 
> `colorValueOfString()`, signalling to the caller that the color string might 
> be a lookup instead.
> 
> Since color lookups can appear in many places in a CSS file, it is preferable 
> to use return values for flow control instead of exceptions. For this 
> purpose, we need non-throwing versions of methods that parse numbers and 
> colors.
> 
> These are the changes in this PR:
> 1. Move color parsing from the `Color` class to `CssColorParser`. This is 
> done so that we can access the new `tryParseColor(String)` method from 
> `CssParser`, and also to separate concerns between color representation and 
> parsing.
> 2. Add a non-throwing `CssNumberParser.tryParseDouble()`, which returns `NaN` 
> to indicate a parsing failure. We can use `NaN` because it is not a valid 
> return value of the parser.
> 3. Since color parsing also uses `Integer.parseInt()`, we need a non-throwing 
> version of this method: `CssNumberParser.tryParseInt()`. Since we can't use 
> any particular `int` as a return value that indicates failure, I've decided 
> to return a `long` value instead, where a value less than `Integer.MIN_VALUE` 
> indicates a parsing failure.

Note to reviewers: many changes in this PR are just whitespace changes. You can 
hide those by going to the "Files changed" tab in GitHub, clicking on the gear 
icon, and selecting "Hide whitespace".

modules/javafx.graphics/src/main/java/com/sun/javafx/css/parser/CssNumberParser.java
 line 42:

> 40:      * returns a long value less than {@link Integer#MIN_VALUE} to 
> indicate failure.
> 41:      */
> 42:     public static long tryParseInt(String s, int start, int end, int 
> radix) {

This code is taken from `Integer.parseInt(String, int, int, int)` in the JDK, 
and modified to return `Long.MIN_VALUE` instead of throwing 
`NumberFormatException`.

modules/javafx.graphics/src/test/java/test/com/sun/javafx/css/parser/CssNumberParserTest.java
 line 42:

> 40: 
> 41:     @Nested
> 42:     class ParseIntegerTest {

These tests are taken from `Integer/ParsingTest` in the JDK.

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

PR Review: https://git.openjdk.org/jfx/pull/2093#pullrequestreview-3877151077
PR Review Comment: https://git.openjdk.org/jfx/pull/2093#discussion_r2873294994
PR Review Comment: https://git.openjdk.org/jfx/pull/2093#discussion_r2873302179

Reply via email to