Skip to content

Commit

Permalink
Add cache for datetimeformatters
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 committed Dec 4, 2023
1 parent 05422bf commit 6c2bc67
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalQuery;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -82,6 +84,8 @@ public class TimeConverterRegistrar implements TypeConverterRegistrar {
private static final Pattern DURATION_MATCHER = Pattern.compile("^(-?\\d+)([unsmhd])(s?)$");
private static final int MILLIS = 3;

private final Map<String, DateTimeFormatter> formattersCache = new ConcurrentHashMap<>();

@NextMajorVersion("Consider deletion of LocalDate and LocalDateTime converters")
@Override
public void register(MutableConversionService conversionService) {
Expand Down Expand Up @@ -184,7 +188,7 @@ private <T extends TemporalAccessor> void addTemporalStringConverters(MutableCon
// try explicit format first
Optional<String> format = context.getAnnotationMetadata().stringValue(Format.class);
if (format.isPresent()) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format.get(), context.getLocale());
DateTimeFormatter formatter = getFormatter(format.get(), context);
try {
T converted = formatter.parse(object, query);
return Optional.of(converted);
Expand Down Expand Up @@ -216,7 +220,7 @@ private <T extends TemporalAccessor> void addTemporalStringConverters(MutableCon
// try explicit format first
Optional<String> format = context.getAnnotationMetadata().stringValue(Format.class);
if (format.isPresent()) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format.get(), context.getLocale());
DateTimeFormatter formatter = getFormatter(format.get(), context);
try {
CharSequence converted = formatter.format(object);
return Optional.of(converted);
Expand All @@ -242,6 +246,19 @@ private <T extends TemporalAccessor> void addTemporalStringConverters(MutableCon
});
}

private DateTimeFormatter getFormatter(String pattern, ConversionContext context) {
var key = pattern + '#' + context.getLocale();
DateTimeFormatter formatter;
var cachedFormater = formattersCache.get(key);
if (cachedFormater != null) {
return cachedFormater;
}
formatter = DateTimeFormatter.ofPattern(pattern, context.getLocale());
formattersCache.put(key, formatter);

return formatter;
}

private <T extends TemporalAccessor> void addTemporalToDateConverter(MutableConversionService conversionService, Class<T> temporalType, Function<T, Instant> toInstant) {
conversionService.addConverter(temporalType, Date.class, (T object, Class<Date> targetType, ConversionContext context) -> Optional.of(Date.from(toInstant.apply(object))));
}
Expand Down

0 comments on commit 6c2bc67

Please sign in to comment.