KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > CachingDateFormat


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util;
18
19 import java.text.ParsePosition JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.WeakHashMap JavaDoc;
25
26 /**
27  * Provides <b>thread safe</b> means of obtaining a cached date formatter.
28  * <p>
29  * The cached string-date mappings are stored in a <tt>WeakHashMap</tt>.
30  *
31  * @see java.text.DateFormat#setLenient(boolean)
32  *
33  * @author Derek Hulley
34  */

35 public class CachingDateFormat extends SimpleDateFormat JavaDoc
36 {
37     private static final long serialVersionUID = 3258415049197565235L;
38
39     /** <pre> yyyy-MM-dd'T'HH:mm:ss </pre> */
40     public static final String JavaDoc FORMAT_FULL_GENERIC = "yyyy-MM-dd'T'HH:mm:ss";
41
42     /** <pre> yyyy-MM-dd </pre> */
43     public static final String JavaDoc FORMAT_DATE_GENERIC = "yyyy-MM-dd";
44
45     /** <pre> HH:mm:ss </pre> */
46     public static final String JavaDoc FORMAT_TIME_GENERIC = "HH:mm:ss";
47
48     private static ThreadLocal JavaDoc<SimpleDateFormat JavaDoc> s_localDateFormat = new ThreadLocal JavaDoc<SimpleDateFormat JavaDoc>();
49
50     private static ThreadLocal JavaDoc<SimpleDateFormat JavaDoc> s_localDateOnlyFormat = new ThreadLocal JavaDoc<SimpleDateFormat JavaDoc>();
51
52     private static ThreadLocal JavaDoc<SimpleDateFormat JavaDoc> s_localTimeOnlyFormat = new ThreadLocal JavaDoc<SimpleDateFormat JavaDoc>();
53
54     transient private Map JavaDoc<String JavaDoc, Date JavaDoc> cacheDates = new WeakHashMap JavaDoc<String JavaDoc, Date JavaDoc>(89);
55
56     private CachingDateFormat(String JavaDoc format)
57     {
58         super(format);
59     }
60
61     public String JavaDoc toString()
62     {
63         return this.toPattern();
64     }
65
66     /**
67      * @param length
68      * the type of date format, e.g. {@link CachingDateFormat#LONG }
69      * @param locale
70      * the <code>Locale</code> that will be used to determine the
71      * date pattern
72      *
73      * @see #getDateFormat(String, boolean)
74      * @see CachingDateFormat#SHORT
75      * @see CachingDateFormat#MEDIUM
76      * @see CachingDateFormat#LONG
77      * @see CachingDateFormat#FULL
78      */

79     public static SimpleDateFormat JavaDoc getDateFormat(int length, Locale JavaDoc locale, boolean lenient)
80     {
81         SimpleDateFormat JavaDoc dateFormat = (SimpleDateFormat JavaDoc) CachingDateFormat.getDateInstance(length, locale);
82         // extract the format string
83
String JavaDoc pattern = dateFormat.toPattern();
84         // we have a pattern to use
85
return getDateFormat(pattern, lenient);
86     }
87
88     /**
89      * @param dateLength
90      * the type of date format, e.g. {@link CachingDateFormat#LONG }
91      * @param timeLength
92      * the type of time format, e.g. {@link CachingDateFormat#LONG }
93      * @param locale
94      * the <code>Locale</code> that will be used to determine the
95      * date pattern
96      *
97      * @see #getDateFormat(String, boolean)
98      * @see CachingDateFormat#SHORT
99      * @see CachingDateFormat#MEDIUM
100      * @see CachingDateFormat#LONG
101      * @see CachingDateFormat#FULL
102      */

103     public static SimpleDateFormat JavaDoc getDateTimeFormat(int dateLength, int timeLength, Locale JavaDoc locale, boolean lenient)
104     {
105         SimpleDateFormat JavaDoc dateFormat = (SimpleDateFormat JavaDoc) CachingDateFormat.getDateTimeInstance(dateLength, timeLength, locale);
106         // extract the format string
107
String JavaDoc pattern = dateFormat.toPattern();
108         // we have a pattern to use
109
return getDateFormat(pattern, lenient);
110     }
111
112     /**
113      * @param pattern
114      * the conversion pattern to use
115      * @param lenient
116      * true to allow the parser to extract the date in conceivable
117      * manner
118      * @return Returns a conversion-cacheing formatter for the given pattern,
119      * but the instance itself is not cached
120      */

121     public static SimpleDateFormat JavaDoc getDateFormat(String JavaDoc pattern, boolean lenient)
122     {
123         // create an alfrescoDateFormat for cacheing purposes
124
SimpleDateFormat JavaDoc dateFormat = new CachingDateFormat(pattern);
125         // set leniency
126
dateFormat.setLenient(lenient);
127         // done
128
return dateFormat;
129     }
130
131     /**
132      * @return Returns a thread-safe formatter for the generic date/time format
133      *
134      * @see #FORMAT_FULL_GENERIC
135      */

136     public static SimpleDateFormat JavaDoc getDateFormat()
137     {
138         if (s_localDateFormat.get() != null)
139         {
140             return s_localDateFormat.get();
141         }
142
143         CachingDateFormat formatter = new CachingDateFormat(FORMAT_FULL_GENERIC);
144         // it must be strict
145
formatter.setLenient(false);
146         // put this into the threadlocal object
147
s_localDateFormat.set(formatter);
148         // done
149
return s_localDateFormat.get();
150     }
151
152     /**
153      * @return Returns a thread-safe formatter for the generic date format
154      *
155      * @see #FORMAT_DATE_GENERIC
156      */

157     public static SimpleDateFormat JavaDoc getDateOnlyFormat()
158     {
159         if (s_localDateOnlyFormat.get() != null)
160         {
161             return s_localDateOnlyFormat.get();
162         }
163
164         CachingDateFormat formatter = new CachingDateFormat(FORMAT_DATE_GENERIC);
165         // it must be strict
166
formatter.setLenient(false);
167         // put this into the threadlocal object
168
s_localDateOnlyFormat.set(formatter);
169         // done
170
return s_localDateOnlyFormat.get();
171     }
172
173     /**
174      * @return Returns a thread-safe formatter for the generic time format
175      *
176      * @see #FORMAT_TIME_GENERIC
177      */

178     public static SimpleDateFormat JavaDoc getTimeOnlyFormat()
179     {
180         if (s_localTimeOnlyFormat.get() != null)
181         {
182             return s_localTimeOnlyFormat.get();
183         }
184
185         CachingDateFormat formatter = new CachingDateFormat(FORMAT_TIME_GENERIC);
186         // it must be strict
187
formatter.setLenient(false);
188         // put this into the threadlocal object
189
s_localTimeOnlyFormat.set(formatter);
190         // done
191
return s_localTimeOnlyFormat.get();
192     }
193
194     /**
195      * Parses and caches date strings.
196      *
197      * @see java.text.DateFormat#parse(java.lang.String,
198      * java.text.ParsePosition)
199      */

200     public Date JavaDoc parse(String JavaDoc text, ParsePosition JavaDoc pos)
201     {
202         Date JavaDoc cached = cacheDates.get(text);
203         if (cached == null)
204         {
205             Date JavaDoc date = super.parse(text, pos);
206             if ((date != null) && (pos.getIndex() == text.length()))
207             {
208                 cacheDates.put(text, date);
209                 Date JavaDoc clonedDate = (Date JavaDoc) date.clone();
210                 return clonedDate;
211             }
212             else
213             {
214                 return date;
215             }
216         }
217         else
218         {
219             pos.setIndex(text.length());
220             Date JavaDoc clonedDate = (Date JavaDoc) cached.clone();
221             return clonedDate;
222         }
223     }
224 }
225
Popular Tags