001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.util.FastDateFormatFactory;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.text.Format;
023    
024    import java.util.Locale;
025    import java.util.Map;
026    import java.util.TimeZone;
027    import java.util.concurrent.ConcurrentHashMap;
028    
029    import org.apache.commons.lang.time.FastDateFormat;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class FastDateFormatFactoryImpl implements FastDateFormatFactory {
035    
036            public Format getDate(Locale locale) {
037                    return getDate(locale, null);
038            }
039    
040            public Format getDate(Locale locale, TimeZone timeZone) {
041                    String key = getKey(locale, timeZone);
042    
043                    Format format = _dateFormats.get(key);
044    
045                    if (format == null) {
046                            format = FastDateFormat.getDateInstance(
047                                    FastDateFormat.SHORT, timeZone, locale);
048    
049                            _dateFormats.put(key, format);
050                    }
051    
052                    return format;
053            }
054    
055            public Format getDate(TimeZone timeZone) {
056                    return getDate(LocaleUtil.getDefault(), timeZone);
057            }
058    
059            public Format getDateTime(Locale locale) {
060                    return getDateTime(locale, null);
061            }
062    
063            public Format getDateTime(Locale locale, TimeZone timeZone) {
064                    String key = getKey(locale, timeZone);
065    
066                    Format format = _dateTimeFormats.get(key);
067    
068                    if (format == null) {
069                            format = FastDateFormat.getDateTimeInstance(
070                                    FastDateFormat.SHORT, FastDateFormat.SHORT, timeZone, locale);
071    
072                            _dateTimeFormats.put(key, format);
073                    }
074    
075                    return format;
076            }
077    
078            public Format getDateTime(TimeZone timeZone) {
079                    return getDateTime(LocaleUtil.getDefault(), timeZone);
080            }
081    
082            public Format getSimpleDateFormat(String pattern) {
083                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), null);
084            }
085    
086            public Format getSimpleDateFormat(String pattern, Locale locale) {
087                    return getSimpleDateFormat(pattern, locale, null);
088            }
089    
090            public Format getSimpleDateFormat(
091                    String pattern, Locale locale, TimeZone timeZone) {
092    
093                    String key = getKey(pattern, locale, timeZone);
094    
095                    Format format = _simpleDateFormats.get(key);
096    
097                    if (format == null) {
098                            format = FastDateFormat.getInstance(pattern, timeZone, locale);
099    
100                            _simpleDateFormats.put(key, format);
101                    }
102    
103                    return format;
104            }
105    
106            public Format getSimpleDateFormat(String pattern, TimeZone timeZone) {
107                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), timeZone);
108            }
109    
110            public Format getTime(Locale locale) {
111                    return getTime(locale, null);
112            }
113    
114            public Format getTime(Locale locale, TimeZone timeZone) {
115                    String key = getKey(locale, timeZone);
116    
117                    Format format = _timeFormats.get(key);
118    
119                    if (format == null) {
120                            format = FastDateFormat.getTimeInstance(
121                                    FastDateFormat.SHORT, timeZone, locale);
122    
123                            _timeFormats.put(key, format);
124                    }
125    
126                    return format;
127            }
128    
129            public Format getTime(TimeZone timeZone) {
130                    return getTime(LocaleUtil.getDefault(), timeZone);
131            }
132    
133            protected String getKey(Locale locale, TimeZone timeZone) {
134                    return String.valueOf(locale).concat(StringPool.UNDERLINE).concat(
135                            String.valueOf(timeZone));
136            }
137    
138            protected String getKey(String pattern, Locale locale, TimeZone timeZone) {
139                    StringBundler sb = new StringBundler(5);
140    
141                    sb.append(pattern);
142                    sb.append(StringPool.UNDERLINE);
143                    sb.append(String.valueOf(locale));
144                    sb.append(StringPool.UNDERLINE);
145                    sb.append(String.valueOf(timeZone));
146    
147                    return sb.toString();
148            }
149    
150            private Map<String, Format> _dateFormats =
151                    new ConcurrentHashMap<String, Format>();
152            private Map<String, Format> _dateTimeFormats =
153                    new ConcurrentHashMap<String, Format>();
154            private Map<String, Format> _simpleDateFormats =
155                    new ConcurrentHashMap<String, Format>();
156            private Map<String, Format> _timeFormats =
157                    new ConcurrentHashMap<String, Format>();
158    
159    }