Improve multithreaded performance of datetime types

This commit is contained in:
jamesagnew 2020-10-09 12:06:47 -04:00 committed by James Agnew
parent 36fa3a97af
commit 1922f549bb
5 changed files with 199 additions and 160 deletions

View File

@ -39,6 +39,7 @@ import org.hl7.fhir.utilities.DateTimeUtil;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import static ca.uhn.fhir.model.api.TemporalPrecisionEnum.*;
@ -74,6 +75,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
private static final FastDateFormat ourHumanDateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM);
private static final FastDateFormat ourHumanDateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM);
private static final Map<String, TimeZone> timezoneCache = new ConcurrentHashMap<>();
static {
ArrayList<FastDateFormat> formatters = new ArrayList<FastDateFormat>();
@ -159,7 +161,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
return ourYearFormat.format(theValue);
case MINUTE:
if (myTimeZoneZulu) {
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
GregorianCalendar cal = new GregorianCalendar(getTimeZone("GMT"));
cal.setTime(theValue);
return ourYearMonthDayTimeMinsFormat.format(cal) + "Z";
} else if (myTimeZone != null) {
@ -171,7 +173,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
}
case SECOND:
if (myTimeZoneZulu) {
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
GregorianCalendar cal = new GregorianCalendar(getTimeZone("GMT"));
cal.setTime(theValue);
return ourYearMonthDayTimeFormat.format(cal) + "Z";
} else if (myTimeZone != null) {
@ -183,7 +185,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
}
case MILLI:
if (myTimeZoneZulu) {
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
GregorianCalendar cal = new GregorianCalendar(getTimeZone("GMT"));
cal.setTime(theValue);
return ourYearMonthDayTimeMilliFormat.format(cal) + "Z";
} else if (myTimeZone != null) {
@ -410,9 +412,9 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
if (theValueString.endsWith("Z")) {
setTimeZoneZulu(true);
} else if (theValueString.indexOf("GMT", timeZoneStart) != -1) {
setTimeZone(TimeZone.getTimeZone(theValueString.substring(timeZoneStart)));
setTimeZone(getTimeZone(theValueString.substring(timeZoneStart)));
} else if (theValueString.indexOf('+', timeZoneStart) != -1 || theValueString.indexOf('-', timeZoneStart) != -1) {
setTimeZone(TimeZone.getTimeZone("GMT" + theValueString.substring(timeZoneStart)));
setTimeZone(getTimeZone("GMT" + theValueString.substring(timeZoneStart)));
}
}
@ -528,9 +530,9 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
int hours = offsetAbs / 60;
if (theZoneOffsetMinutes < 0) {
setTimeZone(TimeZone.getTimeZone("GMT-" + hours + ":" + mins));
setTimeZone(getTimeZone("GMT-" + hours + ":" + mins));
} else {
setTimeZone(TimeZone.getTimeZone("GMT+" + hours + ":" + mins));
setTimeZone(getTimeZone("GMT+" + hours + ":" + mins));
}
}
@ -618,4 +620,9 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
}
}
private TimeZone getTimeZone(String offset) {
return timezoneCache.computeIfAbsent(offset, (offsetLocal) ->
TimeZone.getTimeZone("GMT" + offsetLocal));
}
}

View File

@ -35,7 +35,9 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import org.apache.commons.lang3.StringUtils;
@ -52,6 +54,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
static final long NANOS_PER_MILLIS = 1000000L;
static final long NANOS_PER_SECOND = 1000000000L;
private static final Map<String, TimeZone> timezoneCache = new ConcurrentHashMap<>();
private static final FastDateFormat ourHumanDateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM);
private static final FastDateFormat ourHumanDateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM);
@ -114,7 +117,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
} else {
GregorianCalendar cal;
if (myTimeZoneZulu) {
cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal = new GregorianCalendar(getTimeZone("GMT"));
} else if (myTimeZone != null) {
cal = new GregorianCalendar(myTimeZone);
} else {
@ -209,7 +212,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
*/
public TimeZone getTimeZone() {
if (myTimeZoneZulu) {
return TimeZone.getTimeZone("GMT");
return getTimeZone("GMT");
}
return myTimeZone;
}
@ -414,7 +417,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
parseInt(theWholeValue, theValue.substring(1, 3), 0, 23);
parseInt(theWholeValue, theValue.substring(4, 6), 0, 59);
myTimeZoneZulu = false;
myTimeZone = TimeZone.getTimeZone("GMT" + theValue);
myTimeZone = getTimeZone("GMT" + theValue);
}
return this;
@ -750,4 +753,9 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
}
}
private TimeZone getTimeZone(String offset) {
return timezoneCache.computeIfAbsent(offset, (offsetLocal) ->
TimeZone.getTimeZone("GMT" + offsetLocal));
}
}

