View Javadoc

1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.utils;
29  
30  import java.text.DateFormat;
31  import java.text.ParseException;
32  import java.text.SimpleDateFormat;
33  import java.util.Date;
34  import java.util.Locale;
35  
36  import org.apache.commons.pool.BasePoolableObjectFactory;
37  import org.apache.commons.pool.ObjectPool;
38  import org.apache.commons.pool.impl.GenericObjectPool;
39  
40  /***
41   * Date formatter component.
42   * 
43   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
44   * @version $Id: DateFormatter.java,v 1.2 2004/12/27 05:17:43 rafal Exp $
45   */
46  public class DateFormatter
47  {
48      private String pattern;
49      
50      private Locale locale;    
51  
52      private ObjectPool dateFormatPool = new GenericObjectPool(new DateFormatFactory());    
53      
54      /***
55       * Constructs an instance of the date formatter.
56       * 
57       * @param pattern the formatting pattern.
58       * @param locale the locale name.
59       */    
60      public DateFormatter(String pattern, String locale)
61      {
62          this.pattern = pattern;
63          this.locale = StringUtils.getLocale(locale);
64      }
65  
66      /***
67       * Format date to string according to defined pattern.  
68       * 
69       * @param date the date.
70       * @return the string representation of date.
71       */
72      public String format(Date date)
73      {
74          DateFormat df = getDateFormat();
75          try
76          { 
77              return df.format(date);
78          }
79          finally
80          {
81              releaseDateForamt(df);
82          }
83      }
84      
85      /***
86       * Parse date from string.  
87       * 
88       * @param source the string representation of date.
89       * @return the date.
90       * @throws ParseException if string format is invalid.
91       */
92      public Date parse(String source)
93          throws ParseException
94      {
95          DateFormat df = getDateFormat();
96          try
97          { 
98              return df.parse(source);
99          }
100         finally
101         {
102             releaseDateForamt(df);
103         }
104     }    
105 
106     /***
107      * Acquired DateFormat object from the pool.
108      * 
109      * @return DateFormat object.
110      */    
111     private DateFormat getDateFormat()
112     {
113         try
114         {
115             return (DateFormat)dateFormatPool.borrowObject();
116         }
117         ///CLOVER:OFF
118         catch(Exception e)
119         {
120             throw (RuntimeException)new IllegalStateException("unexpected object pool failure").
121                 initCause(e);
122         }
123         ///CLOVER:ON
124     }
125 
126     /***
127      * Releases DateFormat object into the pool.
128      * 
129      * @param format DateFormat object.
130      */    
131     private void releaseDateForamt(DateFormat format)
132     {
133         try
134         {
135             dateFormatPool.returnObject(format);        
136         }
137         ///CLOVER:OFF
138         catch(Exception e)
139         {   
140             throw (RuntimeException)new IllegalStateException("unexpected object pool failure").
141                 initCause(e);
142         }
143         ///CLOVER:ON
144     }
145     
146     /***
147      * A factory of DateFormat objects.
148      */
149     private class DateFormatFactory
150         extends BasePoolableObjectFactory
151     {
152         /***
153          * {@inheritDoc}
154          */
155         public Object makeObject() throws Exception
156         {
157             return new SimpleDateFormat(pattern, locale);
158         }
159     }
160 }
161