001    /**
002     * Copyright (c) 2000-2012 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.FastDateFormatConstants;
018    import com.liferay.portal.kernel.util.FastDateFormatFactory;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import java.text.Format;
024    
025    import java.util.Locale;
026    import java.util.Map;
027    import java.util.TimeZone;
028    import java.util.concurrent.ConcurrentHashMap;
029    
030    import org.apache.commons.lang.time.FastDateFormat;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class FastDateFormatFactoryImpl implements FastDateFormatFactory {
036    
037            public Format getDate(int style, Locale locale, TimeZone timeZone) {
038                    String key = getKey(style, locale, timeZone);
039    
040                    Format format = _dateFormats.get(key);
041    
042                    if (format == null) {
043                            format = FastDateFormat.getDateInstance(style, timeZone, locale);
044    
045                            _dateFormats.put(key, format);
046                    }
047    
048                    return format;
049            }
050    
051            public Format getDate(Locale locale) {
052                    return getDate(locale, null);
053            }
054    
055            public Format getDate(Locale locale, TimeZone timeZone) {
056                    return getDate(FastDateFormatConstants.SHORT, locale, timeZone);
057            }
058    
059            public Format getDate(TimeZone timeZone) {
060                    return getDate(LocaleUtil.getDefault(), timeZone);
061            }
062    
063            public Format getDateTime(
064                    int dateStyle, int timeStyle, Locale locale, TimeZone timeZone) {
065    
066                    String key = getKey(dateStyle, timeStyle, locale, timeZone);
067    
068                    Format format = _dateTimeFormats.get(key);
069    
070                    if (format == null) {
071                            format = FastDateFormat.getDateTimeInstance(
072                                    dateStyle, timeStyle, timeZone, locale);
073    
074                            _dateTimeFormats.put(key, format);
075                    }
076    
077                    return format;
078            }
079    
080            public Format getDateTime(Locale locale) {
081                    return getDateTime(locale, null);
082            }
083    
084            public Format getDateTime(Locale locale, TimeZone timeZone) {
085                    return getDateTime(
086                            FastDateFormatConstants.SHORT, FastDateFormatConstants.SHORT,
087                            locale, timeZone);
088            }
089    
090            public Format getDateTime(TimeZone timeZone) {
091                    return getDateTime(LocaleUtil.getDefault(), timeZone);
092            }
093    
094            public Format getSimpleDateFormat(String pattern) {
095                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), null);
096            }
097    
098            public Format getSimpleDateFormat(String pattern, Locale locale) {
099                    return getSimpleDateFormat(pattern, locale, null);
100            }
101    
102            public Format getSimpleDateFormat(
103                    String pattern, Locale locale, TimeZone timeZone) {
104    
105                    String key = getKey(pattern, locale, timeZone);
106    
107                    Format format = _simpleDateFormats.get(key);
108    
109                    if (format == null) {
110                            format = FastDateFormat.getInstance(pattern, timeZone, locale);
111    
112                            _simpleDateFormats.put(key, format);
113                    }
114    
115                    return format;
116            }
117    
118            public Format getSimpleDateFormat(String pattern, TimeZone timeZone) {
119                    return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), timeZone);
120            }
121    
122            public Format getTime(int style, Locale locale, TimeZone timeZone) {
123                    String key = getKey(style, locale, timeZone);
124    
125                    Format format = _timeFormats.get(key);
126    
127                    if (format == null) {
128                            format = FastDateFormat.getTimeInstance(style, timeZone, locale);
129    
130                            _timeFormats.put(key, format);
131                    }
132    
133                    return format;
134            }
135    
136            public Format getTime(Locale locale) {
137                    return getTime(locale, null);
138            }
139    
140            public Format getTime(Locale locale, TimeZone timeZone) {
141                    return getTime(FastDateFormatConstants.SHORT, locale, timeZone);
142            }
143    
144            public Format getTime(TimeZone timeZone) {
145                    return getTime(LocaleUtil.getDefault(), timeZone);
146            }
147    
148            protected String getKey(Object... arguments) {
149                    StringBundler sb = new StringBundler(arguments.length * 2 - 1);
150    
151                    for (int i = 0; i < arguments.length; i++) {
152                            sb.append(arguments[i]);
153    
154                            if ((i + 1) < arguments.length) {
155                                    sb.append(StringPool.UNDERLINE);
156                            }
157                    }
158    
159                    return sb.toString();
160            }
161    
162            private Map<String, Format> _dateFormats =
163                    new ConcurrentHashMap<String, Format>();
164            private Map<String, Format> _dateTimeFormats =
165                    new ConcurrentHashMap<String, Format>();
166            private Map<String, Format> _simpleDateFormats =
167                    new ConcurrentHashMap<String, Format>();
168            private Map<String, Format> _timeFormats =
169                    new ConcurrentHashMap<String, Format>();
170    
171    }