View File

@ -35,7 +35,9 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
@ -51,6 +53,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
static final long NANOS_PER_MILLIS = 1000000L;
static final long NANOS_PER_SECOND = 1000000000L;
private static final Map<String, TimeZone> timezoneCache = new ConcurrentHashMap<>();
private static final FastDateFormat ourHumanDateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM);
private static final FastDateFormat ourHumanDateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM);
@ -171,7 +174,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
} else {
GregorianCalendar cal;
if (myTimeZoneZulu) {
cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal = new GregorianCalendar(getTimeZone("GMT"));
} else if (myTimeZone != null) {
cal = new GregorianCalendar(myTimeZone);
} else {
@ -336,7 +339,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
*/
public TimeZone getTimeZone() {
if (myTimeZoneZulu) {
return TimeZone.getTimeZone("GMT");
return getTimeZone("GMT");
}
return myTimeZone;
}
@ -646,7 +649,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
parseInt(theWholeValue, theValue.substring(1, 3), 0, 23);
parseInt(theWholeValue, theValue.substring(4, 6), 0, 59);
myTimeZoneZulu = false;
myTimeZone = TimeZone.getTimeZone("GMT" + theValue);
myTimeZone = getTimeZone("GMT" + theValue);
}
return this;
@ -841,4 +844,9 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
}
}
private TimeZone getTimeZone(String offset) {
return timezoneCache.computeIfAbsent(offset, (offsetLocal) ->
TimeZone.getTimeZone("GMT" + offsetLocal));
}
}

View File

@ -35,7 +35,9 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import org.apache.commons.lang3.StringUtils;
@ -51,6 +53,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
static final long NANOS_PER_MILLIS = 1000000L;
static final long NANOS_PER_SECOND = 1000000000L;
private static final Map<String, TimeZone> timezoneCache = new ConcurrentHashMap<>();
private static final FastDateFormat ourHumanDateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM);
private static final FastDateFormat ourHumanDateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM);
@ -174,7 +177,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
} else {
GregorianCalendar cal;
if (myTimeZoneZulu) {
cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal = new GregorianCalendar(getTimeZone("GMT"));
} else if (myTimeZone != null) {
cal = new GregorianCalendar(myTimeZone);
} else {
@ -339,7 +342,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
*/
public TimeZone getTimeZone() {
if (myTimeZoneZulu) {
return TimeZone.getTimeZone("GMT");
return getTimeZone("GMT");
}
return myTimeZone;
}
@ -645,7 +648,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
parseInt(theWholeValue, theValue.substring(1, 3), 0, 23);
parseInt(theWholeValue, theValue.substring(4, 6), 0, 59);
myTimeZoneZulu = false;
myTimeZone = TimeZone.getTimeZone("GMT" + theValue);
myTimeZone = getTimeZone("GMT" + theValue);
}
return this;
@ -944,4 +947,9 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
return "@"+primitiveValue();
}
private TimeZone getTimeZone(String offset) {
return timezoneCache.computeIfAbsent(offset, (offsetLocal) ->
TimeZone.getTimeZone("GMT" + offsetLocal));
}
}

View File

@ -42,7 +42,9 @@ import org.hl7.fhir.utilities.Utilities;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import static org.apache.commons.lang3.StringUtils.isBlank;
@ -51,6 +53,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
static final long NANOS_PER_MILLIS = 1000000L;
static final long NANOS_PER_SECOND = 1000000000L;
private static final Map<String, TimeZone> timezoneCache = new ConcurrentHashMap<>();
private static final FastDateFormat ourHumanDateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM);
private static final FastDateFormat ourHumanDateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM);
@ -174,7 +177,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
} else {
GregorianCalendar cal;
if (myTimeZoneZulu) {
cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal = new GregorianCalendar(getTimeZone("GMT"));
} else if (myTimeZone != null) {
cal = new GregorianCalendar(myTimeZone);
} else {
@ -346,7 +349,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
*/
public TimeZone getTimeZone() {
if (myTimeZoneZulu) {
return TimeZone.getTimeZone("GMT");
return getTimeZone("GMT");
}
return myTimeZone;
}
@ -652,7 +655,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
parseInt(theWholeValue, theValue.substring(1, 3), 0, 23);
parseInt(theWholeValue, theValue.substring(4, 6), 0, 59);
myTimeZoneZulu = false;
myTimeZone = TimeZone.getTimeZone("GMT" + theValue);
myTimeZone = getTimeZone("GMT" + theValue);
}
return this;
@ -1011,4 +1014,9 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
return "@"+primitiveValue();
}
private TimeZone getTimeZone(String offset) {
return timezoneCache.computeIfAbsent(offset, (offsetLocal) ->
TimeZone.getTimeZone("GMT" + offsetLocal));
}
}