You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/10/24 17:02:50 UTC
svn commit: r587904 - in /jakarta/httpcomponents/httpclient/trunk:
RELEASE_NOTES.txt
module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java
Author: olegk
Date: Wed Oct 24 08:02:49 2007
New Revision: 587904
URL: http://svn.apache.org/viewvc?rev=587904&view=rev
Log:
HTTPCLIENT-689: Caching of SimpleDateFormat in DateUtils
Contributed by Daniel Müller <strider at digitalstrider.com>
Reviewed by Oleg Kalnichevski
Modified:
jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java
Modified: jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=587904&r1=587903&r2=587904&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Wed Oct 24 08:02:49 2007
@@ -1,5 +1,8 @@
Changes since release 4.0 Alpha 1
+* [HTTPCLIENT-689] Caching of SimpleDateFormat in DateUtils
+ Contributed by Daniel Müller <strider at digitalstrider.com>
+
* [HTTPCLIENT-689] stackable parameters in AbstractHttpClient
Contributed by Roland Weber <rolandw at apache.org>
Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java?rev=587904&r1=587903&r2=587904&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java Wed Oct 24 08:02:49 2007
@@ -31,11 +31,14 @@
package org.apache.http.impl.cookie;
+import java.lang.ref.SoftReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
+import java.util.HashMap;
import java.util.Locale;
+import java.util.Map;
import java.util.TimeZone;
/**
@@ -149,15 +152,10 @@
dateValue = dateValue.substring (1, dateValue.length() - 1);
}
- SimpleDateFormat dateParser = null;
for (int i = 0; i < dateFormats.length; i++) {
- if (dateParser == null) {
- dateParser = new SimpleDateFormat(dateFormats[i], Locale.US);
- dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
- dateParser.set2DigitYearStart(startDate);
- } else {
- dateParser.applyPattern(dateFormats[i]);
- }
+ SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormats[i]);
+ dateParser.set2DigitYearStart(startDate);
+
try {
return dateParser.parse(dateValue);
} catch (ParseException pe) {
@@ -198,13 +196,61 @@
if (date == null) throw new IllegalArgumentException("date is null");
if (pattern == null) throw new IllegalArgumentException("pattern is null");
- SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.US);
- formatter.setTimeZone(GMT);
+ SimpleDateFormat formatter = DateFormatHolder.formatFor(pattern);
return formatter.format(date);
}
/** This class should not be instantiated. */
private DateUtils() {
+ }
+
+ /**
+ * A factory for {@link SimpleDateFormat}s. The instances are stored in a
+ * threadlocal way because SimpleDateFormat is not threadsafe as noted in
+ * {@link SimpleDateFormat its javadoc}.
+ *
+ * @author Daniel Mueller
+ */
+ final static class DateFormatHolder {
+
+ private static final ThreadLocal THREADLOCAL_FORMATS = new ThreadLocal() {
+
+ protected Object initialValue() {
+ return new SoftReference(new HashMap());
+ }
+
+ };
+
+ /**
+ * creates a {@link SimpleDateFormat} for the requested format string.
+ *
+ * @param dateformat
+ * a non-<code>null</code> format String according to
+ * {@link SimpleDateFormat}. The format is not checked against
+ * <code>null</code> since all paths go through
+ * {@link DateUtils}.
+ * @return the requested format. This simple dateformat should not be used
+ * to {@link SimpleDateFormat#applyPattern(String) apply} to a
+ * different pattern.
+ */
+ public static SimpleDateFormat formatFor(String pattern) {
+ SoftReference ref = (SoftReference) THREADLOCAL_FORMATS.get();
+ Map formats = (Map) ref.get();
+ if (formats == null) {
+ formats = new HashMap();
+ THREADLOCAL_FORMATS.set(new SoftReference(formats));
+ }
+
+ SimpleDateFormat format = (SimpleDateFormat) formats.get(pattern);
+ if (format == null) {
+ format = new SimpleDateFormat(pattern, Locale.US);
+ format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ formats.put(pattern, format);
+ }
+
+ return format;
+ }
+
}
}