15289
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
* This file is available under and governed by the GNU General Public
|
|
28 |
* License version 2 only, as published by the Free Software Foundation.
|
|
29 |
* However, the following notice accompanied the original version of this
|
|
30 |
* file:
|
|
31 |
*
|
|
32 |
* Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos
|
|
33 |
*
|
|
34 |
* All rights reserved.
|
|
35 |
*
|
|
36 |
* Redistribution and use in source and binary forms, with or without
|
|
37 |
* modification, are permitted provided that the following conditions are met:
|
|
38 |
*
|
|
39 |
* * Redistributions of source code must retain the above copyright notice,
|
|
40 |
* this list of conditions and the following disclaimer.
|
|
41 |
*
|
|
42 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
43 |
* this list of conditions and the following disclaimer in the documentation
|
|
44 |
* and/or other materials provided with the distribution.
|
|
45 |
*
|
|
46 |
* * Neither the name of JSR-310 nor the names of its contributors
|
|
47 |
* may be used to endorse or promote products derived from this software
|
|
48 |
* without specific prior written permission.
|
|
49 |
*
|
|
50 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
51 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
52 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
53 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
54 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
55 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
56 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
57 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
58 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
59 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
61 |
*/
|
|
62 |
package java.time.format;
|
|
63 |
|
|
64 |
import static java.time.temporal.ChronoField.AMPM_OF_DAY;
|
|
65 |
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
|
|
66 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
|
|
67 |
|
|
68 |
import java.time.temporal.Chrono;
|
|
69 |
import java.time.temporal.ISOChrono;
|
|
70 |
import java.time.calendar.JapaneseChrono;
|
|
71 |
import java.time.calendar.ThaiBuddhistChrono;
|
|
72 |
import java.time.temporal.TemporalField;
|
|
73 |
import java.util.AbstractMap.SimpleImmutableEntry;
|
|
74 |
import java.util.ArrayList;
|
|
75 |
import java.util.Calendar;
|
|
76 |
import java.util.Collections;
|
|
77 |
import java.util.Comparator;
|
|
78 |
import java.util.HashMap;
|
|
79 |
import java.util.Iterator;
|
|
80 |
import java.util.List;
|
|
81 |
import java.util.Locale;
|
|
82 |
import java.util.Map;
|
|
83 |
import java.util.Map.Entry;
|
|
84 |
import java.util.concurrent.ConcurrentHashMap;
|
|
85 |
import java.util.concurrent.ConcurrentMap;
|
|
86 |
import java.util.spi.CalendarNameProvider;
|
|
87 |
|
|
88 |
import sun.util.locale.provider.LocaleProviderAdapter;
|
|
89 |
import sun.util.locale.provider.CalendarDataUtility;
|
|
90 |
|
|
91 |
/**
|
|
92 |
* A provider to obtain the textual form of a date-time field.
|
|
93 |
*
|
|
94 |
* <h3>Specification for implementors</h3>
|
|
95 |
* Implementations must be thread-safe.
|
|
96 |
* Implementations should cache the textual information.
|
|
97 |
*
|
|
98 |
* @since 1.8
|
|
99 |
*/
|
|
100 |
class DateTimeTextProvider {
|
|
101 |
|
|
102 |
/** Cache. */
|
|
103 |
private static final ConcurrentMap<Entry<TemporalField, Locale>, Object> CACHE = new ConcurrentHashMap<>(16, 0.75f, 2);
|
|
104 |
/** Comparator. */
|
|
105 |
private static final Comparator<Entry<String, Long>> COMPARATOR = new Comparator<Entry<String, Long>>() {
|
|
106 |
@Override
|
|
107 |
public int compare(Entry<String, Long> obj1, Entry<String, Long> obj2) {
|
|
108 |
return obj2.getKey().length() - obj1.getKey().length(); // longest to shortest
|
|
109 |
}
|
|
110 |
};
|
|
111 |
|
|
112 |
DateTimeTextProvider() {}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Gets the provider of text.
|
|
116 |
*
|
|
117 |
* @return the provider, not null
|
|
118 |
*/
|
|
119 |
static DateTimeTextProvider getInstance() {
|
|
120 |
return new DateTimeTextProvider();
|
|
121 |
}
|
|
122 |
|
|
123 |
/**
|
|
124 |
* Gets the text for the specified field, locale and style
|
|
125 |
* for the purpose of printing.
|
|
126 |
* <p>
|
|
127 |
* The text associated with the value is returned.
|
|
128 |
* The null return value should be used if there is no applicable text, or
|
|
129 |
* if the text would be a numeric representation of the value.
|
|
130 |
*
|
|
131 |
* @param field the field to get text for, not null
|
|
132 |
* @param value the field value to get text for, not null
|
|
133 |
* @param style the style to get text for, not null
|
|
134 |
* @param locale the locale to get text for, not null
|
|
135 |
* @return the text for the field value, null if no text found
|
|
136 |
*/
|
|
137 |
public String getText(TemporalField field, long value, TextStyle style, Locale locale) {
|
|
138 |
Object store = findStore(field, locale);
|
|
139 |
if (store instanceof LocaleStore) {
|
|
140 |
return ((LocaleStore) store).getText(value, style);
|
|
141 |
}
|
|
142 |
return null;
|
|
143 |
}
|
|
144 |
|
|
145 |
private static int toStyle(TextStyle style) {
|
|
146 |
if (style == TextStyle.FULL) {
|
|
147 |
return Calendar.LONG_FORMAT;
|
|
148 |
} else if (style == TextStyle.SHORT) {
|
|
149 |
return Calendar.SHORT_FORMAT;
|
|
150 |
}
|
|
151 |
return Calendar.NARROW_STANDALONE;
|
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
|
155 |
* Gets the era text for the specified chrono, value, style and locale
|
|
156 |
* for the purpose of printing.
|
|
157 |
* <p>
|
|
158 |
* The era text associated with the value is returned.
|
|
159 |
* The null return value should be used if there is no applicable text, or
|
|
160 |
* if the text would be a numeric representation of the value.
|
|
161 |
*
|
|
162 |
* @param chrono the chrono to get text for, not null
|
|
163 |
* @param value the field value to get text for, not null
|
|
164 |
* @param style the style to get text for, not null
|
|
165 |
* @param locale the locale to get text for, not null
|
|
166 |
* @return the era text for the value, null if no text found
|
|
167 |
*/
|
|
168 |
public String getEraText(Chrono chrono, long value, TextStyle style, Locale locale) {
|
|
169 |
String type = null;
|
|
170 |
if (chrono == ISOChrono.INSTANCE) {
|
|
171 |
type = "gregory";
|
|
172 |
} else if (chrono == JapaneseChrono.INSTANCE) {
|
|
173 |
type = "japanese";
|
|
174 |
if (value == -999) {
|
|
175 |
value = 0;
|
|
176 |
} else {
|
|
177 |
value += 2;
|
|
178 |
}
|
|
179 |
} else if (chrono == ThaiBuddhistChrono.INSTANCE) {
|
|
180 |
type = "buddhist";
|
|
181 |
} else {
|
|
182 |
return null;
|
|
183 |
}
|
|
184 |
return CalendarDataUtility.retrieveFieldValueName(
|
|
185 |
type, Calendar.ERA, (int)value, toStyle(style), locale);
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Gets an iterator of text to field for the specified field, locale and style
|
|
190 |
* for the purpose of parsing.
|
|
191 |
* <p>
|
|
192 |
* The iterator must be returned in order from the longest text to the shortest.
|
|
193 |
* <p>
|
|
194 |
* The null return value should be used if there is no applicable parsable text, or
|
|
195 |
* if the text would be a numeric representation of the value.
|
|
196 |
* Text can only be parsed if all the values for that field-style-locale combination are unique.
|
|
197 |
*
|
|
198 |
* @param field the field to get text for, not null
|
|
199 |
* @param style the style to get text for, null for all parsable text
|
|
200 |
* @param locale the locale to get text for, not null
|
|
201 |
* @return the iterator of text to field pairs, in order from longest text to shortest text,
|
|
202 |
* null if the field or style is not parsable
|
|
203 |
*/
|
|
204 |
public Iterator<Entry<String, Long>> getTextIterator(TemporalField field, TextStyle style, Locale locale) {
|
|
205 |
Object store = findStore(field, locale);
|
|
206 |
if (store instanceof LocaleStore) {
|
|
207 |
return ((LocaleStore) store).getTextIterator(style);
|
|
208 |
}
|
|
209 |
return null;
|
|
210 |
}
|
|
211 |
|
|
212 |
private Object findStore(TemporalField field, Locale locale) {
|
|
213 |
Entry<TemporalField, Locale> key = createEntry(field, locale);
|
|
214 |
Object store = CACHE.get(key);
|
|
215 |
if (store == null) {
|
|
216 |
store = createStore(field, locale);
|
|
217 |
CACHE.putIfAbsent(key, store);
|
|
218 |
store = CACHE.get(key);
|
|
219 |
}
|
|
220 |
return store;
|
|
221 |
}
|
|
222 |
|
|
223 |
private static int toWeekDay(int calWeekDay) {
|
|
224 |
if (calWeekDay == Calendar.SUNDAY) {
|
|
225 |
return 7;
|
|
226 |
} else {
|
|
227 |
return calWeekDay - 1;
|
|
228 |
}
|
|
229 |
}
|
|
230 |
|
|
231 |
private Object createStore(TemporalField field, Locale locale) {
|
|
232 |
CalendarNameProvider provider = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, locale)
|
|
233 |
.getCalendarNameProvider();
|
|
234 |
Map<TextStyle, Map<Long, String>> styleMap = new HashMap<>();
|
|
235 |
if (field == MONTH_OF_YEAR) {
|
|
236 |
Map<Long, String> map = new HashMap<>();
|
|
237 |
for (Entry<String, Integer> entry :
|
|
238 |
provider.getDisplayNames("gregory", Calendar.MONTH, Calendar.LONG_FORMAT, locale).entrySet()) {
|
|
239 |
map.put((long)(entry.getValue() + 1), entry.getKey());
|
|
240 |
}
|
|
241 |
styleMap.put(TextStyle.FULL, map);
|
|
242 |
|
|
243 |
map = new HashMap<>();
|
|
244 |
for (Entry<String, Integer> entry :
|
|
245 |
provider.getDisplayNames("gregory", Calendar.MONTH, Calendar.SHORT_FORMAT, locale).entrySet()) {
|
|
246 |
map.put((long)(entry.getValue() + 1), entry.getKey());
|
|
247 |
}
|
|
248 |
styleMap.put(TextStyle.SHORT, map);
|
|
249 |
|
|
250 |
map = new HashMap<>();
|
|
251 |
for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
|
|
252 |
String name = provider.getDisplayName("gregory", Calendar.MONTH, month, Calendar.NARROW_STANDALONE, locale);
|
|
253 |
if (name != null) {
|
|
254 |
map.put((long)(month + 1), name);
|
|
255 |
}
|
|
256 |
}
|
|
257 |
if (map.size() != 0) {
|
|
258 |
styleMap.put(TextStyle.NARROW, map);
|
|
259 |
}
|
|
260 |
return new LocaleStore(styleMap);
|
|
261 |
}
|
|
262 |
if (field == DAY_OF_WEEK) {
|
|
263 |
Map<Long, String> map = new HashMap<>();
|
|
264 |
for (Entry<String, Integer> entry :
|
|
265 |
provider.getDisplayNames("gregory", Calendar.DAY_OF_WEEK, Calendar.LONG_FORMAT, locale).entrySet()) {
|
|
266 |
map.put((long)toWeekDay(entry.getValue()), entry.getKey());
|
|
267 |
}
|
|
268 |
styleMap.put(TextStyle.FULL, map);
|
|
269 |
map = new HashMap<>();
|
|
270 |
for (Entry<String, Integer> entry :
|
|
271 |
provider.getDisplayNames("gregory", Calendar.DAY_OF_WEEK, Calendar.SHORT_FORMAT, locale).entrySet()) {
|
|
272 |
map.put((long)toWeekDay(entry.getValue()), entry.getKey());
|
|
273 |
}
|
|
274 |
styleMap.put(TextStyle.SHORT, map);
|
|
275 |
map = new HashMap<>();
|
|
276 |
for (int wday = Calendar.SUNDAY; wday <= Calendar.SATURDAY; wday++) {
|
|
277 |
map.put((long)toWeekDay(wday),
|
|
278 |
provider.getDisplayName("gregory", Calendar.DAY_OF_WEEK, wday, Calendar.NARROW_FORMAT, locale));
|
|
279 |
}
|
|
280 |
styleMap.put(TextStyle.NARROW, map);
|
|
281 |
return new LocaleStore(styleMap);
|
|
282 |
}
|
|
283 |
if (field == AMPM_OF_DAY) {
|
|
284 |
Map<Long, String> map = new HashMap<>();
|
|
285 |
for (Entry<String, Integer> entry :
|
|
286 |
provider.getDisplayNames("gregory", Calendar.AM_PM, Calendar.LONG_FORMAT, locale).entrySet()) {
|
|
287 |
map.put((long)entry.getValue(), entry.getKey());
|
|
288 |
}
|
|
289 |
styleMap.put(TextStyle.FULL, map);
|
|
290 |
styleMap.put(TextStyle.SHORT, map); // re-use, as we don't have different data
|
|
291 |
return new LocaleStore(styleMap);
|
|
292 |
}
|
|
293 |
return ""; // null marker for map
|
|
294 |
}
|
|
295 |
|
|
296 |
/**
|
|
297 |
* Helper method to create an immutable entry.
|
|
298 |
*
|
|
299 |
* @param text the text, not null
|
|
300 |
* @param field the field, not null
|
|
301 |
* @return the entry, not null
|
|
302 |
*/
|
|
303 |
private static <A, B> Entry<A, B> createEntry(A text, B field) {
|
|
304 |
return new SimpleImmutableEntry<>(text, field);
|
|
305 |
}
|
|
306 |
|
|
307 |
/**
|
|
308 |
* Stores the text for a single locale.
|
|
309 |
* <p>
|
|
310 |
* Some fields have a textual representation, such as day-of-week or month-of-year.
|
|
311 |
* These textual representations can be captured in this class for printing
|
|
312 |
* and parsing.
|
|
313 |
* <p>
|
|
314 |
* This class is immutable and thread-safe.
|
|
315 |
*/
|
|
316 |
static final class LocaleStore {
|
|
317 |
/**
|
|
318 |
* Map of value to text.
|
|
319 |
*/
|
|
320 |
private final Map<TextStyle, Map<Long, String>> valueTextMap;
|
|
321 |
/**
|
|
322 |
* Parsable data.
|
|
323 |
*/
|
|
324 |
private final Map<TextStyle, List<Entry<String, Long>>> parsable;
|
|
325 |
|
|
326 |
/**
|
|
327 |
* Constructor.
|
|
328 |
*
|
|
329 |
* @param valueTextMap the map of values to text to store, assigned and not altered, not null
|
|
330 |
*/
|
|
331 |
LocaleStore(Map<TextStyle, Map<Long, String>> valueTextMap) {
|
|
332 |
this.valueTextMap = valueTextMap;
|
|
333 |
Map<TextStyle, List<Entry<String, Long>>> map = new HashMap<>();
|
|
334 |
List<Entry<String, Long>> allList = new ArrayList<>();
|
|
335 |
for (TextStyle style : valueTextMap.keySet()) {
|
|
336 |
Map<String, Entry<String, Long>> reverse = new HashMap<>();
|
|
337 |
for (Map.Entry<Long, String> entry : valueTextMap.get(style).entrySet()) {
|
|
338 |
if (reverse.put(entry.getValue(), createEntry(entry.getValue(), entry.getKey())) != null) {
|
|
339 |
// TODO: BUG: this has no effect
|
|
340 |
continue; // not parsable, try next style
|
|
341 |
}
|
|
342 |
}
|
|
343 |
List<Entry<String, Long>> list = new ArrayList<>(reverse.values());
|
|
344 |
Collections.sort(list, COMPARATOR);
|
|
345 |
map.put(style, list);
|
|
346 |
allList.addAll(list);
|
|
347 |
map.put(null, allList);
|
|
348 |
}
|
|
349 |
Collections.sort(allList, COMPARATOR);
|
|
350 |
this.parsable = map;
|
|
351 |
}
|
|
352 |
|
|
353 |
/**
|
|
354 |
* Gets the text for the specified field value, locale and style
|
|
355 |
* for the purpose of printing.
|
|
356 |
*
|
|
357 |
* @param value the value to get text for, not null
|
|
358 |
* @param style the style to get text for, not null
|
|
359 |
* @return the text for the field value, null if no text found
|
|
360 |
*/
|
|
361 |
String getText(long value, TextStyle style) {
|
|
362 |
Map<Long, String> map = valueTextMap.get(style);
|
|
363 |
return map != null ? map.get(value) : null;
|
|
364 |
}
|
|
365 |
|
|
366 |
/**
|
|
367 |
* Gets an iterator of text to field for the specified style for the purpose of parsing.
|
|
368 |
* <p>
|
|
369 |
* The iterator must be returned in order from the longest text to the shortest.
|
|
370 |
*
|
|
371 |
* @param style the style to get text for, null for all parsable text
|
|
372 |
* @return the iterator of text to field pairs, in order from longest text to shortest text,
|
|
373 |
* null if the style is not parsable
|
|
374 |
*/
|
|
375 |
Iterator<Entry<String, Long>> getTextIterator(TextStyle style) {
|
|
376 |
List<Entry<String, Long>> list = parsable.get(style);
|
|
377 |
return list != null ? list.iterator() : null;
|
|
378 |
}
|
|
379 |
}
|
|
380 |
}
|