View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  package org.apache.mailet.base;
22  
23  import java.text.ParseException;
24  import java.util.Date;
25  import java.util.Locale;
26  import java.util.TimeZone;
27  
28  /**
29   * A thread-safe date formatting class to produce dates formatted in accord with the
30   * specifications of RFC 977.
31   *
32   */
33  public class RFC977DateFormat implements SimplifiedDateFormat {
34  
35      /**
36       * Internal date formatter for long date formats
37       */
38      private final SynchronizedDateFormat internalLongDateFormat;
39  
40      /**
41       * Internal date formatter for short date formats
42       */
43      private final SynchronizedDateFormat internalShortDateFormat;
44  
45      /**
46       * Constructor for RFC977DateFormat
47       */
48      public RFC977DateFormat() {
49          internalLongDateFormat = new SynchronizedDateFormat("yyyyMMdd HHmmss", Locale.ENGLISH);
50          internalShortDateFormat = new SynchronizedDateFormat("yyMMdd HHmmss", Locale.ENGLISH);
51      }
52  
53      /**
54       * This method returns the long form of the RFC977 Date
55       *
56       * @return java.lang.String
57       * @param d Date
58       */
59      public String format(Date d) {
60          return internalLongDateFormat.format(d);
61      }
62  
63      /**
64       * Parses text from the beginning of the given string to produce a date.
65       * The method may not use the entire text of the given string.
66       * <p>
67       * This method is designed to be thread safe, so we wrap our delegated
68       * parse method in an appropriate synchronized block.
69       *
70       * @param source A <code>String</code> whose beginning should be parsed.
71       * @return A <code>Date</code> parsed from the string.
72       * @throws ParseException if the beginning of the specified string
73       *         cannot be parsed.
74       */
75      public Date parse(String source) throws ParseException {
76          source = source.trim();
77          if (source.indexOf(' ') == 6) {
78              return internalShortDateFormat.parse(source);
79          } else {
80              return internalLongDateFormat.parse(source);
81          }
82      }
83  
84      /**
85       * Sets the time zone of this SynchronizedDateFormat object.
86       * @param zone the given new time zone.
87       */
88      public void setTimeZone(TimeZone zone) {
89          synchronized(this) {
90              internalShortDateFormat.setTimeZone(zone);
91              internalLongDateFormat.setTimeZone(zone);
92          }
93      }
94  
95      /**
96       * Gets the time zone.
97       * @return the time zone associated with this SynchronizedDateFormat.
98       */
99      public TimeZone getTimeZone() {
100         synchronized(this) {
101             return internalShortDateFormat.getTimeZone();
102         }
103     }
104 
105     /**
106      * Specify whether or not date/time parsing is to be lenient.  With
107      * lenient parsing, the parser may use heuristics to interpret inputs that
108      * do not precisely match this object's format.  With strict parsing,
109      * inputs must match this object's format.
110      * @param lenient when true, parsing is lenient
111      * @see java.util.Calendar#setLenient
112      */
113     public void setLenient(boolean lenient)
114     {
115         synchronized(this) {
116             internalShortDateFormat.setLenient(lenient);
117             internalLongDateFormat.setLenient(lenient);
118         }
119     }
120 
121     /**
122      * Tell whether date/time parsing is to be lenient.
123      * @return whether this SynchronizedDateFormat is lenient.
124      */
125     public boolean isLenient()
126     {
127         synchronized(this) {
128             return internalShortDateFormat.isLenient();
129         }
130     }
131 
132 
133     /**
134      * Overrides equals
135      */
136     public boolean equals(Object obj) {
137         if (this == obj) {
138             return true;
139         }
140         if (!(obj instanceof RFC977DateFormat)) {
141             return false;
142         }
143         RFC977DateFormat theOtherRFC977DateFormat = (RFC977DateFormat)obj;
144         synchronized (this) {
145             return ((internalShortDateFormat.equals(theOtherRFC977DateFormat.internalShortDateFormat)) &&
146                     (internalLongDateFormat.equals(theOtherRFC977DateFormat.internalLongDateFormat)));
147         }
148     }
149 
150     /**
151      * Overrides hashCode
152      */
153     public int hashCode() {
154         return (int)(internalLongDateFormat.hashCode() & internalShortDateFormat.hashCode());
155     }
156 
157 }