Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/format/DateTimeTextProvider.java
38918 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time.format;6263import static java.time.temporal.ChronoField.AMPM_OF_DAY;64import static java.time.temporal.ChronoField.DAY_OF_WEEK;65import static java.time.temporal.ChronoField.ERA;66import static java.time.temporal.ChronoField.MONTH_OF_YEAR;6768import java.time.chrono.Chronology;69import java.time.chrono.IsoChronology;70import java.time.chrono.JapaneseChronology;71import java.time.temporal.ChronoField;72import java.time.temporal.IsoFields;73import java.time.temporal.TemporalField;74import java.util.AbstractMap.SimpleImmutableEntry;75import java.util.ArrayList;76import java.util.Calendar;77import java.util.Collections;78import java.util.Comparator;79import java.util.HashMap;80import java.util.Iterator;81import java.util.List;82import java.util.Locale;83import java.util.Map;84import java.util.Map.Entry;85import java.util.ResourceBundle;86import java.util.concurrent.ConcurrentHashMap;87import java.util.concurrent.ConcurrentMap;8889import sun.util.locale.provider.CalendarDataUtility;90import sun.util.locale.provider.LocaleProviderAdapter;91import sun.util.locale.provider.LocaleResources;9293/**94* A provider to obtain the textual form of a date-time field.95*96* @implSpec97* Implementations must be thread-safe.98* Implementations should cache the textual information.99*100* @since 1.8101*/102class DateTimeTextProvider {103104/** Cache. */105private static final ConcurrentMap<Entry<TemporalField, Locale>, Object> CACHE = new ConcurrentHashMap<>(16, 0.75f, 2);106/** Comparator. */107private static final Comparator<Entry<String, Long>> COMPARATOR = new Comparator<Entry<String, Long>>() {108@Override109public int compare(Entry<String, Long> obj1, Entry<String, Long> obj2) {110return obj2.getKey().length() - obj1.getKey().length(); // longest to shortest111}112};113114DateTimeTextProvider() {}115116/**117* Gets the provider of text.118*119* @return the provider, not null120*/121static DateTimeTextProvider getInstance() {122return new DateTimeTextProvider();123}124125/**126* Gets the text for the specified field, locale and style127* for the purpose of formatting.128* <p>129* The text associated with the value is returned.130* The null return value should be used if there is no applicable text, or131* if the text would be a numeric representation of the value.132*133* @param field the field to get text for, not null134* @param value the field value to get text for, not null135* @param style the style to get text for, not null136* @param locale the locale to get text for, not null137* @return the text for the field value, null if no text found138*/139public String getText(TemporalField field, long value, TextStyle style, Locale locale) {140Object store = findStore(field, locale);141if (store instanceof LocaleStore) {142return ((LocaleStore) store).getText(value, style);143}144return null;145}146147/**148* Gets the text for the specified chrono, field, locale and style149* for the purpose of formatting.150* <p>151* The text associated with the value is returned.152* The null return value should be used if there is no applicable text, or153* if the text would be a numeric representation of the value.154*155* @param chrono the Chronology to get text for, not null156* @param field the field to get text for, not null157* @param value the field value to get text for, not null158* @param style the style to get text for, not null159* @param locale the locale to get text for, not null160* @return the text for the field value, null if no text found161*/162public String getText(Chronology chrono, TemporalField field, long value,163TextStyle style, Locale locale) {164if (chrono == IsoChronology.INSTANCE165|| !(field instanceof ChronoField)) {166return getText(field, value, style, locale);167}168169int fieldIndex;170int fieldValue;171if (field == ERA) {172fieldIndex = Calendar.ERA;173if (chrono == JapaneseChronology.INSTANCE) {174if (value == -999) {175fieldValue = 0;176} else {177fieldValue = (int) value + 2;178}179} else {180fieldValue = (int) value;181}182} else if (field == MONTH_OF_YEAR) {183fieldIndex = Calendar.MONTH;184fieldValue = (int) value - 1;185} else if (field == DAY_OF_WEEK) {186fieldIndex = Calendar.DAY_OF_WEEK;187fieldValue = (int) value + 1;188if (fieldValue > 7) {189fieldValue = Calendar.SUNDAY;190}191} else if (field == AMPM_OF_DAY) {192fieldIndex = Calendar.AM_PM;193fieldValue = (int) value;194} else {195return null;196}197return CalendarDataUtility.retrieveJavaTimeFieldValueName(198chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);199}200201/**202* Gets an iterator of text to field for the specified field, locale and style203* for the purpose of parsing.204* <p>205* The iterator must be returned in order from the longest text to the shortest.206* <p>207* The null return value should be used if there is no applicable parsable text, or208* if the text would be a numeric representation of the value.209* Text can only be parsed if all the values for that field-style-locale combination are unique.210*211* @param field the field to get text for, not null212* @param style the style to get text for, null for all parsable text213* @param locale the locale to get text for, not null214* @return the iterator of text to field pairs, in order from longest text to shortest text,215* null if the field or style is not parsable216*/217public Iterator<Entry<String, Long>> getTextIterator(TemporalField field, TextStyle style, Locale locale) {218Object store = findStore(field, locale);219if (store instanceof LocaleStore) {220return ((LocaleStore) store).getTextIterator(style);221}222return null;223}224225/**226* Gets an iterator of text to field for the specified chrono, field, locale and style227* for the purpose of parsing.228* <p>229* The iterator must be returned in order from the longest text to the shortest.230* <p>231* The null return value should be used if there is no applicable parsable text, or232* if the text would be a numeric representation of the value.233* Text can only be parsed if all the values for that field-style-locale combination are unique.234*235* @param chrono the Chronology to get text for, not null236* @param field the field to get text for, not null237* @param style the style to get text for, null for all parsable text238* @param locale the locale to get text for, not null239* @return the iterator of text to field pairs, in order from longest text to shortest text,240* null if the field or style is not parsable241*/242public Iterator<Entry<String, Long>> getTextIterator(Chronology chrono, TemporalField field,243TextStyle style, Locale locale) {244if (chrono == IsoChronology.INSTANCE245|| !(field instanceof ChronoField)) {246return getTextIterator(field, style, locale);247}248249int fieldIndex;250switch ((ChronoField)field) {251case ERA:252fieldIndex = Calendar.ERA;253break;254case MONTH_OF_YEAR:255fieldIndex = Calendar.MONTH;256break;257case DAY_OF_WEEK:258fieldIndex = Calendar.DAY_OF_WEEK;259break;260case AMPM_OF_DAY:261fieldIndex = Calendar.AM_PM;262break;263default:264return null;265}266267int calendarStyle = (style == null) ? Calendar.ALL_STYLES : style.toCalendarStyle();268Map<String, Integer> map = CalendarDataUtility.retrieveJavaTimeFieldValueNames(269chrono.getCalendarType(), fieldIndex, calendarStyle, locale);270if (map == null) {271return null;272}273List<Entry<String, Long>> list = new ArrayList<>(map.size());274switch (fieldIndex) {275case Calendar.ERA:276for (Map.Entry<String, Integer> entry : map.entrySet()) {277int era = entry.getValue();278if (chrono == JapaneseChronology.INSTANCE) {279if (era == 0) {280era = -999;281} else {282era -= 2;283}284}285list.add(createEntry(entry.getKey(), (long)era));286}287break;288case Calendar.MONTH:289for (Map.Entry<String, Integer> entry : map.entrySet()) {290list.add(createEntry(entry.getKey(), (long)(entry.getValue() + 1)));291}292break;293case Calendar.DAY_OF_WEEK:294for (Map.Entry<String, Integer> entry : map.entrySet()) {295list.add(createEntry(entry.getKey(), (long)toWeekDay(entry.getValue())));296}297break;298default:299for (Map.Entry<String, Integer> entry : map.entrySet()) {300list.add(createEntry(entry.getKey(), (long)entry.getValue()));301}302break;303}304return list.iterator();305}306307private Object findStore(TemporalField field, Locale locale) {308Entry<TemporalField, Locale> key = createEntry(field, locale);309Object store = CACHE.get(key);310if (store == null) {311store = createStore(field, locale);312CACHE.putIfAbsent(key, store);313store = CACHE.get(key);314}315return store;316}317318private static int toWeekDay(int calWeekDay) {319if (calWeekDay == Calendar.SUNDAY) {320return 7;321} else {322return calWeekDay - 1;323}324}325326private Object createStore(TemporalField field, Locale locale) {327Map<TextStyle, Map<Long, String>> styleMap = new HashMap<>();328if (field == ERA) {329for (TextStyle textStyle : TextStyle.values()) {330if (textStyle.isStandalone()) {331// Stand-alone isn't applicable to era names.332continue;333}334Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(335"gregory", Calendar.ERA, textStyle.toCalendarStyle(), locale);336if (displayNames != null) {337Map<Long, String> map = new HashMap<>();338for (Entry<String, Integer> entry : displayNames.entrySet()) {339map.put((long) entry.getValue(), entry.getKey());340}341if (!map.isEmpty()) {342styleMap.put(textStyle, map);343}344}345}346return new LocaleStore(styleMap);347}348349if (field == MONTH_OF_YEAR) {350for (TextStyle textStyle : TextStyle.values()) {351Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(352"gregory", Calendar.MONTH, textStyle.toCalendarStyle(), locale);353Map<Long, String> map = new HashMap<>();354if (displayNames != null) {355for (Entry<String, Integer> entry : displayNames.entrySet()) {356map.put((long) (entry.getValue() + 1), entry.getKey());357}358359} else {360// Narrow names may have duplicated names, such as "J" for January, Jun, July.361// Get names one by one in that case.362for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {363String name;364name = CalendarDataUtility.retrieveJavaTimeFieldValueName(365"gregory", Calendar.MONTH, month, textStyle.toCalendarStyle(), locale);366if (name == null) {367break;368}369map.put((long) (month + 1), name);370}371}372if (!map.isEmpty()) {373styleMap.put(textStyle, map);374}375}376return new LocaleStore(styleMap);377}378379if (field == DAY_OF_WEEK) {380for (TextStyle textStyle : TextStyle.values()) {381Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(382"gregory", Calendar.DAY_OF_WEEK, textStyle.toCalendarStyle(), locale);383Map<Long, String> map = new HashMap<>();384if (displayNames != null) {385for (Entry<String, Integer> entry : displayNames.entrySet()) {386map.put((long)toWeekDay(entry.getValue()), entry.getKey());387}388389} else {390// Narrow names may have duplicated names, such as "S" for Sunday and Saturday.391// Get names one by one in that case.392for (int wday = Calendar.SUNDAY; wday <= Calendar.SATURDAY; wday++) {393String name;394name = CalendarDataUtility.retrieveJavaTimeFieldValueName(395"gregory", Calendar.DAY_OF_WEEK, wday, textStyle.toCalendarStyle(), locale);396if (name == null) {397break;398}399map.put((long)toWeekDay(wday), name);400}401}402if (!map.isEmpty()) {403styleMap.put(textStyle, map);404}405}406return new LocaleStore(styleMap);407}408409if (field == AMPM_OF_DAY) {410for (TextStyle textStyle : TextStyle.values()) {411if (textStyle.isStandalone()) {412// Stand-alone isn't applicable to AM/PM.413continue;414}415Map<String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames(416"gregory", Calendar.AM_PM, textStyle.toCalendarStyle(), locale);417if (displayNames != null) {418Map<Long, String> map = new HashMap<>();419for (Entry<String, Integer> entry : displayNames.entrySet()) {420map.put((long) entry.getValue(), entry.getKey());421}422if (!map.isEmpty()) {423styleMap.put(textStyle, map);424}425}426}427return new LocaleStore(styleMap);428}429430if (field == IsoFields.QUARTER_OF_YEAR) {431// The order of keys must correspond to the TextStyle.values() order.432final String[] keys = {433"QuarterNames",434"standalone.QuarterNames",435"QuarterAbbreviations",436"standalone.QuarterAbbreviations",437"QuarterNarrows",438"standalone.QuarterNarrows",439};440for (int i = 0; i < keys.length; i++) {441String[] names = getLocalizedResource(keys[i], locale);442if (names != null) {443Map<Long, String> map = new HashMap<>();444for (int q = 0; q < names.length; q++) {445map.put((long) (q + 1), names[q]);446}447styleMap.put(TextStyle.values()[i], map);448}449}450return new LocaleStore(styleMap);451}452453return ""; // null marker for map454}455456/**457* Helper method to create an immutable entry.458*459* @param text the text, not null460* @param field the field, not null461* @return the entry, not null462*/463private static <A, B> Entry<A, B> createEntry(A text, B field) {464return new SimpleImmutableEntry<>(text, field);465}466467/**468* Returns the localized resource of the given key and locale, or null469* if no localized resource is available.470*471* @param key the key of the localized resource, not null472* @param locale the locale, not null473* @return the localized resource, or null if not available474* @throws NullPointerException if key or locale is null475*/476@SuppressWarnings("unchecked")477static <T> T getLocalizedResource(String key, Locale locale) {478LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()479.getLocaleResources(locale);480ResourceBundle rb = lr.getJavaTimeFormatData();481return rb.containsKey(key) ? (T) rb.getObject(key) : null;482}483484/**485* Stores the text for a single locale.486* <p>487* Some fields have a textual representation, such as day-of-week or month-of-year.488* These textual representations can be captured in this class for printing489* and parsing.490* <p>491* This class is immutable and thread-safe.492*/493static final class LocaleStore {494/**495* Map of value to text.496*/497private final Map<TextStyle, Map<Long, String>> valueTextMap;498/**499* Parsable data.500*/501private final Map<TextStyle, List<Entry<String, Long>>> parsable;502503/**504* Constructor.505*506* @param valueTextMap the map of values to text to store, assigned and not altered, not null507*/508LocaleStore(Map<TextStyle, Map<Long, String>> valueTextMap) {509this.valueTextMap = valueTextMap;510Map<TextStyle, List<Entry<String, Long>>> map = new HashMap<>();511List<Entry<String, Long>> allList = new ArrayList<>();512for (Map.Entry<TextStyle, Map<Long, String>> vtmEntry : valueTextMap.entrySet()) {513Map<String, Entry<String, Long>> reverse = new HashMap<>();514for (Map.Entry<Long, String> entry : vtmEntry.getValue().entrySet()) {515if (reverse.put(entry.getValue(), createEntry(entry.getValue(), entry.getKey())) != null) {516// TODO: BUG: this has no effect517continue; // not parsable, try next style518}519}520List<Entry<String, Long>> list = new ArrayList<>(reverse.values());521Collections.sort(list, COMPARATOR);522map.put(vtmEntry.getKey(), list);523allList.addAll(list);524map.put(null, allList);525}526Collections.sort(allList, COMPARATOR);527this.parsable = map;528}529530/**531* Gets the text for the specified field value, locale and style532* for the purpose of printing.533*534* @param value the value to get text for, not null535* @param style the style to get text for, not null536* @return the text for the field value, null if no text found537*/538String getText(long value, TextStyle style) {539Map<Long, String> map = valueTextMap.get(style);540return map != null ? map.get(value) : null;541}542543/**544* Gets an iterator of text to field for the specified style for the purpose of parsing.545* <p>546* The iterator must be returned in order from the longest text to the shortest.547*548* @param style the style to get text for, null for all parsable text549* @return the iterator of text to field pairs, in order from longest text to shortest text,550* null if the style is not parsable551*/552Iterator<Entry<String, Long>> getTextIterator(TextStyle style) {553List<Entry<String, Long>> list = parsable.get(style);554return list != null ? list.iterator() : null;555}556}557}558559560