001
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
036 @DoPrivileged
037 public class FastDateFormatFactoryImpl implements FastDateFormatFactory {
038
039 @Override
040 public Format getDate(int style, Locale locale, TimeZone timeZone) {
041 String key = getKey(style, locale, timeZone);
042
043 Format format = _dateFormats.get(key);
044
045 if (format == null) {
046 format = FastDateFormat.getDateInstance(style, timeZone, locale);
047
048 _dateFormats.put(key, format);
049 }
050
051 return format;
052 }
053
054 @Override
055 public Format getDate(Locale locale) {
056 return getDate(locale, null);
057 }
058
059 @Override
060 public Format getDate(Locale locale, TimeZone timeZone) {
061 return getDate(FastDateFormatConstants.SHORT, locale, timeZone);
062 }
063
064 @Override
065 public Format getDate(TimeZone timeZone) {
066 return getDate(LocaleUtil.getDefault(), timeZone);
067 }
068
069 @Override
070 public Format getDateTime(
071 int dateStyle, int timeStyle, Locale locale, TimeZone timeZone) {
072
073 String key = getKey(dateStyle, timeStyle, locale, timeZone);
074
075 Format format = _dateTimeFormats.get(key);
076
077 if (format == null) {
078 format = FastDateFormat.getDateTimeInstance(
079 dateStyle, timeStyle, timeZone, locale);
080
081 _dateTimeFormats.put(key, format);
082 }
083
084 return format;
085 }
086
087 @Override
088 public Format getDateTime(Locale locale) {
089 return getDateTime(locale, null);
090 }
091
092 @Override
093 public Format getDateTime(Locale locale, TimeZone timeZone) {
094 return getDateTime(
095 FastDateFormatConstants.SHORT, FastDateFormatConstants.SHORT,
096 locale, timeZone);
097 }
098
099 @Override
100 public Format getDateTime(TimeZone timeZone) {
101 return getDateTime(LocaleUtil.getDefault(), timeZone);
102 }
103
104 @Override
105 public Format getSimpleDateFormat(String pattern) {
106 return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), null);
107 }
108
109 @Override
110 public Format getSimpleDateFormat(String pattern, Locale locale) {
111 return getSimpleDateFormat(pattern, locale, null);
112 }
113
114 @Override
115 public Format getSimpleDateFormat(
116 String pattern, Locale locale, TimeZone timeZone) {
117
118 String key = getKey(pattern, locale, timeZone);
119
120 Format format = _simpleDateFormats.get(key);
121
122 if (format == null) {
123 format = FastDateFormat.getInstance(pattern, timeZone, locale);
124
125 _simpleDateFormats.put(key, format);
126 }
127
128 return format;
129 }
130
131 @Override
132 public Format getSimpleDateFormat(String pattern, TimeZone timeZone) {
133 return getSimpleDateFormat(pattern, LocaleUtil.getDefault(), timeZone);
134 }
135
136 @Override
137 public Format getTime(int style, Locale locale, TimeZone timeZone) {
138 String key = getKey(style, locale, timeZone);
139
140 Format format = _timeFormats.get(key);
141
142 if (format == null) {
143 format = FastDateFormat.getTimeInstance(style, timeZone, locale);
144
145 _timeFormats.put(key, format);
146 }
147
148 return format;
149 }
150
151 @Override
152 public Format getTime(Locale locale) {
153 return getTime(locale, null);
154 }
155
156 @Override
157 public Format getTime(Locale locale, TimeZone timeZone) {
158 return getTime(FastDateFormatConstants.SHORT, locale, timeZone);
159 }
160
161 @Override
162 public Format getTime(TimeZone timeZone) {
163 return getTime(LocaleUtil.getDefault(), timeZone);
164 }
165
166 protected String getKey(Object... arguments) {
167 StringBundler sb = new StringBundler(arguments.length * 2 - 1);
168
169 for (int i = 0; i < arguments.length; i++) {
170 sb.append(arguments[i]);
171
172 if ((i + 1) < arguments.length) {
173 sb.append(StringPool.UNDERLINE);
174 }
175 }
176
177 return sb.toString();
178 }
179
180 private Map<String, Format> _dateFormats =
181 new ConcurrentHashMap<String, Format>();
182 private Map<String, Format> _dateTimeFormats =
183 new ConcurrentHashMap<String, Format>();
184 private Map<String, Format> _simpleDateFormats =
185 new ConcurrentHashMap<String, Format>();
186 private Map<String, Format> _timeFormats =
187 new ConcurrentHashMap<String, Format>();
188
189 }