Skip to content

Commit d8742d7

Browse files
committed
use EnumMap
1 parent b649557 commit d8742d7

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

src/java.base/share/classes/java/time/format/DateTimeFormatter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ public final class DateTimeFormatter {
544544
*/
545545
private final ZoneId zone;
546546

547+
/**
548+
* Flag indicating whether this formatter only uses ChronoField instances.
549+
* This is used to optimize the storage of parsed field values in the Parsed class.
550+
*/
551+
final boolean onlyChronoField;
552+
547553
//-----------------------------------------------------------------------
548554
/**
549555
* Creates a formatter using the specified pattern.
@@ -1486,6 +1492,7 @@ public static final TemporalQuery<Boolean> parsedLeapSecond() {
14861492
this.resolverStyle = Objects.requireNonNull(resolverStyle, "resolverStyle");
14871493
this.chrono = chrono;
14881494
this.zone = zone;
1495+
this.onlyChronoField = printerParser.onlyChronoField();
14891496
}
14901497

14911498
//-----------------------------------------------------------------------

src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,23 @@ public int parse(DateTimeParseContext context, CharSequence text, int position)
25802580
}
25812581
}
25822582

2583+
/**
2584+
* Checks whether this composite printer parser only uses ChronoField instances.
2585+
* This is used to optimize the storage of parsed field values in the Parsed class.
2586+
*
2587+
* @return true if all printer parsers in this composite only use ChronoField instances,
2588+
* false otherwise
2589+
*/
2590+
boolean onlyChronoField() {
2591+
for (DateTimePrinterParser pp : printerParsers) {
2592+
if ((pp instanceof NumberPrinterParser npp && !(npp.field instanceof ChronoField))
2593+
|| (pp instanceof CompositePrinterParser cpp && !cpp.onlyChronoField())) {
2594+
return false;
2595+
}
2596+
}
2597+
return true;
2598+
}
2599+
25832600
@Override
25842601
public String toString() {
25852602
StringBuilder buf = new StringBuilder();

src/java.base/share/classes/java/time/format/DateTimeParseContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ final class DateTimeParseContext {
120120
DateTimeParseContext(DateTimeFormatter formatter) {
121121
super();
122122
this.formatter = formatter;
123-
parsed.add(new Parsed());
123+
parsed.add(new Parsed(formatter.onlyChronoField));
124124
}
125125

126126
/**

src/java.base/share/classes/java/time/format/Parsed.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
import java.time.temporal.TemporalQueries;
9999
import java.time.temporal.TemporalQuery;
100100
import java.time.temporal.UnsupportedTemporalTypeException;
101+
import java.util.EnumMap;
101102
import java.util.HashMap;
102103
import java.util.Iterator;
103104
import java.util.Map;
@@ -125,10 +126,21 @@
125126
final class Parsed implements TemporalAccessor {
126127
// some fields are accessed using package scope from DateTimeParseContext
127128

129+
final boolean onlyChronoField;
130+
@SuppressWarnings("unchecked")
131+
private Map<TemporalField, Long> createFieldValuesMap() {
132+
if (onlyChronoField) {
133+
return new HashMap<>();
134+
} else {
135+
// Create the EnumMap with raw types and cast it appropriately
136+
// This is safe because ChronoField implements TemporalField
137+
return (Map<TemporalField, Long>) (Map) new EnumMap<>(ChronoField.class);
138+
}
139+
}
128140
/**
129141
* The parsed fields.
130142
*/
131-
final Map<TemporalField, Long> fieldValues = new HashMap<>();
143+
final Map<TemporalField, Long> fieldValues = createFieldValuesMap();
132144
/**
133145
* The parsed zone.
134146
*/
@@ -169,15 +181,16 @@ final class Parsed implements TemporalAccessor {
169181
/**
170182
* Creates an instance.
171183
*/
172-
Parsed() {
184+
Parsed(boolean onlyChronoField) {
185+
this.onlyChronoField = onlyChronoField;
173186
}
174187

175188
/**
176189
* Creates a copy.
177190
*/
178191
Parsed copy() {
179192
// only copy fields used in parsing stage
180-
Parsed cloned = new Parsed();
193+
Parsed cloned = new Parsed(onlyChronoField);
181194
cloned.fieldValues.putAll(this.fieldValues);
182195
cloned.zone = this.zone;
183196
cloned.zoneNameType = this.zoneNameType;

0 commit comments

Comments
 (0)