From 1922f549bb3f1721cfa5f8535a86128f62c4beee Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Fri, 9 Oct 2020 12:06:47 -0400 Subject: [PATCH] Improve multithreaded performance of datetime types --- .../fhir/dstu2/model/BaseDateTimeType.java | 77 ++++++++++--------- .../dstu2016may/model/BaseDateTimeType.java | 70 +++++++++-------- .../fhir/dstu3/model/BaseDateTimeType.java | 70 +++++++++-------- .../hl7/fhir/r4/model/BaseDateTimeType.java | 72 +++++++++-------- .../hl7/fhir/r5/model/BaseDateTimeType.java | 70 +++++++++-------- 5 files changed, 199 insertions(+), 160 deletions(-) diff --git a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/model/BaseDateTimeType.java b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/model/BaseDateTimeType.java index e675afec6..5118146f2 100644 --- a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/model/BaseDateTimeType.java +++ b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/model/BaseDateTimeType.java @@ -1,33 +1,33 @@ package org.hl7.fhir.dstu2.model; -/* - Copyright (c) 2011+, HL7, Inc. - All rights reserved. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - * Neither the name of HL7 nor the names of its contributors may be used to - endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. - - */ +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + */ import ca.uhn.fhir.model.api.TemporalPrecisionEnum; @@ -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 { private static final FastDateFormat ourHumanDateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM); private static final FastDateFormat ourHumanDateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM); + private static final Map timezoneCache = new ConcurrentHashMap<>(); static { ArrayList formatters = new ArrayList(); @@ -159,7 +161,7 @@ public abstract class BaseDateTimeType extends PrimitiveType { 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 { } 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 { } 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 { 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 { 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 { } } + private TimeZone getTimeZone(String offset) { + return timezoneCache.computeIfAbsent(offset, (offsetLocal) -> + TimeZone.getTimeZone("GMT" + offsetLocal)); + } + } \ No newline at end of file diff --git a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/model/BaseDateTimeType.java b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/model/BaseDateTimeType.java index 2aae228ea..fd285274a 100644 --- a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/model/BaseDateTimeType.java +++ b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/model/BaseDateTimeType.java @@ -1,33 +1,33 @@ package org.hl7.fhir.dstu2016may.model; -/* - Copyright (c) 2011+, HL7, Inc. - All rights reserved. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - * Neither the name of HL7 nor the names of its contributors may be used to - endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. - - */ +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + */ import static org.apache.commons.lang3.StringUtils.isBlank; @@ -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 { static final long NANOS_PER_MILLIS = 1000000L; static final long NANOS_PER_SECOND = 1000000000L; + private static final Map 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 { } 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 { */ public TimeZone getTimeZone() { if (myTimeZoneZulu) { - return TimeZone.getTimeZone("GMT"); + return getTimeZone("GMT"); } return myTimeZone; } @@ -414,7 +417,7 @@ public abstract class BaseDateTimeType extends PrimitiveType { 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 { } } + private TimeZone getTimeZone(String offset) { + return timezoneCache.computeIfAbsent(offset, (offsetLocal) -> + TimeZone.getTimeZone("GMT" + offsetLocal)); + } + } \ No newline at end of file diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/BaseDateTimeType.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/BaseDateTimeType.java index 7456fcb6a..53dddf5a8 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/BaseDateTimeType.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/BaseDateTimeType.java @@ -1,33 +1,33 @@ package org.hl7.fhir.dstu3.model; -/* - Copyright (c) 2011+, HL7, Inc. - All rights reserved. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - * Neither the name of HL7 nor the names of its contributors may be used to - endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. - - */ +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + */ import static org.apache.commons.lang3.StringUtils.isBlank; @@ -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 { static final long NANOS_PER_MILLIS = 1000000L; static final long NANOS_PER_SECOND = 1000000000L; + private static final Map 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 { } 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 { */ public TimeZone getTimeZone() { if (myTimeZoneZulu) { - return TimeZone.getTimeZone("GMT"); + return getTimeZone("GMT"); } return myTimeZone; } @@ -646,7 +649,7 @@ public abstract class BaseDateTimeType extends PrimitiveType { 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 { } } + private TimeZone getTimeZone(String offset) { + return timezoneCache.computeIfAbsent(offset, (offsetLocal) -> + TimeZone.getTimeZone("GMT" + offsetLocal)); + } + } \ No newline at end of file diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/BaseDateTimeType.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/BaseDateTimeType.java index 699db4c0e..d01b27736 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/BaseDateTimeType.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/BaseDateTimeType.java @@ -1,33 +1,33 @@ package org.hl7.fhir.r4.model; -/* - Copyright (c) 2011+, HL7, Inc. - All rights reserved. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - * Neither the name of HL7 nor the names of its contributors may be used to - endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. - - */ +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + */ import static org.apache.commons.lang3.StringUtils.isBlank; @@ -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 { static final long NANOS_PER_MILLIS = 1000000L; static final long NANOS_PER_SECOND = 1000000000L; + private static final Map 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 { } 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 { */ public TimeZone getTimeZone() { if (myTimeZoneZulu) { - return TimeZone.getTimeZone("GMT"); + return getTimeZone("GMT"); } return myTimeZone; } @@ -645,7 +648,7 @@ public abstract class BaseDateTimeType extends PrimitiveType { 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; @@ -943,5 +946,10 @@ public abstract class BaseDateTimeType extends PrimitiveType { public String fpValue() { return "@"+primitiveValue(); } - + + private TimeZone getTimeZone(String offset) { + return timezoneCache.computeIfAbsent(offset, (offsetLocal) -> + TimeZone.getTimeZone("GMT" + offsetLocal)); + } + } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BaseDateTimeType.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BaseDateTimeType.java index b9dcb4ace..769ca44fe 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BaseDateTimeType.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BaseDateTimeType.java @@ -1,33 +1,33 @@ package org.hl7.fhir.r5.model; -/* - Copyright (c) 2011+, HL7, Inc. - All rights reserved. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - * Neither the name of HL7 nor the names of its contributors may be used to - endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. - - */ +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + */ import ca.uhn.fhir.model.api.TemporalPrecisionEnum; @@ -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 { static final long NANOS_PER_MILLIS = 1000000L; static final long NANOS_PER_SECOND = 1000000000L; + private static final Map 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 { } 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 { */ public TimeZone getTimeZone() { if (myTimeZoneZulu) { - return TimeZone.getTimeZone("GMT"); + return getTimeZone("GMT"); } return myTimeZone; } @@ -652,7 +655,7 @@ public abstract class BaseDateTimeType extends PrimitiveType { 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 { return "@"+primitiveValue(); } + private TimeZone getTimeZone(String offset) { + return timezoneCache.computeIfAbsent(offset, (offsetLocal) -> + TimeZone.getTimeZone("GMT" + offsetLocal)); + } + } \ No newline at end of file