001 /*
002 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 /*
027 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
028 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
029 *
030 * The original version of this source code and documentation is copyrighted
031 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
032 * materials are provided under terms of a License Agreement between Taligent
033 * and Sun. This technology is protected by multiple US and International
034 * patents. This notice and attribution to Taligent may not be removed.
035 * Taligent is a registered trademark of Taligent, Inc.
036 *
037 */
038
039 package java.text;
040
041 import java.io.IOException;
042 import java.io.ObjectOutputStream;
043 import java.io.Serializable;
044 import java.lang.ref.SoftReference;
045 import java.text.spi.DateFormatSymbolsProvider;
046 import java.util.Arrays;
047 import java.util.Hashtable;
048 import java.util.List;
049 import java.util.Locale;
050 import java.util.ResourceBundle;
051 import java.util.TimeZone;
052 import java.util.spi.LocaleServiceProvider;
053 import sun.util.LocaleServiceProviderPool;
054 import sun.util.TimeZoneNameUtility;
055 import sun.util.calendar.ZoneInfo;
056 import sun.util.resources.LocaleData;
057
058 /**
059 * <code>DateFormatSymbols</code> is a public class for encapsulating
060 * localizable date-time formatting data, such as the names of the
061 * months, the names of the days of the week, and the time zone data.
062 * <code>DateFormat</code> and <code>SimpleDateFormat</code> both use
063 * <code>DateFormatSymbols</code> to encapsulate this information.
064 *
065 * <p>
066 * Typically you shouldn't use <code>DateFormatSymbols</code> directly.
067 * Rather, you are encouraged to create a date-time formatter with the
068 * <code>DateFormat</code> class's factory methods: <code>getTimeInstance</code>,
069 * <code>getDateInstance</code>, or <code>getDateTimeInstance</code>.
070 * These methods automatically create a <code>DateFormatSymbols</code> for
071 * the formatter so that you don't have to. After the
072 * formatter is created, you may modify its format pattern using the
073 * <code>setPattern</code> method. For more information about
074 * creating formatters using <code>DateFormat</code>'s factory methods,
075 * see {@link DateFormat}.
076 *
077 * <p>
078 * If you decide to create a date-time formatter with a specific
079 * format pattern for a specific locale, you can do so with:
080 * <blockquote>
081 * <pre>
082 * new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale)).
083 * </pre>
084 * </blockquote>
085 *
086 * <p>
087 * <code>DateFormatSymbols</code> objects are cloneable. When you obtain
088 * a <code>DateFormatSymbols</code> object, feel free to modify the
089 * date-time formatting data. For instance, you can replace the localized
090 * date-time format pattern characters with the ones that you feel easy
091 * to remember. Or you can change the representative cities
092 * to your favorite ones.
093 *
094 * <p>
095 * New <code>DateFormatSymbols</code> subclasses may be added to support
096 * <code>SimpleDateFormat</code> for date-time formatting for additional locales.
097
098 * @see DateFormat
099 * @see SimpleDateFormat
100 * @see java.util.SimpleTimeZone
101 * @version 1.54 05/05/07
102 * @author Chen-Lieh Huang
103 */
104 public class DateFormatSymbols implements Serializable, Cloneable {
105
106 /**
107 * Construct a DateFormatSymbols object by loading format data from
108 * resources for the default locale. This constructor can only
109 * construct instances for the locales supported by the Java
110 * runtime environment, not for those supported by installed
111 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
112 * implementations. For full locale coverage, use the
113 * {@link #getInstance(Locale) getInstance} method.
114 *
115 * @see #getInstance()
116 * @exception java.util.MissingResourceException
117 * if the resources for the default locale cannot be
118 * found or cannot be loaded.
119 */
120 public DateFormatSymbols() {
121 initializeData(Locale.getDefault());
122 }
123
124 /**
125 * Construct a DateFormatSymbols object by loading format data from
126 * resources for the given locale. This constructor can only
127 * construct instances for the locales supported by the Java
128 * runtime environment, not for those supported by installed
129 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
130 * implementations. For full locale coverage, use the
131 * {@link #getInstance(Locale) getInstance} method.
132 *
133 * @see #getInstance(Locale)
134 * @exception java.util.MissingResourceException
135 * if the resources for the specified locale cannot be
136 * found or cannot be loaded.
137 */
138 public DateFormatSymbols(Locale locale) {
139 initializeData(locale);
140 }
141
142 /**
143 * Era strings. For example: "AD" and "BC". An array of 2 strings,
144 * indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
145 * @serial
146 */
147 String eras[] = null;
148
149 /**
150 * Month strings. For example: "January", "February", etc. An array
151 * of 13 strings (some calendars have 13 months), indexed by
152 * <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc.
153 * @serial
154 */
155 String months[] = null;
156
157 /**
158 * Short month strings. For example: "Jan", "Feb", etc. An array of
159 * 13 strings (some calendars have 13 months), indexed by
160 * <code>Calendar.JANUARY</code>, <code>Calendar.FEBRUARY</code>, etc.
161
162 * @serial
163 */
164 String shortMonths[] = null;
165
166 /**
167 * Weekday strings. For example: "Sunday", "Monday", etc. An array
168 * of 8 strings, indexed by <code>Calendar.SUNDAY</code>,
169 * <code>Calendar.MONDAY</code>, etc.
170 * The element <code>weekdays[0]</code> is ignored.
171 * @serial
172 */
173 String weekdays[] = null;
174
175 /**
176 * Short weekday strings. For example: "Sun", "Mon", etc. An array
177 * of 8 strings, indexed by <code>Calendar.SUNDAY</code>,
178 * <code>Calendar.MONDAY</code>, etc.
179 * The element <code>shortWeekdays[0]</code> is ignored.
180 * @serial
181 */
182 String shortWeekdays[] = null;
183
184 /**
185 * AM and PM strings. For example: "AM" and "PM". An array of
186 * 2 strings, indexed by <code>Calendar.AM</code> and
187 * <code>Calendar.PM</code>.
188 * @serial
189 */
190 String ampms[] = null;
191
192 /**
193 * Localized names of time zones in this locale. This is a
194 * two-dimensional array of strings of size <em>n</em> by <em>m</em>,
195 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an
196 * entry containing the localized names for a single <code>TimeZone</code>.
197 * Each such row contains (with <code>i</code> ranging from
198 * 0..<em>n</em>-1):
199 * <ul>
200 * <li><code>zoneStrings[i][0]</code> - time zone ID</li>
201 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard
202 * time</li>
203 * <li><code>zoneStrings[i][2]</code> - short name of zone in
204 * standard time</li>
205 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight
206 * saving time</li>
207 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight
208 * saving time</li>
209 * </ul>
210 * The zone ID is <em>not</em> localized; it's one of the valid IDs of
211 * the {@link java.util.TimeZone TimeZone} class that are not
212 * <a href="../java/util/TimeZone.html#CustomID">custom IDs</a>.
213 * All other entries are localized names.
214 * @see java.util.TimeZone
215 * @serial
216 */
217 String zoneStrings[][] = null;
218
219 /**
220 * Indicates that zoneStrings is set externally with setZoneStrings() method.
221 */
222 transient boolean isZoneStringsSet = false;
223
224 /**
225 * Unlocalized date-time pattern characters. For example: 'y', 'd', etc.
226 * All locales use the same these unlocalized pattern characters.
227 */
228 static final String patternChars = "GyMdkHmsSEDFwWahKzZ";
229
230 /**
231 * Localized date-time pattern characters. For example, a locale may
232 * wish to use 'u' rather than 'y' to represent years in its date format
233 * pattern strings.
234 * This string must be exactly 18 characters long, with the index of
235 * the characters described by <code>DateFormat.ERA_FIELD</code>,
236 * <code>DateFormat.YEAR_FIELD</code>, etc. Thus, if the string were
237 * "Xz...", then localized patterns would use 'X' for era and 'z' for year.
238 * @serial
239 */
240 String localPatternChars = null;
241
242 /**
243 * The locale which is used for initializing this DateFormatSymbols object.
244 *
245 * @since 1.6
246 * @serial
247 */
248 Locale locale = null;
249
250 /* use serialVersionUID from JDK 1.1.4 for interoperability */
251 static final long serialVersionUID = -5987973545549424702L;
252
253 /**
254 * Returns an array of all locales for which the
255 * <code>getInstance</code> methods of this class can return
256 * localized instances.
257 * The returned array represents the union of locales supported by the
258 * Java runtime and by installed
259 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
260 * implementations. It must contain at least a <code>Locale</code>
261 * instance equal to {@link java.util.Locale#US Locale.US}.
262 *
263 * @return An array of locales for which localized
264 * <code>DateFormatSymbols</code> instances are available.
265 * @since 1.6
266 */
267 public static Locale[] getAvailableLocales() {
268 LocaleServiceProviderPool pool = LocaleServiceProviderPool
269 .getPool(DateFormatSymbolsProvider.class);
270 return pool.getAvailableLocales();
271 }
272
273 /**
274 * Gets the <code>DateFormatSymbols</code> instance for the default
275 * locale. This method provides access to <code>DateFormatSymbols</code>
276 * instances for locales supported by the Java runtime itself as well
277 * as for those supported by installed
278 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
279 * implementations.
280 * @return a <code>DateFormatSymbols</code> instance.
281 * @since 1.6
282 */
283 public static final DateFormatSymbols getInstance() {
284 return getInstance(Locale.getDefault());
285 }
286
287 /**
288 * Gets the <code>DateFormatSymbols</code> instance for the specified
289 * locale. This method provides access to <code>DateFormatSymbols</code>
290 * instances for locales supported by the Java runtime itself as well
291 * as for those supported by installed
292 * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider}
293 * implementations.
294 * @param locale the given locale.
295 * @return a <code>DateFormatSymbols</code> instance.
296 * @exception NullPointerException if <code>locale</code> is null
297 * @since 1.6
298 */
299 public static final DateFormatSymbols getInstance(Locale locale) {
300
301 // Check whether a provider can provide an implementation that's closer
302 // to the requested locale than what the Java runtime itself can provide.
303 LocaleServiceProviderPool pool = LocaleServiceProviderPool
304 .getPool(DateFormatSymbolsProvider.class);
305 if (pool.hasProviders()) {
306 DateFormatSymbols providersInstance = pool
307 .getLocalizedObject(
308 DateFormatSymbolsGetter.INSTANCE, locale);
309 if (providersInstance != null) {
310 return providersInstance;
311 }
312 }
313
314 return new DateFormatSymbols(locale);
315 }
316
317 /**
318 * Gets era strings. For example: "AD" and "BC".
319 * @return the era strings.
320 */
321 public String[] getEras() {
322 return duplicate(eras);
323 }
324
325 /**
326 * Sets era strings. For example: "AD" and "BC".
327 * @param newEras the new era strings.
328 */
329 public void setEras(String[] newEras) {
330 eras = duplicate(newEras);
331 }
332
333 /**
334 * Gets month strings. For example: "January", "February", etc.
335 * @return the month strings.
336 */
337 public String[] getMonths() {
338 return duplicate(months);
339 }
340
341 /**
342 * Sets month strings. For example: "January", "February", etc.
343 * @param newMonths the new month strings.
344 */
345 public void setMonths(String[] newMonths) {
346 months = duplicate(newMonths);
347 }
348
349 /**
350 * Gets short month strings. For example: "Jan", "Feb", etc.
351 * @return the short month strings.
352 */
353 public String[] getShortMonths() {
354 return duplicate(shortMonths);
355 }
356
357 /**
358 * Sets short month strings. For example: "Jan", "Feb", etc.
359 * @param newShortMonths the new short month strings.
360 */
361 public void setShortMonths(String[] newShortMonths) {
362 shortMonths = duplicate(newShortMonths);
363 }
364
365 /**
366 * Gets weekday strings. For example: "Sunday", "Monday", etc.
367 * @return the weekday strings. Use <code>Calendar.SUNDAY</code>,
368 * <code>Calendar.MONDAY</code>, etc. to index the result array.
369 */
370 public String[] getWeekdays() {
371 return duplicate(weekdays);
372 }
373
374 /**
375 * Sets weekday strings. For example: "Sunday", "Monday", etc.
376 * @param newWeekdays the new weekday strings. The array should
377 * be indexed by <code>Calendar.SUNDAY</code>,
378 * <code>Calendar.MONDAY</code>, etc.
379 */
380 public void setWeekdays(String[] newWeekdays) {
381 weekdays = duplicate(newWeekdays);
382 }
383
384 /**
385 * Gets short weekday strings. For example: "Sun", "Mon", etc.
386 * @return the short weekday strings. Use <code>Calendar.SUNDAY</code>,
387 * <code>Calendar.MONDAY</code>, etc. to index the result array.
388 */
389 public String[] getShortWeekdays() {
390 return duplicate(shortWeekdays);
391 }
392
393 /**
394 * Sets short weekday strings. For example: "Sun", "Mon", etc.
395 * @param newShortWeekdays the new short weekday strings. The array should
396 * be indexed by <code>Calendar.SUNDAY</code>,
397 * <code>Calendar.MONDAY</code>, etc.
398 */
399 public void setShortWeekdays(String[] newShortWeekdays) {
400 shortWeekdays = duplicate(newShortWeekdays);
401 }
402
403 /**
404 * Gets ampm strings. For example: "AM" and "PM".
405 * @return the ampm strings.
406 */
407 public String[] getAmPmStrings() {
408 return duplicate(ampms);
409 }
410
411 /**
412 * Sets ampm strings. For example: "AM" and "PM".
413 * @param newAmpms the new ampm strings.
414 */
415 public void setAmPmStrings(String[] newAmpms) {
416 ampms = duplicate(newAmpms);
417 }
418
419 /**
420 * Gets time zone strings. Use of this method is discouraged; use
421 * {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()}
422 * instead.
423 * <p>
424 * The value returned is a
425 * two-dimensional array of strings of size <em>n</em> by <em>m</em>,
426 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an
427 * entry containing the localized names for a single <code>TimeZone</code>.
428 * Each such row contains (with <code>i</code> ranging from
429 * 0..<em>n</em>-1):
430 * <ul>
431 * <li><code>zoneStrings[i][0]</code> - time zone ID</li>
432 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard
433 * time</li>
434 * <li><code>zoneStrings[i][2]</code> - short name of zone in
435 * standard time</li>
436 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight
437 * saving time</li>
438 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight
439 * saving time</li>
440 * </ul>
441 * The zone ID is <em>not</em> localized; it's one of the valid IDs of
442 * the {@link java.util.TimeZone TimeZone} class that are not
443 * <a href="../util/TimeZone.html#CustomID">custom IDs</a>.
444 * All other entries are localized names. If a zone does not implement
445 * daylight saving time, the daylight saving time names should not be used.
446 * <p>
447 * If {@link #setZoneStrings(String[][]) setZoneStrings} has been called
448 * on this <code>DateFormatSymbols</code> instance, then the strings
449 * provided by that call are returned. Otherwise, the returned array
450 * contains names provided by the Java runtime and by installed
451 * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider}
452 * implementations.
453 *
454 * @return the time zone strings.
455 * @see #setZoneStrings(String[][])
456 */
457 public String[][] getZoneStrings() {
458 return getZoneStringsImpl(true);
459 }
460
461 /**
462 * Sets time zone strings. The argument must be a
463 * two-dimensional array of strings of size <em>n</em> by <em>m</em>,
464 * where <em>m</em> is at least 5. Each of the <em>n</em> rows is an
465 * entry containing the localized names for a single <code>TimeZone</code>.
466 * Each such row contains (with <code>i</code> ranging from
467 * 0..<em>n</em>-1):
468 * <ul>
469 * <li><code>zoneStrings[i][0]</code> - time zone ID</li>
470 * <li><code>zoneStrings[i][1]</code> - long name of zone in standard
471 * time</li>
472 * <li><code>zoneStrings[i][2]</code> - short name of zone in
473 * standard time</li>
474 * <li><code>zoneStrings[i][3]</code> - long name of zone in daylight
475 * saving time</li>
476 * <li><code>zoneStrings[i][4]</code> - short name of zone in daylight
477 * saving time</li>
478 * </ul>
479 * The zone ID is <em>not</em> localized; it's one of the valid IDs of
480 * the {@link java.util.TimeZone TimeZone} class that are not
481 * <a href="../util/TimeZone.html#CustomID">custom IDs</a>.
482 * All other entries are localized names.
483 *
484 * @param newZoneStrings the new time zone strings.
485 * @exception IllegalArgumentException if the length of any row in
486 * <code>newZoneStrings</code> is less than 5
487 * @exception NullPointerException if <code>newZoneStrings</code> is null
488 * @see #getZoneStrings()
489 */
490 public void setZoneStrings(String[][] newZoneStrings) {
491 String[][] aCopy = new String[newZoneStrings.length][];
492 for (int i = 0; i < newZoneStrings.length; ++i) {
493 if (newZoneStrings[i].length < 5) {
494 throw new IllegalArgumentException();
495 }
496 aCopy[i] = duplicate(newZoneStrings[i]);
497 }
498 zoneStrings = aCopy;
499 isZoneStringsSet = true;
500 }
501
502 /**
503 * Gets localized date-time pattern characters. For example: 'u', 't', etc.
504 * @return the localized date-time pattern characters.
505 */
506 public String getLocalPatternChars() {
507 return new String(localPatternChars);
508 }
509
510 /**
511 * Sets localized date-time pattern characters. For example: 'u', 't', etc.
512 * @param newLocalPatternChars the new localized date-time
513 * pattern characters.
514 */
515 public void setLocalPatternChars(String newLocalPatternChars) {
516 localPatternChars = new String(newLocalPatternChars);
517 }
518
519 /**
520 * Overrides Cloneable
521 */
522 public Object clone() {
523 try {
524 DateFormatSymbols other = (DateFormatSymbols) super .clone();
525 copyMembers(this , other);
526 return other;
527 } catch (CloneNotSupportedException e) {
528 throw new InternalError();
529 }
530 }
531
532 /**
533 * Override hashCode.
534 * Generates a hash code for the DateFormatSymbols object.
535 */
536 public int hashCode() {
537 int hashcode = 0;
538 String[][] zoneStrings = getZoneStringsWrapper();
539 for (int index = 0; index < zoneStrings[0].length; ++index)
540 hashcode ^= zoneStrings[0][index].hashCode();
541 return hashcode;
542 }
543
544 /**
545 * Override equals
546 */
547 public boolean equals(Object obj) {
548 if (this == obj)
549 return true;
550 if (obj == null || getClass() != obj.getClass())
551 return false;
552 DateFormatSymbols that = (DateFormatSymbols) obj;
553 return (Arrays.equals(eras, that.eras)
554 && Arrays.equals(months, that.months)
555 && Arrays.equals(shortMonths, that.shortMonths)
556 && Arrays.equals(weekdays, that.weekdays)
557 && Arrays.equals(shortWeekdays, that.shortWeekdays)
558 && Arrays.equals(ampms, that.ampms)
559 && Arrays.deepEquals(getZoneStringsWrapper(), that
560 .getZoneStringsWrapper()) && ((localPatternChars != null && localPatternChars
561 .equals(that.localPatternChars)) || (localPatternChars == null && that.localPatternChars == null)));
562 }
563
564 // =======================privates===============================
565
566 /**
567 * Useful constant for defining time zone offsets.
568 */
569 static final int millisPerHour = 60 * 60 * 1000;
570
571 /**
572 * Cache to hold the FormatData and TimeZoneNames ResourceBundles
573 * of a Locale.
574 */
575 private static Hashtable cachedLocaleData = new Hashtable(3);
576
577 /**
578 * Look up resource data for the desiredLocale in the cache; update the
579 * cache if necessary.
580 */
581 private static ResourceBundle cacheLookup(Locale desiredLocale) {
582 ResourceBundle rb;
583 SoftReference data = (SoftReference) cachedLocaleData
584 .get(desiredLocale);
585 if (data == null) {
586 rb = LocaleData.getDateFormatData(desiredLocale);
587 data = new SoftReference(rb);
588 cachedLocaleData.put(desiredLocale, data);
589 } else {
590 if ((rb = (ResourceBundle) data.get()) == null) {
591 rb = LocaleData.getDateFormatData(desiredLocale);
592 data = new SoftReference(rb);
593 }
594 }
595 return rb;
596 }
597
598 private void initializeData(Locale desiredLocale) {
599 int i;
600 ResourceBundle resource = cacheLookup(desiredLocale);
601
602 // FIXME: cache only ResourceBundle. Hence every time, will do
603 // getObject(). This won't be necessary if the Resource itself
604 // is cached.
605 eras = (String[]) resource.getObject("Eras");
606 months = resource.getStringArray("MonthNames");
607 shortMonths = resource.getStringArray("MonthAbbreviations");
608 String[] lWeekdays = resource.getStringArray("DayNames");
609 weekdays = new String[8];
610 weekdays[0] = ""; // 1-based
611 for (i = 0; i < lWeekdays.length; i++)
612 weekdays[i + 1] = lWeekdays[i];
613 String[] sWeekdays = resource
614 .getStringArray("DayAbbreviations");
615 shortWeekdays = new String[8];
616 shortWeekdays[0] = ""; // 1-based
617 for (i = 0; i < sWeekdays.length; i++)
618 shortWeekdays[i + 1] = sWeekdays[i];
619 ampms = resource.getStringArray("AmPmMarkers");
620 localPatternChars = resource.getString("DateTimePatternChars");
621
622 locale = desiredLocale;
623 }
624
625 /**
626 * Package private: used by SimpleDateFormat
627 * Gets the index for the given time zone ID to obtain the time zone
628 * strings for formatting. The time zone ID is just for programmatic
629 * lookup. NOT LOCALIZED!!!
630 * @param ID the given time zone ID.
631 * @return the index of the given time zone ID. Returns -1 if
632 * the given time zone ID can't be located in the DateFormatSymbols object.
633 * @see java.util.SimpleTimeZone
634 */
635 final int getZoneIndex(String ID) {
636 String[][] zoneStrings = getZoneStringsWrapper();
637 for (int index = 0; index < zoneStrings.length; index++) {
638 if (ID.equalsIgnoreCase(zoneStrings[index][0]))
639 return index;
640 }
641
642 return -1;
643 }
644
645 /**
646 * Wrapper method to the getZoneStrings(), which is called from inside
647 * the java.text package and not to mutate the returned arrays, so that
648 * it does not need to create a defensive copy.
649 */
650 final String[][] getZoneStringsWrapper() {
651 if (isSubclassObject()) {
652 return getZoneStrings();
653 } else {
654 return getZoneStringsImpl(false);
655 }
656 }
657
658 private final String[][] getZoneStringsImpl(boolean needsCopy) {
659 if (zoneStrings == null) {
660 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
661 }
662
663 if (needsCopy) {
664 String[][] aCopy = new String[zoneStrings.length][];
665 for (int i = 0; i < zoneStrings.length; ++i) {
666 aCopy[i] = duplicate(zoneStrings[i]);
667 }
668 return aCopy;
669 } else {
670 return zoneStrings;
671 }
672 }
673
674 private final boolean isSubclassObject() {
675 return !getClass().getName().equals(
676 "java.text.DateFormatSymbols");
677 }
678
679 /**
680 * Clones an array of Strings.
681 * @param srcArray the source array to be cloned.
682 * @param count the number of elements in the given source array.
683 * @return a cloned array.
684 */
685 private final String[] duplicate(String[] srcArray) {
686 String[] dstArray = new String[srcArray.length];
687 System.arraycopy(srcArray, 0, dstArray, 0, srcArray.length);
688 return dstArray;
689 }
690
691 /**
692 * Clones all the data members from the source DateFormatSymbols to
693 * the target DateFormatSymbols. This is only for subclasses.
694 * @param src the source DateFormatSymbols.
695 * @param dst the target DateFormatSymbols.
696 */
697 private final void copyMembers(DateFormatSymbols src,
698 DateFormatSymbols dst) {
699 dst.eras = duplicate(src.eras);
700 dst.months = duplicate(src.months);
701 dst.shortMonths = duplicate(src.shortMonths);
702 dst.weekdays = duplicate(src.weekdays);
703 dst.shortWeekdays = duplicate(src.shortWeekdays);
704 dst.ampms = duplicate(src.ampms);
705 if (src.zoneStrings != null) {
706 if (dst.zoneStrings == null) {
707 dst.zoneStrings = new String[src.zoneStrings.length][];
708 }
709 for (int i = 0; i < dst.zoneStrings.length; ++i) {
710 dst.zoneStrings[i] = duplicate(src.zoneStrings[i]);
711 }
712 } else {
713 dst.zoneStrings = null;
714 }
715 dst.localPatternChars = new String(src.localPatternChars);
716 }
717
718 /**
719 * Compares the equality of the two arrays of String.
720 * @param current this String array.
721 * @param other that String array.
722 */
723 private final boolean equals(String[] current, String[] other) {
724 int count = current.length;
725
726 for (int i = 0; i < count; ++i)
727 if (!current[i].equals(other[i]))
728 return false;
729 return true;
730 }
731
732 /**
733 * Write out the default serializable data, after ensuring the
734 * <code>zoneStrings</code> field is initialized in order to make
735 * sure the backward compatibility.
736 *
737 * @since 1.6
738 */
739 private void writeObject(ObjectOutputStream stream)
740 throws IOException {
741 if (zoneStrings == null) {
742 zoneStrings = TimeZoneNameUtility.getZoneStrings(locale);
743 }
744 stream.defaultWriteObject();
745 }
746
747 /**
748 * Obtains a DateFormatSymbols instance from a DateFormatSymbolsProvider
749 * implementation.
750 */
751 private static class DateFormatSymbolsGetter
752 implements
753 LocaleServiceProviderPool.LocalizedObjectGetter<DateFormatSymbolsProvider, DateFormatSymbols> {
754 private static final DateFormatSymbolsGetter INSTANCE = new DateFormatSymbolsGetter();
755
756 public DateFormatSymbols getObject(
757 DateFormatSymbolsProvider dateFormatSymbolsProvider,
758 Locale locale, String key, Object... params) {
759 assert params.length == 0;
760 return dateFormatSymbolsProvider.getInstance(locale);
761 }
762 }
763 }
|