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

Chen Liang liach at openjdk.org
Sat Sep 20 00:39:41 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.

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

> 2479:          * @throws DateTimeException if the date-time cannot be printed successfully
> 2480:          */
> 2481:         boolean format(DateTimePrintContext context, StringBuilder buf, boolean optional);

Please document this new parameter.

src/java.base/share/classes/java/time/format/DateTimePrintContext.java line 1:

> 1: /*

You should update the implSpec tag because this is now thread safe.

src/java.base/share/classes/java/time/format/DateTimePrintContext.java line 113:

> 111:      * @param formatter  the formatter controlling the format, not null
> 112:      */
> 113:     public DateTimePrintContext(TemporalAccessor temporal, DateTimeFormatter formatter) {

Suggestion:

    DateTimePrintContext(TemporalAccessor temporal, DateTimeFormatter formatter) {

Redundant.

src/java.base/share/classes/java/time/format/DateTimePrintContext.java line 353:

> 351:      * @throws DateTimeException if the type is not available and the section is not optional
> 352:      */
> 353:     <R> R getValue(TemporalQuery<R> query, boolean optional) {

Please document the new parameter. Same for getValue(TemporalField, boolean).

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

PR Review Comment: https://git.openjdk.org/jdk/pull/26913#discussion_r2347790245
PR Review Comment: https://git.openjdk.org/jdk/pull/26913#discussion_r2361837731
PR Review Comment: https://git.openjdk.org/jdk/pull/26913#discussion_r2361838040
PR Review Comment: https://git.openjdk.org/jdk/pull/26913#discussion_r2347789766


More information about the core-libs-dev mailing list