Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache formatter with offset parsed #120

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,6 +23,8 @@ public class JacksonJodaDateFormat extends JacksonJodaFormatBase

protected final DateTimeFormatter _formatter;

protected final DateTimeFormatter _formatterWithOffsetParsed;

protected final TimeZone _jdkTimezone;

protected transient DateTimeZone _jodaTimezone;
Expand All @@ -47,6 +49,7 @@ public JacksonJodaDateFormat(DateTimeFormatter defaultFormatter)
{
super();
_formatter = defaultFormatter;
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
DateTimeZone tz = defaultFormatter.getZone();
_jdkTimezone = (tz == null) ? null : tz.toTimeZone();
_explicitTimezone = false;
Expand All @@ -59,6 +62,7 @@ public JacksonJodaDateFormat(JacksonJodaDateFormat base,
{
super(base, useTimestamp);
_formatter = base._formatter;
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably could just be base._formatterWithOffsetParsed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for this vanilla copy & paste is just in case _formatter is changed to something else in the future, so that you don't have to also maintain the value at _formatterWithOffsetParsed

The idea is, whatever is assigned to _formatter, create a with offset parsed version of exactly that.
Do let me know if the reason I stated justifies it, if you still don't agree I will change it to what you have suggested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have strong feelings here -- seems like avoiding another operation would be nice, but then again this is not in the hot codepath (as (de)serializer that uses it is cached). So I'll trust your judgment with it.

_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
Expand All @@ -70,6 +74,7 @@ public JacksonJodaDateFormat(JacksonJodaDateFormat base,
{
super(base);
_formatter = formatter;
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
Expand All @@ -80,6 +85,7 @@ public JacksonJodaDateFormat(JacksonJodaDateFormat base, TimeZone jdkTimezone)
{
super(base, jdkTimezone);
_formatter = base._formatter.withZone(DateTimeZone.forTimeZone(jdkTimezone));
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
_jdkTimezone = jdkTimezone;
_explicitTimezone = true;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
Expand All @@ -90,6 +96,7 @@ public JacksonJodaDateFormat(JacksonJodaDateFormat base, Locale locale)
{
super(base, locale);
_formatter = base._formatter.withLocale(locale);
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
Expand All @@ -104,6 +111,7 @@ protected JacksonJodaDateFormat(JacksonJodaDateFormat base,
{
super(base);
_formatter = base._formatter;
_formatterWithOffsetParsed = _formatter.withOffsetParsed();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also just copy?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as my previous comment.

_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = adjustToContextTZOverride;
Expand Down Expand Up @@ -255,20 +263,20 @@ public DateTimeFormatter createFormatterWithLocale(SerializerProvider ctxt)
public DateTimeFormatter createParser(DeserializationContext ctxt)
{
DateTimeFormatter formatter = _formatter;
if (!_explicitLocale) {
Locale loc = ctxt.getLocale();
if (loc != null && !loc.equals(_locale)) {
formatter = formatter.withLocale(loc);
}
}
if (!_explicitTimezone) {
if (shouldAdjustToContextTimeZone(ctxt)) {
TimeZone tz = ctxt.getTimeZone();
if (tz != null && !tz.equals(_jdkTimezone)) {
formatter = formatter.withZone(DateTimeZone.forTimeZone(tz));
}
} else {
formatter = formatter.withOffsetParsed();
formatter = _formatterWithOffsetParsed;
}
}
if (!_explicitLocale) {
Locale loc = ctxt.getLocale();
if (loc != null && !loc.equals(_locale)) {
formatter = formatter.withLocale(loc);
}
}
return formatter;
Expand Down