KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > SynchronizedDateFormat


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.util;
19
20 import java.text.ParseException JavaDoc;
21 import java.text.DateFormat JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.TimeZone JavaDoc;
26
27 /**
28  * This class is designed to be a synchronized wrapper for a
29  * <code>java.text.DateFormat</code> subclass. In general,
30  * these subclasses (most notably the <code>java.text.SimpleDateFormat</code>
31  * classes are not thread safe, so we need to synchronize on the
32  * internal DateFormat for all delegated calls.
33  *
34  */

35 public class SynchronizedDateFormat implements SimplifiedDateFormat {
36     private final DateFormat JavaDoc internalDateFormat;
37
38     /**
39      * Public constructor that mimics that of SimpleDateFormat. See
40      * java.text.SimpleDateFormat for more details.
41      *
42      * @param pattern the pattern that defines this DateFormat
43      * @param locale the locale
44      */

45     public SynchronizedDateFormat(String JavaDoc pattern, Locale JavaDoc locale) {
46         internalDateFormat = new SimpleDateFormat JavaDoc(pattern, locale);
47     }
48
49     /**
50      * <p>Wrapper method to allow child classes to synchronize a preexisting
51      * DateFormat.</p>
52      *
53      * <p>TODO: Investigate replacing this with a factory method.</p>
54      *
55      * @param the DateFormat to synchronize
56      */

57     protected SynchronizedDateFormat(DateFormat JavaDoc theDateFormat) {
58         internalDateFormat = theDateFormat;
59     }
60
61     /**
62      * SimpleDateFormat will handle most of this for us, but we
63      * want to ensure thread safety, so we wrap the call in a
64      * synchronized block.
65      *
66      * @return java.lang.String
67      * @param d Date
68      */

69     public String JavaDoc format(Date JavaDoc d) {
70         synchronized (internalDateFormat) {
71            return internalDateFormat.format(d);
72         }
73     }
74
75     /**
76      * Parses text from the beginning of the given string to produce a date.
77      * The method may not use the entire text of the given string.
78      * <p>
79      * This method is designed to be thread safe, so we wrap our delegated
80      * parse method in an appropriate synchronized block.
81      *
82      * @param source A <code>String</code> whose beginning should be parsed.
83      * @return A <code>Date</code> parsed from the string.
84      * @throws ParseException if the beginning of the specified string
85      * cannot be parsed.
86      */

87     public Date JavaDoc parse(String JavaDoc source) throws ParseException JavaDoc {
88         synchronized (internalDateFormat) {
89             return internalDateFormat.parse(source);
90         }
91     }
92
93     /**
94      * Sets the time zone of this SynchronizedDateFormat object.
95      * @param zone the given new time zone.
96      */

97     public void setTimeZone(TimeZone JavaDoc zone) {
98         synchronized(internalDateFormat) {
99             internalDateFormat.setTimeZone(zone);
100         }
101     }
102
103     /**
104      * Gets the time zone.
105      * @return the time zone associated with this SynchronizedDateFormat.
106      */

107     public TimeZone JavaDoc getTimeZone() {
108         synchronized(internalDateFormat) {
109             return internalDateFormat.getTimeZone();
110         }
111     }
112
113     /**
114      * Specify whether or not date/time parsing is to be lenient. With
115      * lenient parsing, the parser may use heuristics to interpret inputs that
116      * do not precisely match this object's format. With strict parsing,
117      * inputs must match this object's format.
118      * @param lenient when true, parsing is lenient
119      * @see java.util.Calendar#setLenient
120      */

121     public void setLenient(boolean lenient)
122     {
123         synchronized(internalDateFormat) {
124             internalDateFormat.setLenient(lenient);
125         }
126     }
127
128     /**
129      * Tell whether date/time parsing is to be lenient.
130      * @return whether this SynchronizedDateFormat is lenient.
131      */

132     public boolean isLenient()
133     {
134         synchronized(internalDateFormat) {
135             return internalDateFormat.isLenient();
136         }
137     }
138
139     /**
140      * Overrides hashCode
141      */

142     public int hashCode() {
143         synchronized(internalDateFormat) {
144             return internalDateFormat.hashCode();
145         }
146     }
147
148     /**
149      * Overrides equals
150      */

151     public boolean equals(Object JavaDoc obj) {
152         if (this == obj) {
153             return true;
154         }
155         if (obj == null || getClass() != obj.getClass()) {
156             return false;
157         }
158         synchronized(internalDateFormat) {
159             return internalDateFormat.equals(obj);
160         }
161     }
162
163 }
164
Popular Tags