RFR: 8341670: [Text, TextFlow] Public API for Text Layout Info [v18]
Andy Goryachev
angorya at openjdk.org
Thu May 22 14:43:02 UTC 2025
On Wed, 21 May 2025 23:49:29 GMT, Michael Strauß <mstrauss at openjdk.org> wrote:
>> This method does not throw any exceptions, so none are specified.
>
> Instead of returning `float[]` here, this would be a good candidate to return a discriminated union instead, something like:
>
> sealed interface CaretGeometry {
> record Single(float x, float y, float height) implements CaretGeometry {}
> record Split(float x1, float x2, float y, float height) implements CaretGeometry {}
> }
>
>
> This has two advantages:
> 1. It's more expressive, as you don't need to look up what the array values represent.
> 2. Instead of disciminating between the two cases by inspecting the array length as you do in `PrismLayoutInfo::caretInfoAt`:
>
> ```java
> Rectangle2D[] parts;
> if (c.length == 3) {
> ..
> parts = ...
> } else {
> ...
> parts = ...
> }
> ```
> ...you can use an exhaustive switch expression instead:
> ```
> Rectangle2D[] parts = switch (c) {
> case Single s -> new Rectangle2D[] {
> new Rectangle2D(s.x() + dx, s.y() + dy, 0, s.height())
> };
>
> case Split s -> new Rectangle2D[] {
> new Rectangle2D(...),
> new Rectangle2D(...)
> };
> };
> ```
>
> This is easier to read, and you'll get the compiler checks for free.
I would have agreed with the proposed change, if it were a public API.
This is an internal method, so it was made as low level as possible. The return value is documented, and the consumer is another part of internal code. No need to make it more complicated, in my opinion.
-------------
PR Review Comment: https://git.openjdk.org/jfx/pull/1596#discussion_r2102740296
More information about the openjfx-dev
mailing list