From 7a339c37473e76a400459de1260e5a9aa3857b15 Mon Sep 17 00:00:00 2001 From: heromyth Date: Wed, 1 Jul 2020 11:24:53 +0800 Subject: [PATCH] Build failed in release mode on Windows --- .../hunt/time/format/LocalizedPrinterParser.d | 89 +++++++++---------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/source/hunt/time/format/LocalizedPrinterParser.d b/source/hunt/time/format/LocalizedPrinterParser.d index 56e71b3..b1b1143 100644 --- a/source/hunt/time/format/LocalizedPrinterParser.d +++ b/source/hunt/time/format/LocalizedPrinterParser.d @@ -3,6 +3,7 @@ module hunt.time.format.LocalizedPrinterParser; import hunt.time.chrono.Chronology; import hunt.time.chrono.ChronoLocalDate; import hunt.time.format.DateTimeFormatter; +import hunt.time.format.DateTimeFormatterBuilder; import hunt.time.format.DateTimeParseContext; import hunt.time.format.DateTimePrinterParser; import hunt.time.format.DateTimePrintContext; @@ -17,29 +18,30 @@ import hunt.collection.Map; import hunt.Exceptions; import hunt.util.Locale; +import std.concurrency : initOnce; + //----------------------------------------------------------------------- /** * Prints or parses a localized pattern. */ -static final class LocalizedPrinterParser : DateTimePrinterParser +final class LocalizedPrinterParser : DateTimePrinterParser { /** Cache of formatters. */ - // __gshared Map!(string, DateTimeFormatter) FORMATTER_CACHE; + static Map!(string, DateTimeFormatter) FORMATTER_CACHE() { + __gshared Map!(string, DateTimeFormatter) inst; + return initOnce!(inst)( + new HashMap!(string, DateTimeFormatter)(16, 0.75f /* , 2 */ )); + } private FormatStyle dateStyle; private FormatStyle timeStyle; - // shared static this() - // { - // FORMATTER_CACHE = new HashMap!(string, DateTimeFormatter)(16, 0.75f /* , 2 */ ); - mixin(MakeGlobalVar!(Map!(string, DateTimeFormatter))("FORMATTER_CACHE",`new HashMap!(string, DateTimeFormatter)(16, 0.75f /* , 2 */ )`)); - // } /** - * Constructor. - * - * @param dateStyle the date style to use, may be null - * @param timeStyle the time style to use, may be null - */ + * Constructor. + * + * @param dateStyle the date style to use, may be null + * @param timeStyle the time style to use, may be null + */ this(FormatStyle dateStyle, FormatStyle timeStyle) { // validated by caller @@ -47,14 +49,14 @@ static final class LocalizedPrinterParser : DateTimePrinterParser this.timeStyle = timeStyle; } - override public bool format(DateTimePrintContext context, StringBuilder buf) + override bool format(DateTimePrintContext context, StringBuilder buf) { Chronology chrono = Chronology.from(context.getTemporal()); return formatter(context.getLocale(), chrono).toPrinterParser(false) .format(context, buf); } - override public int parse(DateTimeParseContext context, string text, int position) + override int parse(DateTimeParseContext context, string text, int position) { Chronology chrono = context.getEffectiveChronology(); return formatter(context.getLocale(), chrono).toPrinterParser(false) @@ -62,35 +64,33 @@ static final class LocalizedPrinterParser : DateTimePrinterParser } /** - * Gets the formatter to use. - * !(p) - * The formatter will be the most appropriate to use for the date and time style _in the locale. - * For example, some locales will use the month name while others will use the number. - * - * @param locale the locale to use, not null - * @param chrono the chronology to use, not null - * @return the formatter, not null - * @throws IllegalArgumentException if the formatter cannot be found - */ + * Gets the formatter to use. + * !(p) + * The formatter will be the most appropriate to use for the date and time style _in the locale. + * For example, some locales will use the month name while others will use the number. + * + * @param locale the locale to use, not null + * @param chrono the chronology to use, not null + * @return the formatter, not null + * @throws IllegalArgumentException if the formatter cannot be found + */ private DateTimeFormatter formatter(Locale locale, Chronology chrono) { - // string key = chrono.getId() ~ '|' ~ locale.toString() ~ '|' ~ dateStyle.name() ~ timeStyle.name(); - // DateTimeFormatter formatter = FORMATTER_CACHE.get(key); - // if (formatter is null) - // { - // string pattern = getLocalizedDateTimePattern(dateStyle, - // timeStyle, chrono, locale); - // formatter = new DateTimeFormatterBuilder().appendPattern(pattern) - // .toFormatter(locale); - // DateTimeFormatter old = FORMATTER_CACHE.putIfAbsent(key, formatter); - // if (old !is null) - // { - // formatter = old; - // } - // } - // return formatter; - implementationMissing(false); - return null; + string key = chrono.getId() ~ '|' ~ locale.toString() ~ '|' ~ dateStyle.name() ~ timeStyle.name(); + DateTimeFormatter formatter = FORMATTER_CACHE.get(key); + if (formatter is null) { + string pattern = getLocalizedDateTimePattern(dateStyle, + timeStyle, chrono, locale); + formatter = new DateTimeFormatterBuilder().appendPattern(pattern) + .toFormatter(locale); + DateTimeFormatter old = FORMATTER_CACHE.putIfAbsent(key, formatter); + if (old !is null) { + formatter = old; + } + } + return formatter; + // implementationMissing(false); + // return null; } @@ -110,9 +110,8 @@ static final class LocalizedPrinterParser : DateTimePrinterParser * @return the locale and Chronology specific formatting pattern * @throws IllegalArgumentException if both dateStyle and timeStyle are null */ - public static string getLocalizedDateTimePattern(FormatStyle dateStyle, - FormatStyle timeStyle, Chronology chrono, Locale locale) - { + static string getLocalizedDateTimePattern(FormatStyle dateStyle, + FormatStyle timeStyle, Chronology chrono, Locale locale) { // assert(locale, "locale"); // assert(chrono, "chrono"); // if (dateStyle is null && timeStyle is null) { @@ -128,7 +127,7 @@ static final class LocalizedPrinterParser : DateTimePrinterParser return null; } - override public string toString() + override string toString() { return "Localized(" ~ (dateStyle !is null ? dateStyle.name() : "") ~ "," ~ (timeStyle !is null ? timeStyle.name() : "") ~ ")";