RFR: 8368172: Make java.time.format.DateTimePrintContext immutable

Stephen Colebourne scolebourne at openjdk.org
Sat Sep 20 11:08:19 UTC 2025


On Sat, 23 Aug 2025 11:27:55 GMT, Shaojin Wen <swen at openjdk.org> wrote:

> I propose to make j.t.f.DateTimePrintContext immutable.
> 
> Currently, DateTimePrintContext has only one mutable field, optional. This can be replaced by adding an optional parameter to the DateTimeFormatter.formatTo method.
> 
> Immutable DateTimePrintContext can be optimized by escape analysis, such as immutable object optimization.

AFAICT this seems like a good change, thanks

src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java line 2537:

> 2535:             optional |= this.optional;
> 2536:             for (DateTimePrinterParser pp : printerParsers) {
> 2537:                 if (pp.format(context, buf, optional) == false) {

Suggestion:

            boolean effectiveOptional = optional | this.optional;
            for (DateTimePrinterParser pp : printerParsers) {
                if (pp.format(context, buf, effectiveOptional) == false) {

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

PR Review: https://git.openjdk.org/jdk/pull/26913#pullrequestreview-3248990792
PR Review Comment: https://git.openjdk.org/jdk/pull/26913#discussion_r2365585860


More information about the core-libs-dev mailing list