1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.melati.poem;
47
48 import java.text.SimpleDateFormat;
49 import java.time.format.DateTimeFormatter;
50 import java.util.HashMap;
51 import java.util.Locale;
52 import java.text.DateFormat;
53 import java.text.DateFormatSymbols;
54
55 import org.melati.poem.util.StringUtils;
56
57
58
59
60 public class PoemLocale {
61
62 private static final HashMap<Locale, PoemLocale> localeCache = new HashMap<Locale, PoemLocale>();
63
64
65 public static final PoemLocale HERE = new PoemLocale(Locale.UK);
66
67 private final Locale locale;
68
69 private final DateFormatSymbols dateFormatSymbols;
70 private final String[] months, shortMonths;
71 private final DateFormat[] dateFormats;
72 private final DateFormat[] timestampFormats;
73
74
75
76
77
78
79
80 public static PoemLocale fromLanguageTag(String tag) {
81 String subtags[] = StringUtils.split(tag, '-');
82
83
84 if (subtags.length > 0 && subtags[0].length() == 2) {
85 Locale locale = null;
86
87 if (subtags.length > 1 && subtags[1].length() == 2)
88 locale = new Locale(subtags[0], subtags[1]);
89 else
90 locale = new Locale(subtags[0], "");
91 return new PoemLocale(locale);
92 }
93 return null;
94 }
95
96 public static PoemLocale from(Locale locale) {
97 if (locale == null)
98 throw new NullPointerException();
99 PoemLocale it = localeCache.get(locale);
100 if(it == null)
101 localeCache.put(locale, new PoemLocale(locale));
102 return localeCache.get(locale);
103 }
104
105
106
107
108
109
110 public PoemLocale(Locale locale) {
111 if (locale == null)
112 throw new NullPointerException();
113 this.locale = locale;
114
115 dateFormatSymbols = new DateFormatSymbols(locale);
116 months = dateFormatSymbols.getMonths();
117 shortMonths = dateFormatSymbols.getShortMonths();
118
119 dateFormats = new DateFormat[4];
120 dateFormats[DateFormat.FULL] =
121 DateFormat.getDateInstance(DateFormat.FULL, locale);
122 dateFormats[DateFormat.LONG] =
123 DateFormat.getDateInstance(DateFormat.LONG, locale);
124
125 dateFormats[DateFormat.MEDIUM] = locale == Locale.UK ? new SimpleDateFormat("DD-MMM-YYYY") :
126 DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
127 dateFormats[DateFormat.SHORT] =
128 DateFormat.getDateInstance(DateFormat.SHORT, locale);
129
130 timestampFormats = new DateFormat[4];
131 timestampFormats[DateFormat.FULL] =
132 DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,
133 locale);
134 timestampFormats[DateFormat.LONG] =
135 DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,
136 locale);
137 timestampFormats[DateFormat.MEDIUM] =
138 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM,
139 locale);
140 timestampFormats[DateFormat.SHORT] =
141 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
142 locale);
143 }
144
145
146
147
148 public final Locale locale() {
149 return locale;
150 }
151
152
153
154
155
156 public String monthName(int monthNum) {
157 return months[monthNum - 1];
158 }
159
160
161
162
163
164 public String shortMonthName(int monthNum) {
165 return shortMonths[monthNum - 1];
166 }
167
168
169
170
171
172 public DateFormat dateFormat(int style) {
173 return dateFormats[style];
174 }
175
176
177
178
179
180 public DateFormat timestampFormat(int style) {
181 return timestampFormats[style];
182 }
183
184
185
186
187
188
189
190
191 public int hashCode() {
192 return locale.hashCode();
193 }
194
195
196
197
198
199 public boolean equals(Object o) {
200 if (this == o)
201 return true;
202 if (o instanceof PoemLocale)
203 return locale.equals(((PoemLocale)o).locale());
204 else
205 return false;
206 }
207
208
209
210
211
212
213
214 public String toString() {
215 return locale.toString();
216 }
217 }