KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > forums > helper > SimpleDateFormatPool


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under GPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portlet.forums.helper;
12
13 import java.text.DateFormat JavaDoc;
14 import java.text.SimpleDateFormat JavaDoc;
15 import java.util.Date JavaDoc;
16
17 /**
18  * A simple date format pool. These objects are very expensive
19  * to create.
20  *
21  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
22  * @version $Revision: 1.1.1.1 $
23  */

24 public final class SimpleDateFormatPool
25    extends AbstractPool
26 {
27    /**
28     * The date version.
29     */

30    private int version = 0;
31
32    /**
33     * Default value is same as "D M d, Y g:i a" in PHP.
34     */

35    private String JavaDoc pattern = "EEE MMM d, yyyy HH:mm aaa";
36
37    /**
38     * Creates a new {@link SimpleDateFormatPool} object.
39     *
40     * @param maxSize DOCUMENT_ME
41     * @param initialSize DOCUMENT_ME
42     */

43    public SimpleDateFormatPool(int maxSize,
44                                int initialSize)
45    {
46       initialize(maxSize, initialSize);
47    }
48
49    /**
50     * DOCUMENT_ME
51     *
52     * @param format DOCUMENT_ME
53     */

54    public void setPattern(String JavaDoc format)
55    {
56       version++;
57       this.pattern = format;
58    }
59
60    /**
61     * DOCUMENT_ME
62     *
63     * @return DOCUMENT_ME
64     */

65    public String JavaDoc getPattern()
66    {
67       return pattern;
68    }
69
70    /**
71     * DOCUMENT_ME
72     *
73     * @return DOCUMENT_ME
74     */

75    protected Object JavaDoc _create()
76    {
77       return new SimpleDateFormatPool.VersionedSimpleDateFormat(pattern, version);
78    }
79
80    /**
81     * DOCUMENT_ME
82     *
83     * @param instance DOCUMENT_ME
84     */

85    protected void _acquire(Object JavaDoc instance)
86    {
87       SimpleDateFormatPool.VersionedSimpleDateFormat format =
88          (SimpleDateFormatPool.VersionedSimpleDateFormat) instance;
89       if (format.version < version)
90       {
91          format.version = version;
92          format.applyPattern(pattern);
93       }
94    }
95
96    private static class VersionedSimpleDateFormat
97       extends SimpleDateFormat JavaDoc
98    {
99       private int version;
100
101       /**
102        * Creates a new {@link VersionedSimpleDateFormat} object.
103        *
104        * @param pattern DOCUMENT_ME
105        * @param version DOCUMENT_ME
106        */

107       public VersionedSimpleDateFormat(String JavaDoc pattern,
108                                        int version)
109       {
110          super(pattern);
111          this.version = version;
112       }
113    }
114
115    /**
116     * DOCUMENT_ME
117     *
118     * @param date DOCUMENT_ME
119     *
120     * @return DOCUMENT_ME
121     */

122    public String JavaDoc formatDate(Date JavaDoc date)
123    {
124       DateFormat JavaDoc format = null;
125       try
126       {
127          format = (DateFormat JavaDoc) acquire();
128          return format.format(date);
129       }
130       finally
131       {
132          release(format);
133       }
134    }
135 }
Popular Tags