aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alexander Alexeev2016-12-10 21:00:14 +0700
committerGravatar Vitaly Takmazov2016-12-11 18:24:37 +0300
commit0cbdc4ecb927b7ab068e5833caefbba65c9aa87a (patch)
tree284267d6ebcf20e3f6d49e1e272443a55b450444
parenta110dea4804dd1011899a909612ba92e2807718a (diff)
caching date formatter
-rw-r--r--juick-core/src/main/java/com/juick/util/DateFormatter.java2
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java6
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java43
-rw-r--r--juick-spring-www/src/main/java/com/juick/www/formatter/SpringDateFormatter.java45
4 files changed, 49 insertions, 47 deletions
diff --git a/juick-core/src/main/java/com/juick/util/DateFormatter.java b/juick-core/src/main/java/com/juick/util/DateFormatter.java
index 34585c63..c4e7095b 100644
--- a/juick-core/src/main/java/com/juick/util/DateFormatter.java
+++ b/juick-core/src/main/java/com/juick/util/DateFormatter.java
@@ -13,7 +13,7 @@ import java.util.Date;
public class DateFormatter {
private final DateTimeFormatter formatter;
- DateFormatter(String pattern) {
+ public DateFormatter(String pattern) {
formatter = DateTimeFormatter.ofPattern(pattern);
}
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<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 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<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);
+ }
+
+}