1 7 8 20 21 package java.text; 22 import java.util.Locale ; 23 import java.util.ResourceBundle ; 24 import java.io.Serializable ; 25 import java.lang.ref.SoftReference ; 26 import java.util.Vector ; 27 import java.util.Enumeration ; 28 import sun.text.Utility; 29 import sun.text.resources.LocaleData; 30 import java.util.Hashtable ; 31 32 78 public class DateFormatSymbols implements Serializable , Cloneable { 79 80 88 public DateFormatSymbols() 89 { 90 initializeData(Locale.getDefault()); 91 } 92 93 101 public DateFormatSymbols(Locale locale) 102 { 103 initializeData(locale); 104 } 105 106 111 String eras[] = null; 112 113 119 String months[] = null; 120 121 128 String shortMonths[] = null; 129 130 137 String weekdays[] = null; 138 139 146 String shortWeekdays[] = null; 147 148 154 String ampms[] = null; 155 156 181 String zoneStrings[][] = null; 182 183 187 static final String patternChars = "GyMdkHmsSEDFwWahKzZ"; 188 189 199 String localPatternChars = null; 200 201 202 static final long serialVersionUID = -5987973545549424702L; 203 204 208 public String [] getEras() { 209 return duplicate(eras); 210 } 211 212 216 public void setEras(String [] newEras) { 217 eras = duplicate(newEras); 218 } 219 220 224 public String [] getMonths() { 225 return duplicate(months); 226 } 227 228 232 public void setMonths(String [] newMonths) { 233 months = duplicate(newMonths); 234 } 235 236 240 public String [] getShortMonths() { 241 return duplicate(shortMonths); 242 } 243 244 248 public void setShortMonths(String [] newShortMonths) { 249 shortMonths = duplicate(newShortMonths); 250 } 251 252 257 public String [] getWeekdays() { 258 return duplicate(weekdays); 259 } 260 261 267 public void setWeekdays(String [] newWeekdays) { 268 weekdays = duplicate(newWeekdays); 269 } 270 271 276 public String [] getShortWeekdays() { 277 return duplicate(shortWeekdays); 278 } 279 280 286 public void setShortWeekdays(String [] newShortWeekdays) { 287 shortWeekdays = duplicate(newShortWeekdays); 288 } 289 290 294 public String [] getAmPmStrings() { 295 return duplicate(ampms); 296 } 297 298 302 public void setAmPmStrings(String [] newAmpms) { 303 ampms = duplicate(newAmpms); 304 } 305 306 310 public String [][] getZoneStrings() { 311 String [][] aCopy = new String [zoneStrings.length][]; 312 for (int i = 0; i < zoneStrings.length; ++i) 313 aCopy[i] = duplicate(zoneStrings[i]); 314 return aCopy; 315 } 316 317 321 public void setZoneStrings(String [][] newZoneStrings) { 322 String [][] aCopy = new String [newZoneStrings.length][]; 323 for (int i = 0; i < newZoneStrings.length; ++i) 324 aCopy[i] = duplicate(newZoneStrings[i]); 325 zoneStrings = aCopy; 326 } 327 328 332 public String getLocalPatternChars() { 333 return new String (localPatternChars); 334 } 335 336 341 public void setLocalPatternChars(String newLocalPatternChars) { 342 localPatternChars = new String (newLocalPatternChars); 343 } 344 345 348 public Object clone() 349 { 350 try 351 { 352 DateFormatSymbols other = (DateFormatSymbols )super.clone(); 353 copyMembers(this, other); 354 return other; 355 } catch (CloneNotSupportedException e) { 356 throw new InternalError (); 357 } 358 } 359 360 364 public int hashCode() { 365 int hashcode = 0; 366 for (int index = 0; index < this.zoneStrings[0].length; ++index) 367 hashcode ^= this.zoneStrings[0][index].hashCode(); 368 return hashcode; 369 } 370 371 374 public boolean equals(Object obj) 375 { 376 if (this == obj) return true; 377 if (obj == null || getClass() != obj.getClass()) return false; 378 DateFormatSymbols that = (DateFormatSymbols ) obj; 379 return (Utility.arrayEquals(eras, that.eras) 380 && Utility.arrayEquals(months, that.months) 381 && Utility.arrayEquals(shortMonths, that.shortMonths) 382 && Utility.arrayEquals(weekdays, that.weekdays) 383 && Utility.arrayEquals(shortWeekdays, that.shortWeekdays) 384 && Utility.arrayEquals(ampms, that.ampms) 385 && Utility.arrayEquals(zoneStrings, that.zoneStrings) 386 && Utility.arrayEquals(localPatternChars, 387 that.localPatternChars)); 388 } 389 390 392 395 static final int millisPerHour = 60*60*1000; 396 397 401 private static Hashtable cachedLocaleData = new Hashtable (3); 402 403 406 private static Hashtable cachedZoneData = new Hashtable (); 407 408 412 private ResourceBundle [] cacheLookup(Locale desiredLocale) { 413 ResourceBundle [] rbs = new ResourceBundle [2]; 414 SoftReference [] data 415 = (SoftReference [])cachedLocaleData.get(desiredLocale); 416 if (data == null) { 417 rbs[0] = LocaleData.getLocaleElements(desiredLocale); 418 rbs[1] = LocaleData.getDateFormatZoneData(desiredLocale); 419 data = new SoftReference [] { new SoftReference (rbs[0]), 420 new SoftReference (rbs[1]) }; 421 cachedLocaleData.put(desiredLocale, data); 422 } else { 423 ResourceBundle r; 424 if ((r = (ResourceBundle )data[0].get()) == null) { 425 r = LocaleData.getLocaleElements(desiredLocale); 426 data[0] = new SoftReference (r); 427 } 428 rbs[0] = r; 429 if ((r = (ResourceBundle )data[1].get()) == null) { 430 r = LocaleData.getDateFormatZoneData(desiredLocale); 431 data[1] = new SoftReference (r); 432 } 433 rbs[1] = r; 434 } 435 return rbs; 436 } 437 438 442 private String [][] loadZoneStrings(Locale desiredLocale, 443 ResourceBundle rsrc) 444 { 445 String [][] zones; 446 SoftReference data = (SoftReference )cachedZoneData.get(desiredLocale); 447 if (data == null || ((zones = (String [][])data.get()) == null)) { 448 Vector vec = new Vector (); 449 Enumeration keys = rsrc.getKeys(); 450 while(keys.hasMoreElements()) { 451 String key = (String )keys.nextElement(); 452 if (!key.equals("localPatternChars") && 453 !key.equals("zoneStrings")) { 454 vec.add(rsrc.getObject(key)); 455 } 456 } 457 zones = new String [vec.size()][]; 458 vec.toArray(zones); 459 data = new SoftReference (zones); 460 cachedZoneData.put(desiredLocale, data); 461 } 462 return zones; 463 } 464 465 private void initializeData(Locale desiredLocale) 466 { 467 int i; 468 ResourceBundle [] rbs = cacheLookup(desiredLocale); 469 ResourceBundle resource = rbs[0]; 470 ResourceBundle zoneResource = rbs[1]; 471 472 eras = (String [])resource.getObject("Eras"); 476 months = resource.getStringArray("MonthNames"); 477 shortMonths = resource.getStringArray("MonthAbbreviations"); 478 String [] lWeekdays = resource.getStringArray("DayNames"); 479 weekdays = new String [8]; 480 weekdays[0] = ""; for (i=0; i<lWeekdays.length; i++) 482 weekdays[i+1] = lWeekdays[i]; 483 String [] sWeekdays = resource.getStringArray("DayAbbreviations"); 484 shortWeekdays = new String [8]; 485 shortWeekdays[0] = ""; for (i=0; i<sWeekdays.length; i++) 487 shortWeekdays[i+1] = sWeekdays[i]; 488 ampms = resource.getStringArray("AmPmMarkers"); 489 zoneStrings = (String [][])loadZoneStrings(desiredLocale, 490 zoneResource); 491 localPatternChars 492 = (String ) zoneResource.getObject("localPatternChars"); 493 } 494 495 505 final int getZoneIndex (String ID) 506 { 507 for (int index=0; index<zoneStrings.length; index++) 508 { 509 if (ID.equalsIgnoreCase(zoneStrings[index][0])) return index; 510 } 511 512 return -1; 513 } 514 515 521 private final String [] duplicate(String [] srcArray) 522 { 523 String [] dstArray = new String [srcArray.length]; 524 System.arraycopy(srcArray, 0, dstArray, 0, srcArray.length); 525 return dstArray; 526 } 527 528 534 private final void copyMembers(DateFormatSymbols src, DateFormatSymbols dst) 535 { 536 dst.eras = duplicate(src.eras); 537 dst.months = duplicate(src.months); 538 dst.shortMonths = duplicate(src.shortMonths); 539 dst.weekdays = duplicate(src.weekdays); 540 dst.shortWeekdays = duplicate(src.shortWeekdays); 541 dst.ampms = duplicate(src.ampms); 542 for (int i = 0; i < dst.zoneStrings.length; ++i) 543 dst.zoneStrings[i] = duplicate(src.zoneStrings[i]); 544 dst.localPatternChars = new String (src.localPatternChars); 545 } 546 547 552 private final boolean equals(String [] current, String [] other) 553 { 554 int count = current.length; 555 556 for (int i = 0; i < count; ++i) 557 if (!current[i].equals(other[i])) 558 return false; 559 return true; 560 } 561 562 } 563 | Popular Tags |