jdk/src/share/classes/sun/util/locale/provider/TimeZoneNameProviderImpl.java
author sherman
Tue, 12 Feb 2013 09:25:43 -0800
changeset 15658 55b829ca2334
parent 15260 7af2d7a87806
child 23010 6dadb192ad81
permissions -rw-r--r--
8007392: JSR 310: DateTime API Updates 8007520: Update date/time classes in j.util and j.sql packages 8007572: Replace existing jdk timezone data at <java.home>/lib/zi with JSR310's tzdb Summary: Integration of JSR310 Date/Time API for M7 Reviewed-by: darcy, alanb, naoto Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, masayoshi.okutsu@oracle.com, patrick.zhang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     1
/*
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     4
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    10
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    15
 * accompanied this code).
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    16
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    20
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    23
 * questions.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    24
 */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    25
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    26
package sun.util.locale.provider;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    27
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    28
import java.util.Locale;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    29
import java.util.Set;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    30
import java.util.TimeZone;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    31
import java.util.spi.TimeZoneNameProvider;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    32
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    33
/**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    34
 * Concrete implementation of the
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    35
 * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider} class
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    36
 * for the JRE LocaleProviderAdapter.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    37
 *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    38
 * @author Naoto Sato
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    39
 * @author Masayoshi Okutsu
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    40
 */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    41
public class TimeZoneNameProviderImpl extends TimeZoneNameProvider {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    42
    private final LocaleProviderAdapter.Type type;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    43
    private final Set<String> langtags;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    44
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    45
    TimeZoneNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    46
        this.type = type;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    47
        this.langtags = langtags;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    48
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    49
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    50
    /**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    51
     * Returns an array of all locales for which this locale service provider
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    52
     * can provide localized objects or names.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    53
     *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    54
     * @return An array of all locales for which this locale service provider
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    55
     * can provide localized objects or names.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    56
     */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    57
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    58
    public Locale[] getAvailableLocales() {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    59
        return LocaleProviderAdapter.toLocaleArray(langtags);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    60
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    61
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    62
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    63
    public boolean isSupportedLocale(Locale locale) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    64
        return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    65
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    66
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    67
    /**
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    68
     * Returns a name for the given time zone ID that's suitable for
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    69
     * presentation to the user in the specified locale. The given time
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    70
     * zone ID is "GMT" or one of the names defined using "Zone" entries
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    71
     * in the "tz database", a public domain time zone database at
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    72
     * <a href="ftp://elsie.nci.nih.gov/pub/">ftp://elsie.nci.nih.gov/pub/</a>.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    73
     * The data of this database is contained in a file whose name starts with
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    74
     * "tzdata", and the specification of the data format is part of the zic.8
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    75
     * man page, which is contained in a file whose name starts with "tzcode".
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    76
     * <p>
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    77
     * If <code>daylight</code> is true, the method should return a name
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    78
     * appropriate for daylight saving time even if the specified time zone
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    79
     * has not observed daylight saving time in the past.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    80
     *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    81
     * @param ID a time zone ID string
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    82
     * @param daylight if true, return the daylight saving name.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    83
     * @param style either {@link java.util.TimeZone#LONG TimeZone.LONG} or
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    84
     *    {@link java.util.TimeZone#SHORT TimeZone.SHORT}
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    85
     * @param locale the desired locale
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    86
     * @return the human-readable name of the given time zone in the
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    87
     *     given locale, or null if it's not available.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    88
     * @exception IllegalArgumentException if <code>style</code> is invalid,
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    89
     *     or <code>locale</code> isn't one of the locales returned from
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    90
     *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    91
     *     getAvailableLocales()}.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    92
     * @exception NullPointerException if <code>ID</code> or <code>locale</code>
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    93
     *     is null
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    94
     * @see java.util.TimeZone#getDisplayName(boolean, int, java.util.Locale)
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    95
     */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    96
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
    97
    public String getDisplayName(String id, boolean daylight, int style, Locale locale) {
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
    98
        String[] names = getDisplayNameArray(id, 5, locale);
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
    99
        if (names != null) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   100
            int index = daylight ? 3 : 1;
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   101
            if (style == TimeZone.SHORT) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   102
                index++;
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   103
            }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   104
            return names[index];
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   105
        }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   106
        return null;
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   107
    }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   108
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   109
    @Override
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   110
    public String getGenericDisplayName(String id, int style, Locale locale) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   111
        String[] names = getDisplayNameArray(id, 7, locale);
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   112
        if (names != null && names.length >= 7) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   113
            return names[(style == TimeZone.LONG) ? 5 : 6];
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   114
        }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   115
        return null;
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   116
    }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   117
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   118
    private String[] getDisplayNameArray(String id, int n, Locale locale) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   119
        if (id == null || locale == null) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   120
            throw new NullPointerException();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   121
        }
15260
7af2d7a87806 7162007: Clean up i18n related caches
naoto
parents: 14765
diff changeset
   122
        return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getTimeZoneNames(id, n);
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   123
    }
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   124
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   125
    /**
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   126
     * Returns a String[][] as the DateFormatSymbols.getZoneStrings() value for
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   127
     * the given locale. This method is package private.
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   128
     *
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   129
     * @param locale a Locale for time zone names
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   130
     * @return an array of time zone names arrays
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   131
     */
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14185
diff changeset
   132
    String[][] getZoneStrings(Locale locale) {
15260
7af2d7a87806 7162007: Clean up i18n related caches
naoto
parents: 14765
diff changeset
   133
        return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getZoneStrings();
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   134
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents:
diff changeset
   135
}