aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-03-11 17:28:03 +0100
committersimonbrandhof <simon.brandhof@gmail.com>2011-03-14 08:41:33 +0100
commitec581b9a040c30eb69e6002ae2af16ab36b35d05 (patch)
treed12cd72a90f1fbba5bbad461a45ea5105478f133 /sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java
parent8d4fab85509a8864cecb5781fd4bc8d407a5e964 (diff)
downloadsonarqube-ec581b9a040c30eb69e6002ae2af16ab36b35d05.tar.gz
sonarqube-ec581b9a040c30eb69e6002ae2af16ab36b35d05.zip
API: DateUtils is now thread-safe
Diffstat (limited to 'sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java54
1 files changed, 38 insertions, 16 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java
index abf8e2bfa0e..b7702d660e5 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java
@@ -19,37 +19,31 @@
*/
package org.sonar.api.utils;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
+import java.lang.ref.SoftReference;
+import java.text.*;
import java.util.Date;
/**
+ * Parses and formats ISO 8601 dates. See http://en.wikipedia.org/wiki/ISO_8601.
+ * This class is thread-safe.
+ *
* @since 2.7
*/
public final class DateUtils {
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
- private static final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
- private static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATETIME_FORMAT);
+ private static final DateFormat dateFormat = new ThreadSafeDateFormat(DATE_FORMAT);
+ private static final DateFormat dateTimeFormat = new ThreadSafeDateFormat(DATETIME_FORMAT);
- /**
- * This method is not thread-safe.
- */
public static String formatDate(Date d) {
return dateFormat.format(d);
}
- /**
- * This method is not thread-safe.
- */
public static String formatDateTime(Date d) {
return dateTimeFormat.format(d);
}
- /**
- * This method is not thread-safe.
- */
public static Date parseDate(String s) {
try {
return dateFormat.parse(s);
@@ -59,9 +53,6 @@ public final class DateUtils {
}
}
- /**
- * This method is not thread-safe.
- */
public static Date parseDateTime(String s) {
try {
return dateTimeFormat.parse(s);
@@ -70,4 +61,35 @@ public final class DateUtils {
throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'");
}
}
+
+ static class ThreadSafeDateFormat extends DateFormat {
+ private final String format;
+
+ ThreadSafeDateFormat(String format) {
+ this.format = format;
+ }
+
+ private final ThreadLocal cache = new ThreadLocal() {
+ public Object get() {
+ SoftReference softRef = (SoftReference) super.get();
+ if (softRef == null || softRef.get() == null) {
+ softRef = new SoftReference(new SimpleDateFormat(format));
+ super.set(softRef);
+ }
+ return softRef;
+ }
+ };
+
+ private DateFormat getDateFormat() {
+ return (DateFormat) ((SoftReference) cache.get()).get();
+ }
+
+ public StringBuffer format(Date date,StringBuffer toAppendTo, FieldPosition fieldPosition) {
+ return getDateFormat().format(date, toAppendTo, fieldPosition);
+ }
+
+ public Date parse(String source, ParsePosition pos) {
+ return getDateFormat().parse(source, pos);
+ }
+ }
}