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