diff options
Diffstat (limited to 'juick-spring-www/src/main')
3 files changed, 48 insertions, 46 deletions
diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java index 9a2bb1d41..2eb39e1c2 100644 --- a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java +++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java @@ -2,7 +2,7 @@ package com.juick.www.configuration; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; -import com.juick.www.formatter.DateFormatter; +import com.juick.www.formatter.SpringDateFormatter; import nz.net.ultraq.thymeleaf.LayoutDialect; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -122,7 +122,7 @@ public class WwwServletConfiguration extends WebMvcConfigurationSupport { } @Bean - public DateFormatter dateFormatter() { - return new DateFormatter(); + public SpringDateFormatter dateFormatter() { + return new SpringDateFormatter(); } } diff --git a/juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java b/juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java deleted file mode 100644 index 74596fc58..000000000 --- a/juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.juick.www.formatter; - -import org.springframework.context.MessageSource; -import org.springframework.format.Formatter; - -import javax.annotation.Resource; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; - -/** - * Created by aalexeev on 11/22/16. - */ -public class DateFormatter implements Formatter<Date> { - @Resource - private MessageSource messageSource; - - - public DateFormatter() { - super(); - } - - @Override - public Date parse(final String text, final Locale locale) throws ParseException { - final SimpleDateFormat dateFormat = createDateFormat(locale); - return dateFormat.parse(text); - } - - @Override - public String print(final Date object, final Locale locale) { - final SimpleDateFormat dateFormat = createDateFormat(locale); - return dateFormat.format(object); - } - - private SimpleDateFormat createDateFormat(final Locale locale) { - final String format = this.messageSource.getMessage("date.format", null, locale); - final SimpleDateFormat dateFormat = new SimpleDateFormat(format); - dateFormat.setLenient(false); - return dateFormat; - } - -} diff --git a/juick-spring-www/src/main/java/com/juick/www/formatter/SpringDateFormatter.java b/juick-spring-www/src/main/java/com/juick/www/formatter/SpringDateFormatter.java new file mode 100644 index 000000000..61a2fe885 --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/formatter/SpringDateFormatter.java @@ -0,0 +1,45 @@ +package com.juick.www.formatter; + +import com.juick.util.DateFormatter; +import org.springframework.context.MessageSource; +import org.springframework.format.Formatter; + +import javax.annotation.Resource; +import java.text.ParseException; +import java.util.Date; +import java.util.Locale; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * Created by aalexeev on 11/22/16. + */ +public class SpringDateFormatter implements Formatter<Date> { + @Resource + private MessageSource messageSource; + private ConcurrentMap<Locale, com.juick.util.DateFormatter> formattersMap = + new ConcurrentHashMap<>(4, 0.75f, 2); // MAX 4 languages and 4/2=2 threads for write + + + @Override + public Date parse(final String text, final Locale locale) throws ParseException { + DateFormatter formatter = formattersMap.getOrDefault( + locale, createFormatter(locale)); + + return formatter.parse(text); + } + + @Override + public String print(final Date object, final Locale locale) { + DateFormatter formatter = formattersMap.getOrDefault( + locale, createFormatter(locale)); + return formatter.format(object); + } + + private DateFormatter createFormatter(final Locale locale) { + String pattern = messageSource.getMessage("date.format", null, locale); + + return new DateFormatter(pattern); + } + +} |