<i18n dev> RFR: 8276220: Reduce excessive allocations in DateTimeFormatter [v3]

Claes Redestad redestad at openjdk.java.net
Wed Nov 3 12:47:22 UTC 2021

On Wed, 3 Nov 2021 12:21:00 GMT, Claes Redestad <redestad at openjdk.org> wrote:

>> src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java line 3266:
>>> 3264:             if (!field.range().isValidIntValue(value)) {
>>> 3265:                 if (fallback == null) {
>>> 3266:                     fallback = new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint, subsequentWidth);
>> It would be nice to see a test case cover this.
> I'll see to it.

When adding a test for this I discovered that  `FractionPrinterParser::format` will end up calling `field.range().checkValidValue(value, field)` [here](https://github.com/openjdk/jdk/blob/579b2c017f24f2266abefd35c2b8f28fa7268d93/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java#L3543). This means that the pre-existing implementation does check the value range and throws exceptions when trying to print a `value` outside of the `field` range. 

To mimic the existing behavior we have to do the same check in `NanosPrinterParser::format` and drop the fallback (which would have somewhat nonsensical output for values outside the range, anyhow).


PR: https://git.openjdk.java.net/jdk/pull/6188

More information about the i18n-dev mailing list