jdk/src/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java
author okutsu
Mon, 10 Dec 2012 10:52:11 +0900
changeset 14765 0987999ed367
parent 14502 d63fed06fed4
child 15260 7af2d7a87806
permissions -rw-r--r--
8000983: Support narrow display names for calendar fields 8003267: Support generic time zone names in TimeZoneNameProvider (SPI) Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
     1
/*
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
     4
 *
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    10
 *
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    15
 * accompanied this code).
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    16
 *
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    20
 *
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    23
 * questions.
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    24
 */
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    25
package sun.util.locale.provider;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    26
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    27
import static java.util.Calendar.*;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    28
import java.util.Comparator;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    29
import java.util.Locale;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    30
import java.util.Map;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    31
import java.util.ResourceBundle;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    32
import java.util.Set;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    33
import java.util.TreeMap;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    34
import java.util.spi.CalendarNameProvider;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    35
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    36
/**
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    37
 * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    38
 * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    39
 *
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    40
 * @author Masayoshi Okutsu
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    41
 * @author Naoto Sato
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    42
 */
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    43
public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    44
    private final LocaleProviderAdapter.Type type;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    45
    private final Set<String> langtags;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    46
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    47
    public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    48
        this.type = type;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    49
        this.langtags = langtags;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    50
    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    51
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    52
    @Override
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    53
    public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    54
        String name = null;
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    55
        String key = getResourceKey(calendarType, field, style);
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    56
        if (key != null) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    57
            ResourceBundle rb = LocaleProviderAdapter.forType(type).getLocaleData().getDateFormatData(locale);
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    58
            if (rb.containsKey(key)) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    59
                String[] strings = rb.getStringArray(key);
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    60
                if (strings.length > 0) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    61
                    if (field == DAY_OF_WEEK || field == YEAR) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    62
                        --value;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    63
                    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    64
                    name = strings[value];
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    65
                    // If name is empty in standalone, try its `format' style.
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    66
                    if (name.length() == 0
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    67
                            && (style == SHORT_STANDALONE || style == LONG_STANDALONE
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    68
                                || style == NARROW_STANDALONE)) {
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    69
                        name = getDisplayName(calendarType, field, value,
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    70
                                              getBaseStyle(style),
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    71
                                              locale);
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    72
                    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    73
                }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    74
            }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    75
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    76
        return name;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    77
    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    78
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    79
    private static int[] REST_OF_STYLES = {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    80
        SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    81
        NARROW_FORMAT, NARROW_STANDALONE
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    82
    };
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    83
    @Override
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    84
    public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    85
        Map<String, Integer> names;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    86
        if (style == ALL_STYLES) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    87
            names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale);
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    88
            for (int st : REST_OF_STYLES) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
    89
                names.putAll(getDisplayNamesImpl(calendarType, field, st, locale));
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    90
            }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    91
        } else {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    92
            // specific style
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    93
            names = getDisplayNamesImpl(calendarType, field, style, locale);
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    94
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    95
        return names.isEmpty() ? null : names;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    96
    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    97
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    98
    private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
    99
                                                     int style, Locale locale) {
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   100
        String key = getResourceKey(calendarType, field, style);
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   101
        Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   102
        if (key != null) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   103
            ResourceBundle rb = LocaleProviderAdapter.forType(type).getLocaleData().getDateFormatData(locale);
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   104
            if (rb.containsKey(key)) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   105
                String[] strings = rb.getStringArray(key);
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   106
                if (!hasDuplicates(strings)) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   107
                    if (field == YEAR) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   108
                        if (strings.length > 0) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   109
                            map.put(strings[0], 1);
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   110
                        }
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   111
                    } else {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   112
                        int base = (field == DAY_OF_WEEK) ? 1 : 0;
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   113
                        for (int i = 0; i < strings.length; i++) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   114
                            String name = strings[i];
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   115
                            // Ignore any empty string (some standalone month names
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   116
                            // are not defined)
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   117
                            if (name.length() == 0) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   118
                                continue;
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   119
                            }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   120
                            map.put(name, base + i);
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   121
                        }
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   122
                    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   123
                }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   124
            }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   125
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   126
        return map;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   127
    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   128
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   129
    private int getBaseStyle(int style) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   130
        return style & ~(SHORT_STANDALONE - SHORT_FORMAT);
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   131
    }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   132
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   133
    /**
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   134
     * Comparator implementation for TreeMap which iterates keys from longest
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   135
     * to shortest.
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   136
     */
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   137
    private static class LengthBasedComparator implements Comparator<String> {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   138
        private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   139
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   140
        private LengthBasedComparator() {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   141
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   142
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   143
        @Override
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   144
        public int compare(String o1, String o2) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   145
            int n = o2.length() - o1.length();
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   146
            return (n == 0) ? o1.compareTo(o2) : n;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   147
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   148
    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   149
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   150
    @Override
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   151
    public Locale[] getAvailableLocales() {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   152
        return LocaleProviderAdapter.toLocaleArray(langtags);
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   153
    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   154
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   155
    @Override
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   156
    public boolean isSupportedLocale(Locale locale) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   157
        if (Locale.ROOT.equals(locale)) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   158
            return true;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   159
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   160
        String calendarType = null;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   161
        if (locale.hasExtensions()) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   162
            calendarType = locale.getUnicodeLocaleType("ca");
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   163
            locale = locale.stripExtensions();
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   164
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   165
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   166
        if (calendarType != null) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   167
            switch (calendarType) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   168
            case "buddhist":
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   169
            case "japanese":
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   170
            case "gregory":
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   171
                break;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   172
            default:
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   173
                // Unknown calendar type
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   174
                return false;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   175
            }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   176
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   177
        if (langtags.contains(locale.toLanguageTag())) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   178
            return true;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   179
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   180
        if (type == LocaleProviderAdapter.Type.JRE) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   181
            String oldname = locale.toString().replace('_', '-');
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   182
            return langtags.contains(oldname);
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   183
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   184
        return false;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   185
    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   186
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   187
    @Override
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   188
    public Set<String> getAvailableLanguageTags() {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   189
        return langtags;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   190
    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   191
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   192
    private boolean hasDuplicates(String[] strings) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   193
        int len = strings.length;
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   194
        for (int i = 0; i < len - 1; i++) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   195
            String a = strings[i];
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   196
            if (a != null) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   197
                for (int j = i + 1; j < len; j++) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   198
                    if (a.equals(strings[j]))  {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   199
                        return true;
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   200
                    }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   201
                }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   202
            }
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   203
        }
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   204
        return false;
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   205
    }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   206
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   207
    private String getResourceKey(String type, int field, int style) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   208
        int baseStyle = getBaseStyle(style);
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   209
        boolean isStandalone = (style != baseStyle);
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   210
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   211
        if ("gregory".equals(type)) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   212
            type = null;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   213
        }
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   214
        boolean isNarrow = (baseStyle == NARROW_FORMAT);
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   215
        StringBuilder key = new StringBuilder();
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   216
        switch (field) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   217
        case ERA:
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   218
            if (type != null) {
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   219
                key.append(type).append('.');
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   220
            }
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   221
            if (isNarrow) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   222
                key.append("narrow.");
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   223
            } else {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   224
                // JRE and CLDR use different resource key conventions
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   225
                // due to historical reasons. (JRE DateFormatSymbols.getEras returns
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   226
                // abbreviations while other getShort*() return abbreviations.)
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   227
                if (this.type == LocaleProviderAdapter.Type.JRE) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   228
                    if (baseStyle == SHORT) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   229
                        key.append("short.");
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   230
                    }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   231
                } else { // CLDR
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   232
                    if (baseStyle == LONG) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   233
                        key.append("long.");
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   234
                    }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   235
                }
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   236
            }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   237
            key.append("Eras");
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   238
            break;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   239
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   240
        case YEAR:
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   241
            if (!isNarrow) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   242
                key.append(type).append(".FirstYear");
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   243
            }
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   244
            break;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   245
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   246
        case MONTH:
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   247
            if (isStandalone) {
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   248
                key.append("standalone.");
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   249
            }
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   250
            key.append("Month").append(toStyleName(baseStyle));
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   251
            break;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   252
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   253
        case DAY_OF_WEEK:
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   254
            // support standalone narrow day names
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   255
            if (isStandalone && isNarrow) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   256
                key.append("standalone.");
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   257
            }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   258
            key.append("Day").append(toStyleName(baseStyle));
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   259
            break;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   260
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   261
        case AM_PM:
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   262
            if (isNarrow) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   263
                key.append("narrow.");
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   264
            }
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   265
            key.append("AmPmMarkers");
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   266
            break;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   267
        }
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   268
        return key.length() > 0 ? key.toString() : null;
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   269
    }
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   270
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   271
    private String toStyleName(int baseStyle) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   272
        switch (baseStyle) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   273
        case SHORT:
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   274
            return "Abbreviations";
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   275
        case NARROW_FORMAT:
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   276
            return "Narrows";
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   277
        }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   278
        return "Names";
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14502
diff changeset
   279
    }
14502
d63fed06fed4 8000986: Split java.util.spi.CalendarDataProvider into week parameters and field names portions
okutsu
parents:
diff changeset
   280
}