src/java.base/share/classes/sun/util/locale/provider/TimeZoneNameProviderImpl.java

Print this page


   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 package sun.util.locale.provider;
  27 
  28 import java.util.Locale;
  29 import java.util.Objects;
  30 import java.util.Set;
  31 import java.util.TimeZone;
  32 import java.util.spi.TimeZoneNameProvider;

  33 
  34 /**
  35  * Concrete implementation of the
  36  * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider} class
  37  * for the JRE LocaleProviderAdapter.
  38  *
  39  * @author Naoto Sato
  40  * @author Masayoshi Okutsu
  41  */
  42 public class TimeZoneNameProviderImpl extends TimeZoneNameProvider {
  43     private final LocaleProviderAdapter.Type type;
  44     private final Set<String> langtags;

  45 
  46     TimeZoneNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  47         this.type = type;
  48         this.langtags = langtags;
  49     }
  50 
  51     /**
  52      * Returns an array of all locales for which this locale service provider
  53      * can provide localized objects or names.
  54      *
  55      * @return An array of all locales for which this locale service provider
  56      * can provide localized objects or names.
  57      */
  58     @Override
  59     public Locale[] getAvailableLocales() {
  60         return LocaleProviderAdapter.toLocaleArray(langtags);
  61     }
  62 
  63     @Override
  64     public boolean isSupportedLocale(Locale locale) {
  65         return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
  66     }
  67 
  68     /**
  69      * Returns a name for the given time zone ID that's suitable for
  70      * presentation to the user in the specified locale. The given time
  71      * zone ID is "GMT" or one of the names defined using "Zone" entries
  72      * in the "tz database", a public domain time zone database at
  73      * <a href="ftp://elsie.nci.nih.gov/pub/">ftp://elsie.nci.nih.gov/pub/</a>.
  74      * The data of this database is contained in a file whose name starts with
  75      * "tzdata", and the specification of the data format is part of the zic.8
  76      * man page, which is contained in a file whose name starts with "tzcode".
  77      * <p>
  78      * If <code>daylight</code> is true, the method should return a name
  79      * appropriate for daylight saving time even if the specified time zone
  80      * has not observed daylight saving time in the past.
  81      *
  82      * @param id a time zone ID string
  83      * @param daylight if true, return the daylight saving name.
  84      * @param style either {@link java.util.TimeZone#LONG TimeZone.LONG} or
  85      *    {@link java.util.TimeZone#SHORT TimeZone.SHORT}


 104                 index++;
 105             }
 106             return names[index];
 107         }
 108         return null;
 109     }
 110 
 111     @Override
 112     public String getGenericDisplayName(String id, int style, Locale locale) {
 113         String[] names = getDisplayNameArray(id, locale);
 114         if (Objects.nonNull(names)) {
 115             assert names.length >= 7;
 116             return names[(style == TimeZone.LONG) ? 5 : 6];
 117         }
 118         return null;
 119     }
 120 
 121     private String[] getDisplayNameArray(String id, Locale locale) {
 122         Objects.requireNonNull(id);
 123         Objects.requireNonNull(locale);
 124         return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getTimeZoneNames(id);





















 125     }
 126 
 127     /**
 128      * Returns a String[][] as the DateFormatSymbols.getZoneStrings() value for
 129      * the given locale. This method is package private.
 130      *
 131      * @param locale a Locale for time zone names
 132      * @return an array of time zone names arrays
 133      */
 134     String[][] getZoneStrings(Locale locale) {
 135         return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getZoneStrings();
 136     }
 137 }
   1 /*
   2  * Copyright (c) 2012, 2015, 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 package sun.util.locale.provider;
  27 
  28 import java.util.Locale;
  29 import java.util.Objects;
  30 import java.util.Set;
  31 import java.util.TimeZone;
  32 import java.util.spi.TimeZoneNameProvider;
  33 import sun.util.calendar.ZoneInfoFile;
  34 
  35 /**
  36  * Concrete implementation of the
  37  * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider} class
  38  * for the JRE LocaleProviderAdapter.
  39  *
  40  * @author Naoto Sato
  41  * @author Masayoshi Okutsu
  42  */
  43 public class TimeZoneNameProviderImpl extends TimeZoneNameProvider {
  44     private final LocaleProviderAdapter.Type type;
  45     private final Set<String> langtags;
  46     private static final String CLDR_NO_INHERITANCE_MARKER = "\u2205\u2205\u2205";
  47 
  48     TimeZoneNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  49         this.type = type;
  50         this.langtags = langtags;
  51     }
  52 
  53     /**
  54      * Returns an array of all locales for which this locale service provider
  55      * can provide localized objects or names.
  56      *
  57      * @return An array of all locales for which this locale service provider
  58      * can provide localized objects or names.
  59      */
  60     @Override
  61     public Locale[] getAvailableLocales() {
  62         return LocaleProviderAdapter.toLocaleArray(langtags);
  63     }
  64 
  65     @Override
  66     public boolean isSupportedLocale(Locale locale) {
  67         return LocaleProviderAdapter.forType(type).isSupportedProviderLocale(locale, langtags);
  68     }
  69 
  70     /**
  71      * Returns a name for the given time zone ID that's suitable for
  72      * presentation to the user in the specified locale. The given time
  73      * zone ID is "GMT" or one of the names defined using "Zone" entries
  74      * in the "tz database", a public domain time zone database at
  75      * <a href="ftp://elsie.nci.nih.gov/pub/">ftp://elsie.nci.nih.gov/pub/</a>.
  76      * The data of this database is contained in a file whose name starts with
  77      * "tzdata", and the specification of the data format is part of the zic.8
  78      * man page, which is contained in a file whose name starts with "tzcode".
  79      * <p>
  80      * If <code>daylight</code> is true, the method should return a name
  81      * appropriate for daylight saving time even if the specified time zone
  82      * has not observed daylight saving time in the past.
  83      *
  84      * @param id a time zone ID string
  85      * @param daylight if true, return the daylight saving name.
  86      * @param style either {@link java.util.TimeZone#LONG TimeZone.LONG} or
  87      *    {@link java.util.TimeZone#SHORT TimeZone.SHORT}


 106                 index++;
 107             }
 108             return names[index];
 109         }
 110         return null;
 111     }
 112 
 113     @Override
 114     public String getGenericDisplayName(String id, int style, Locale locale) {
 115         String[] names = getDisplayNameArray(id, locale);
 116         if (Objects.nonNull(names)) {
 117             assert names.length >= 7;
 118             return names[(style == TimeZone.LONG) ? 5 : 6];
 119         }
 120         return null;
 121     }
 122 
 123     private String[] getDisplayNameArray(String id, Locale locale) {
 124         Objects.requireNonNull(id);
 125         Objects.requireNonNull(locale);
 126 
 127         String[] ret =
 128             LocaleProviderAdapter.forType(type).getLocaleResources(locale).getTimeZoneNames(id);
 129 
 130         if (Objects.nonNull(ret) && type == LocaleProviderAdapter.Type.CLDR) {
 131             // check for CLDR's "no inheritance marker"
 132             for (int index = 0; index < ret.length; index++) {
 133                 TimeZone tz = null;
 134                 if (CLDR_NO_INHERITANCE_MARKER.equals(ret[index])) {
 135                     if (Objects.isNull(tz)) {
 136                         tz = TimeZone.getTimeZone(id);
 137                     }
 138                     int offset = tz.getRawOffset();
 139                     if (index == 3 || index == 4) { // daylight
 140                         offset += tz.getDSTSavings();
 141                     }
 142                     ret[index] = ZoneInfoFile.toCustomID(offset);
 143                 }
 144             }
 145         }
 146 
 147         return ret;
 148     }
 149 
 150     /**
 151      * Returns a String[][] as the DateFormatSymbols.getZoneStrings() value for
 152      * the given locale. This method is package private.
 153      *
 154      * @param locale a Locale for time zone names
 155      * @return an array of time zone names arrays
 156      */
 157     String[][] getZoneStrings(Locale locale) {
 158         return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getZoneStrings();
 159     }
 160 }