• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4  *******************************************************************************
5  * Copyright (C) 2011-2016, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  *******************************************************************************
8  */
9 package com.ibm.icu.text;
10 
11 import java.io.Serializable;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.EnumSet;
15 import java.util.Locale;
16 import java.util.Set;
17 
18 import com.ibm.icu.impl.ICUConfig;
19 import com.ibm.icu.impl.SoftCache;
20 import com.ibm.icu.impl.TZDBTimeZoneNames;
21 import com.ibm.icu.impl.TimeZoneNamesImpl;
22 import com.ibm.icu.util.TimeZone;
23 import com.ibm.icu.util.ULocale;
24 
25 /**
26  * <code>TimeZoneNames</code> is an abstract class representing the time zone display name data model defined
27  * by <a href="http://www.unicode.org/reports/tr35/">UTS#35 Unicode Locale Data Markup Language (LDML)</a>.
28  * The model defines meta zone, which is used for storing a set of display names. A meta zone can be shared
29  * by multiple time zones. Also a time zone may have multiple meta zone historic mappings.
30  * <p>
31  * For example, people in the United States refer the zone used by the east part of North America as "Eastern Time".
32  * The tz database contains multiple time zones "America/New_York", "America/Detroit", "America/Montreal" and some
33  * others that belong to "Eastern Time". However, assigning different display names to these time zones does not make
34  * much sense for most of people.
35  * <p>
36  * In <a href="http://cldr.unicode.org/">CLDR</a> (which uses LDML for representing locale data), the display name
37  * "Eastern Time" is stored as long generic display name of a meta zone identified by the ID "America_Eastern".
38  * Then, there is another table maintaining the historic mapping to meta zones for each time zone. The time zones in
39  * the above example ("America/New_York", "America/Detroit"...) are mapped to the meta zone "America_Eastern".
40  * <p>
41  * Sometimes, a time zone is mapped to a different time zone in the past. For example, "America/Indiana/Knox"
42  * had been moving "Eastern Time" and "Central Time" back and forth. Therefore, it is necessary that time zone
43  * to meta zones mapping data are stored by date range.
44  *
45  * <p><b>Note:</b>
46  * <p>
47  * {@link TimeZoneFormat} assumes an instance of <code>TimeZoneNames</code> is immutable. If you want to provide
48  * your own <code>TimeZoneNames</code> implementation and use it with {@link TimeZoneFormat}, you must follow
49  * the contract.
50  * <p>
51  * The methods in this class assume that time zone IDs are already canonicalized. For example, you may not get proper
52  * result returned by a method with time zone ID "America/Indiana/Indianapolis", because it's not a canonical time zone
53  * ID (the canonical time zone ID for the time zone is "America/Indianapolis". See
54  * {@link TimeZone#getCanonicalID(String)} about ICU canonical time zone IDs.
55  *
56  * <p>
57  * In CLDR, most of time zone display names except location names are provided through meta zones. But a time zone may
58  * have a specific name that is not shared with other time zones.
59  *
60  * For example, time zone "Europe/London" has English long name for standard time "Greenwich Mean Time", which is also
61  * shared with other time zones. However, the long name for daylight saving time is "British Summer Time", which is only
62  * used for "Europe/London".
63  *
64  * <p>
65  * {@link #getTimeZoneDisplayName(String, NameType)} is designed for accessing a name only used by a single time zone.
66  * But is not necessarily mean that a subclass implementation use the same model with CLDR. A subclass implementation
67  * may provide time zone names only through {@link #getTimeZoneDisplayName(String, NameType)}, or only through
68  * {@link #getMetaZoneDisplayName(String, NameType)}, or both.
69  *
70  * <p>
71  * The default <code>TimeZoneNames</code> implementation returned by {@link #getInstance(ULocale)} uses the locale data
72  * imported from CLDR. In CLDR, set of meta zone IDs and mappings between zone IDs and meta zone IDs are shared by all
73  * locales. Therefore, the behavior of {@link #getAvailableMetaZoneIDs()}, {@link #getAvailableMetaZoneIDs(String)},
74  * {@link #getMetaZoneID(String, long)}, and {@link #getReferenceZoneID(String, String)} won't be changed no matter
75  * what locale is used for getting an instance of <code>TimeZoneNames</code>.
76  *
77  * @stable ICU 49
78  */
79 public abstract class TimeZoneNames implements Serializable {
80 
81     private static final long serialVersionUID = -9180227029248969153L;
82 
83     /**
84      * Time zone display name types
85      *
86      * @stable ICU 49
87      */
88     public enum NameType {
89         /**
90          * Long display name, such as "Eastern Time".
91          *
92          * @stable ICU 49
93          */
94         LONG_GENERIC,
95         /**
96          * Long display name for standard time, such as "Eastern Standard Time".
97          *
98          * @stable ICU 49
99          */
100         LONG_STANDARD,
101         /**
102          * Long display name for daylight saving time, such as "Eastern Daylight Time".
103          *
104          * @stable ICU 49
105          */
106         LONG_DAYLIGHT,
107         /**
108          * Short display name, such as "ET".
109          *
110          * @stable ICU 49
111          */
112         SHORT_GENERIC,
113         /**
114          * Short display name for standard time, such as "EST".
115          *
116          * @stable ICU 49
117          */
118         SHORT_STANDARD,
119         /**
120          * Short display name for daylight saving time, such as "EDT".
121          *
122          * @stable ICU 49
123          */
124         SHORT_DAYLIGHT,
125         /**
126          * Exemplar location name, such as "Los Angeles".
127          *
128          * @stable ICU 51
129          */
130         EXEMPLAR_LOCATION,
131     }
132 
133     private static Cache TZNAMES_CACHE = new Cache();
134 
135     private static final Factory TZNAMES_FACTORY;
136     private static final String FACTORY_NAME_PROP = "com.ibm.icu.text.TimeZoneNames.Factory.impl";
137     private static final String DEFAULT_FACTORY_CLASS = "com.ibm.icu.impl.TimeZoneNamesFactoryImpl";
138 
139     static {
140         Factory factory = null;
141         String classname = ICUConfig.get(FACTORY_NAME_PROP, DEFAULT_FACTORY_CLASS);
142         while (true) {
143             try {
144                 factory = (Factory) Class.forName(classname).newInstance();
145                 break;
146             } catch (ClassNotFoundException cnfe) {
147                 // fall through
148             } catch (IllegalAccessException iae) {
149                 // fall through
150             } catch (InstantiationException ie) {
151                 // fall through
152             }
153             if (classname.equals(DEFAULT_FACTORY_CLASS)) {
154                 break;
155             }
156             classname = DEFAULT_FACTORY_CLASS;
157         }
158 
159         if (factory == null) {
160             factory = new DefaultTimeZoneNames.FactoryImpl();
161         }
162         TZNAMES_FACTORY = factory;
163     }
164 
165     /**
166      * Returns an instance of <code>TimeZoneNames</code> for the specified locale.
167      *
168      * @param locale
169      *            The locale.
170      * @return An instance of <code>TimeZoneNames</code>
171      * @stable ICU 49
172      */
getInstance(ULocale locale)173     public static TimeZoneNames getInstance(ULocale locale) {
174         String key = locale.getBaseName();
175         return TZNAMES_CACHE.getInstance(key, locale);
176     }
177 
178     /**
179      * Returns an instance of <code>TimeZoneNames</code> for the specified
180      * {@link java.util.Locale}.
181      *
182      * @param locale
183      *            The {@link java.util.Locale}.
184      * @return An instance of <code>TimeZoneDisplayNames</code>
185      * @stable ICU 54
186      */
getInstance(Locale locale)187     public static TimeZoneNames getInstance(Locale locale) {
188         return getInstance(ULocale.forLocale(locale));
189     }
190 
191     /**
192      * Returns an instance of <code>TimeZoneNames</code> containing only short specific
193      * zone names ({@link NameType#SHORT_STANDARD} and {@link NameType#SHORT_DAYLIGHT}),
194      * compatible with the IANA tz database's zone abbreviations (not localized).
195      * <br>
196      * Note: The input locale is used for resolving ambiguous names (e.g. "IST" is parsed
197      * as Israel Standard Time for Israel, while it is parsed as India Standard Time for
198      * all other regions). The zone names returned by this instance are not localized.
199      *
200      * @stable ICU 54
201      */
getTZDBInstance(ULocale locale)202     public static TimeZoneNames getTZDBInstance(ULocale locale) {
203         return new TZDBTimeZoneNames(locale);
204     }
205 
206     /**
207      * Returns an immutable set of all available meta zone IDs.
208      * @return An immutable set of all available meta zone IDs.
209      * @stable ICU 49
210      */
getAvailableMetaZoneIDs()211     public abstract Set<String> getAvailableMetaZoneIDs();
212 
213     /**
214      * Returns an immutable set of all available meta zone IDs used by the given time zone.
215      *
216      * @param tzID
217      *            The canonical time zone ID.
218      * @return An immutable set of all available meta zone IDs used by the given time zone.
219      * @stable ICU 49
220      */
getAvailableMetaZoneIDs(String tzID)221     public abstract Set<String> getAvailableMetaZoneIDs(String tzID);
222 
223     /**
224      * Returns the meta zone ID for the given canonical time zone ID at the given date.
225      *
226      * @param tzID
227      *            The canonical time zone ID.
228      * @param date
229      *            The date.
230      * @return The meta zone ID for the given time zone ID at the given date. If the time zone does not have a
231      *         corresponding meta zone at the given date or the implementation does not support meta zones, null is
232      *         returned.
233      * @stable ICU 49
234      */
getMetaZoneID(String tzID, long date)235     public abstract String getMetaZoneID(String tzID, long date);
236 
237     /**
238      * Returns the reference zone ID for the given meta zone ID for the region.
239      *
240      * Note: Each meta zone must have a reference zone associated with a special region "001" (world).
241      * Some meta zones may have region specific reference zone IDs other than the special region
242      * "001". When a meta zone does not have any region specific reference zone IDs, this method
243      * return the reference zone ID for the special region "001" (world).
244      *
245      * @param mzID
246      *            The meta zone ID.
247      * @param region
248      *            The region.
249      * @return The reference zone ID ("golden zone" in the LDML specification) for the given time zone ID for the
250      *         region. If the meta zone is unknown or the implementation does not support meta zones, null is returned.
251      * @stable ICU 49
252      */
getReferenceZoneID(String mzID, String region)253     public abstract String getReferenceZoneID(String mzID, String region);
254 
255     /**
256      * Returns the display name of the meta zone.
257      *
258      * @param mzID
259      *            The meta zone ID.
260      * @param type
261      *            The display name type. See {@link TimeZoneNames.NameType}.
262      * @return The display name of the meta zone. When this object does not have a localized display name for the given
263      *         meta zone with the specified type or the implementation does not provide any display names associated
264      *         with meta zones, null is returned.
265      * @stable ICU 49
266      */
getMetaZoneDisplayName(String mzID, NameType type)267     public abstract String getMetaZoneDisplayName(String mzID, NameType type);
268 
269     /**
270      * Returns the display name of the time zone at the given date.
271      *
272      * <p>
273      * <b>Note:</b> This method calls the subclass's {@link #getTimeZoneDisplayName(String, NameType)} first. When the
274      * result is null, this method calls {@link #getMetaZoneID(String, long)} to get the meta zone ID mapped from the
275      * time zone, then calls {@link #getMetaZoneDisplayName(String, NameType)}.
276      *
277      * @param tzID
278      *            The canonical time zone ID.
279      * @param type
280      *            The display name type. See {@link TimeZoneNames.NameType}.
281      * @param date
282      *            The date
283      * @return The display name for the time zone at the given date. When this object does not have a localized display
284      *         name for the time zone with the specified type and date, null is returned.
285      * @stable ICU 49
286      */
getDisplayName(String tzID, NameType type, long date)287     public final String getDisplayName(String tzID, NameType type, long date) {
288         String name = getTimeZoneDisplayName(tzID, type);
289         if (name == null) {
290             String mzID = getMetaZoneID(tzID, date);
291             name = getMetaZoneDisplayName(mzID, type);
292         }
293         return name;
294     }
295 
296     /**
297      * Returns the display name of the time zone. Unlike {@link #getDisplayName(String, NameType, long)},
298      * this method does not get a name from a meta zone used by the time zone.
299      *
300      * @param tzID
301      *            The canonical time zone ID.
302      * @param type
303      *            The display name type. See {@link TimeZoneNames.NameType}.
304      * @return The display name for the time zone. When this object does not have a localized display name for the given
305      *         time zone with the specified type, null is returned.
306      * @stable ICU 49
307      */
getTimeZoneDisplayName(String tzID, NameType type)308     public abstract String getTimeZoneDisplayName(String tzID, NameType type);
309 
310     /**
311      * Returns the exemplar location name for the given time zone. When this object does not have a localized location
312      * name, the default implementation may still returns a programmatically generated name with the logic described
313      * below.
314      * <ol>
315      * <li>Check if the ID contains "/". If not, return null.
316      * <li>Check if the ID does not start with "Etc/" or "SystemV/". If it does, return null.
317      * <li>Extract a substring after the last occurrence of "/".
318      * <li>Replace "_" with " ".
319      * </ol>
320      * For example, "New York" is returned for the time zone ID "America/New_York" when this object does not have the
321      * localized location name.
322      *
323      * @param tzID
324      *            The canonical time zone ID
325      * @return The exemplar location name for the given time zone, or null when a localized location name is not
326      *         available and the fallback logic described above cannot extract location from the ID.
327      * @stable ICU 49
328      */
getExemplarLocationName(String tzID)329     public String getExemplarLocationName(String tzID) {
330         return TimeZoneNamesImpl.getDefaultExemplarLocationName(tzID);
331     }
332 
333     /**
334      * Finds time zone name prefix matches for the input text at the
335      * given offset and returns a collection of the matches.
336      *
337      * @param text the text.
338      * @param start the starting offset within the text.
339      * @param types the set of name types, or <code>null</code> for all name types.
340      * @return A collection of matches.
341      * @see NameType
342      * @see MatchInfo
343      * @draft ICU 49 (Retain)
344      */
find(CharSequence text, int start, EnumSet<NameType> types)345     public Collection<MatchInfo> find(CharSequence text, int start, EnumSet<NameType> types) {
346         throw new UnsupportedOperationException("The method is not implemented in TimeZoneNames base class.");
347     }
348 
349     /**
350      * A <code>MatchInfo</code> represents a time zone name match used by
351      * {@link TimeZoneNames#find(CharSequence, int, EnumSet)}.
352      * @draft ICU 49 (Retain)
353      */
354     public static class MatchInfo {
355         private NameType _nameType;
356         private String _tzID;
357         private String _mzID;
358         private int _matchLength;
359 
360         /**
361          * Constructing a <code>MatchInfo</code>.
362          *
363          * @param nameType the name type enum.
364          * @param tzID the time zone ID, or null
365          * @param mzID the meta zone ID, or null
366          * @param matchLength the match length.
367          * @throws IllegalArgumentException when 1) <code>nameType</code> is <code>null</code>,
368          * or 2) both <code>tzID</code> and <code>mzID</code> are <code>null</code>,
369          * or 3) <code>matchLength</code> is 0 or smaller.
370          * @see NameType
371          * @draft ICU 49 (Retain)
372          */
MatchInfo(NameType nameType, String tzID, String mzID, int matchLength)373         public MatchInfo(NameType nameType, String tzID, String mzID, int matchLength) {
374             if (nameType == null) {
375                 throw new IllegalArgumentException("nameType is null");
376             }
377             if (tzID == null && mzID == null) {
378                 throw new IllegalArgumentException("Either tzID or mzID must be available");
379             }
380             if (matchLength <= 0) {
381                 throw new IllegalArgumentException("matchLength must be positive value");
382             }
383             _nameType = nameType;
384             _tzID = tzID;
385             _mzID = mzID;
386             _matchLength = matchLength;
387         }
388 
389         /**
390          * Returns the time zone ID, or <code>null</code> if not available.
391          *
392          * <p><b>Note</b>: A <code>MatchInfo</code> must have either a time zone ID
393          * or a meta zone ID.
394          *
395          * @return the time zone ID, or <code>null</code>.
396          * @see #mzID()
397          * @draft ICU 49 (Retain)
398          */
tzID()399         public String tzID() {
400             return _tzID;
401         }
402 
403         /**
404          * Returns the meta zone ID, or <code>null</code> if not available.
405          *
406          * <p><b>Note</b>: A <code>MatchInfo</code> must have either a time zone ID
407          * or a meta zone ID.
408          *
409          * @return the meta zone ID, or <code>null</code>.
410          * @see #tzID()
411          * @draft ICU 49 (Retain)
412          */
mzID()413         public String mzID() {
414             return _mzID;
415         }
416 
417         /**
418          * Returns the time zone name type.
419          * @return the time zone name type enum.
420          * @see NameType
421          * @draft ICU 49 (Retain)
422          */
nameType()423         public NameType nameType() {
424             return _nameType;
425         }
426 
427         /**
428          * Returns the match length.
429          * @return the match length.
430          * @draft ICU 49 (Retain)
431          */
matchLength()432         public int matchLength() {
433             return _matchLength;
434         }
435     }
436 
437     /**
438      * @internal For specific users only until proposed publicly.
439      * @deprecated This API is ICU internal only.
440      */
441     @Deprecated
loadAllDisplayNames()442     public void loadAllDisplayNames() {}
443 
444     /**
445      * @internal For specific users only until proposed publicly.
446      * @deprecated This API is ICU internal only.
447      */
448     @Deprecated
getDisplayNames(String tzID, NameType[] types, long date, String[] dest, int destOffset)449     public void getDisplayNames(String tzID, NameType[] types, long date,
450             String[] dest, int destOffset) {
451         if (tzID == null || tzID.length() == 0) {
452             return;
453         }
454         String mzID = null;
455         for (int i = 0; i < types.length; ++i) {
456             NameType type = types[i];
457             String name = getTimeZoneDisplayName(tzID, type);
458             if (name == null) {
459                 if (mzID == null) {
460                     mzID = getMetaZoneID(tzID, date);
461                 }
462                 name = getMetaZoneDisplayName(mzID, type);
463             }
464             dest[destOffset + i] = name;
465         }
466     }
467 
468     /**
469      * Sole constructor for invocation by subclass constructors.
470      *
471      * @draft ICU 49 (Retain)
472      */
TimeZoneNames()473     protected TimeZoneNames() {
474     }
475 
476     /**
477      * The super class of <code>TimeZoneNames</code> service factory classes.
478      *
479      * @internal
480      * @deprecated This API is ICU internal only.
481      */
482     @Deprecated
483     public static abstract class Factory {
484         /**
485          * The factory method of <code>TimeZoneNames</code>.
486          *
487          * @param locale
488          *            The display locale
489          * @return An instance of <code>TimeZoneNames</code>.
490          * @internal
491          * @deprecated This API is ICU internal only.
492          */
493         @Deprecated
getTimeZoneNames(ULocale locale)494         public abstract TimeZoneNames getTimeZoneNames(ULocale locale);
495 
496         /**
497          * Sole constructor
498          * @internal
499          * @deprecated This API is ICU internal only.
500          */
501         @Deprecated
Factory()502         protected Factory() {
503         }
504     }
505 
506     /**
507      * TimeZoneNames cache used by {@link TimeZoneNames#getInstance(ULocale)}
508      */
509     private static class Cache extends SoftCache<String, TimeZoneNames, ULocale> {
510 
511         /*
512          * (non-Javadoc)
513          *
514          * @see com.ibm.icu.impl.CacheBase#createInstance(java.lang.Object, java.lang.Object)
515          */
516         @Override
createInstance(String key, ULocale data)517         protected TimeZoneNames createInstance(String key, ULocale data) {
518             return TZNAMES_FACTORY.getTimeZoneNames(data);
519         }
520 
521     }
522 
523     ///CLOVER:OFF
524     /**
525      * The default implementation of <code>TimeZoneNames</code> used by {@link TimeZoneNames#getInstance(ULocale)} when
526      * the ICU4J tznamedata component is not available.
527      */
528     private static class DefaultTimeZoneNames extends TimeZoneNames {
529 
530         private static final long serialVersionUID = -995672072494349071L;
531 
532         public static final DefaultTimeZoneNames INSTANCE = new DefaultTimeZoneNames();
533 
534         /* (non-Javadoc)
535          * @see com.ibm.icu.text.TimeZoneNames#getAvailableMetaZoneIDs()
536          */
537         @Override
getAvailableMetaZoneIDs()538         public Set<String> getAvailableMetaZoneIDs() {
539             return Collections.emptySet();
540         }
541 
542         /* (non-Javadoc)
543          * @see com.ibm.icu.text.TimeZoneNames#getAvailableMetaZoneIDs(java.lang.String)
544          */
545         @Override
getAvailableMetaZoneIDs(String tzID)546         public Set<String> getAvailableMetaZoneIDs(String tzID) {
547             return Collections.emptySet();
548         }
549 
550         /*
551          * (non-Javadoc)
552          *
553          * @see com.ibm.icu.text.TimeZoneNames#getMetaZoneID (java.lang.String, long)
554          */
555         @Override
getMetaZoneID(String tzID, long date)556         public String getMetaZoneID(String tzID, long date) {
557             return null;
558         }
559 
560         /*
561          * (non-Javadoc)
562          *
563          * @see com.ibm.icu.text.TimeZoneNames#getReferenceZoneID(java.lang.String, java.lang.String)
564          */
565         @Override
getReferenceZoneID(String mzID, String region)566         public String getReferenceZoneID(String mzID, String region) {
567             return null;
568         }
569 
570         /*
571          *  (non-Javadoc)
572          * @see com.ibm.icu.text.TimeZoneNames#getMetaZoneDisplayName(java.lang.String, com.ibm.icu.text.TimeZoneNames.NameType)
573          */
574         @Override
getMetaZoneDisplayName(String mzID, NameType type)575         public String getMetaZoneDisplayName(String mzID, NameType type) {
576             return null;
577         }
578 
579         /*
580          * (non-Javadoc)
581          * @see com.ibm.icu.text.TimeZoneNames#getTimeZoneDisplayName(java.lang.String, com.ibm.icu.text.TimeZoneNames.NameType)
582          */
583         @Override
getTimeZoneDisplayName(String tzID, NameType type)584         public String getTimeZoneDisplayName(String tzID, NameType type) {
585             return null;
586         }
587 
588         /* (non-Javadoc)
589          * @see com.ibm.icu.text.TimeZoneNames#find(java.lang.CharSequence, int, com.ibm.icu.text.TimeZoneNames.NameType[])
590          */
591         @Override
find(CharSequence text, int start, EnumSet<NameType> nameTypes)592         public Collection<MatchInfo> find(CharSequence text, int start, EnumSet<NameType> nameTypes) {
593             return Collections.emptyList();
594         }
595 
596         /**
597          * The default <code>TimeZoneNames</code> factory called from {@link TimeZoneNames#getInstance(ULocale)} when
598          * the ICU4J tznamedata component is not available.
599          */
600         public static class FactoryImpl extends Factory {
601 
602             /*
603              * (non-Javadoc)
604              *
605              * @see com.ibm.icu.text.TimeZoneNames.Factory#getTimeZoneNames (com.ibm.icu.util.ULocale)
606              */
607             @Override
getTimeZoneNames(ULocale locale)608             public TimeZoneNames getTimeZoneNames(ULocale locale) {
609                 return DefaultTimeZoneNames.INSTANCE;
610             }
611         }
612     }
613     ///CLOVER:ON
614 }
615