From 0cbdc4ecb927b7ab068e5833caefbba65c9aa87a Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sat, 10 Dec 2016 21:00:14 +0700 Subject: caching date formatter --- .../www/configuration/WwwServletConfiguration.java | 6 +-- .../com/juick/www/formatter/DateFormatter.java | 43 --------------------- .../juick/www/formatter/SpringDateFormatter.java | 45 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 46 deletions(-) delete mode 100644 juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java create mode 100644 juick-spring-www/src/main/java/com/juick/www/formatter/SpringDateFormatter.java (limited to 'juick-spring-www/src/main/java/com/juick/www') 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 9a2bb1d4..2eb39e1c 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 74596fc5..00000000 --- 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 { - @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 00000000..61a2fe88 --- /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 { + @Resource + private MessageSource messageSource; + private ConcurrentMap 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); + } + +} -- cgit v1.2.3