315 String[] getCalendarNames(String key) { 316 String[] names = null; 317 String cacheKey = CALENDAR_NAMES + key; 318 319 removeEmptyReferences(); 320 ResourceReference data = cache.get(cacheKey); 321 322 if (data == null || ((names = (String[]) data.get()) == null)) { 323 ResourceBundle rb = localeData.getDateFormatData(locale); 324 if (rb.containsKey(key)) { 325 names = rb.getStringArray(key); 326 cache.put(cacheKey, 327 new ResourceReference(cacheKey, (Object) names, referenceQueue)); 328 } 329 } 330 331 return names; 332 } 333 334 public String getDateTimePattern(int timeStyle, int dateStyle, Calendar cal) { 335 String pattern; 336 337 if (cal == null) { 338 cal = Calendar.getInstance(locale); 339 } 340 String calType = cal.getCalendarType(); 341 String timePattern = null; 342 String datePattern = null; 343 if (timeStyle >= 0) { 344 timePattern = getDateTimePattern("TimePatterns", timeStyle, calType); 345 } 346 if (dateStyle >= 0) { 347 datePattern = getDateTimePattern("DatePatterns", dateStyle, calType); 348 } 349 if (timeStyle >= 0) { 350 if (dateStyle >= 0) { 351 String dateTimePattern = getDateTimePattern("DateTimePatterns", 0, calType); 352 switch (dateTimePattern) { 353 case "{1} {0}": 354 pattern = datePattern + " " + timePattern; 355 break; 356 case "{0} {1}": 357 pattern = timePattern + " " + datePattern; 358 break; 359 default: 360 pattern = MessageFormat.format(dateTimePattern, timePattern, datePattern); 361 break; 362 } 363 } else { 364 pattern = timePattern; 365 } 366 } else if (dateStyle >= 0) { 367 pattern = datePattern; 368 } else { 369 throw new IllegalArgumentException("No date or time style specified"); 370 } 371 return pattern; 379 380 if (data == null || ((numberPatterns = (String[]) data.get()) == null)) { 381 ResourceBundle resource = localeData.getNumberFormatData(locale); 382 numberPatterns = resource.getStringArray("NumberPatterns"); 383 cache.put(NUMBER_PATTERNS_CACHEKEY, 384 new ResourceReference(NUMBER_PATTERNS_CACHEKEY, (Object) numberPatterns, referenceQueue)); 385 } 386 387 return numberPatterns; 388 } 389 390 /** 391 * Returns the FormatData resource bundle of this LocaleResources. 392 * The FormatData should be used only for accessing extra 393 * resources required by JSR 310. 394 */ 395 public ResourceBundle getFormatData() { 396 return localeData.getDateFormatData(locale); 397 } 398 399 private String getDateTimePattern(String key, int styleIndex, String calendarType) { 400 String resourceKey = "gregory".equals(calendarType) ? key : calendarType + "." + key; 401 String cacheKey = DATE_TIME_PATTERN + resourceKey; 402 String[] patterns = null; 403 404 removeEmptyReferences(); 405 ResourceReference data = cache.get(cacheKey); 406 407 if (data == null || ((patterns = (String[]) data.get()) == null)) { 408 ResourceBundle r = localeData.getDateFormatData(locale); 409 if (r.containsKey(resourceKey)) { 410 patterns = r.getStringArray(resourceKey); 411 } else { 412 assert !resourceKey.equals(key); 413 patterns = r.getStringArray(key); 414 } 415 cache.put(cacheKey, 416 new ResourceReference(cacheKey, (Object) patterns, referenceQueue)); 417 } 418 419 return patterns[styleIndex]; 420 } 421 422 private static class ResourceReference extends SoftReference<Object> { 423 private final String cacheKey; 424 425 ResourceReference(String cacheKey, Object o, ReferenceQueue<Object> q) { 426 super(o, q); 427 this.cacheKey = cacheKey; 428 } 429 430 String getCacheKey() { 431 return cacheKey; 432 } 433 } 434 } | 315 String[] getCalendarNames(String key) { 316 String[] names = null; 317 String cacheKey = CALENDAR_NAMES + key; 318 319 removeEmptyReferences(); 320 ResourceReference data = cache.get(cacheKey); 321 322 if (data == null || ((names = (String[]) data.get()) == null)) { 323 ResourceBundle rb = localeData.getDateFormatData(locale); 324 if (rb.containsKey(key)) { 325 names = rb.getStringArray(key); 326 cache.put(cacheKey, 327 new ResourceReference(cacheKey, (Object) names, referenceQueue)); 328 } 329 } 330 331 return names; 332 } 333 334 public String getDateTimePattern(int timeStyle, int dateStyle, Calendar cal) { 335 if (cal == null) { 336 cal = Calendar.getInstance(locale); 337 } 338 return getDateTimePattern(null, timeStyle, dateStyle, cal.getCalendarType()); 339 } 340 341 /** 342 * Returns a date-time format pattern 343 * @param timeStyle style of time; one of FULL, LONG, MEDIUM, SHORT in DateFormat, 344 * or -1 if not required 345 * @param dateStyle style of time; one of FULL, LONG, MEDIUM, SHORT in DateFormat, 346 * or -1 if not required 347 * @param calType the calendar type for the pattern 348 * @return the pattern string 349 */ 350 public String getCldrDateTimePattern(int timeStyle, int dateStyle, String calType) { 351 calType = CalendarDataUtility.normalizeCalendarType(calType); 352 String pattern; 353 pattern = getDateTimePattern("cldr.", timeStyle, dateStyle, calType); 354 if (pattern == null) { 355 pattern = getDateTimePattern(null, timeStyle, dateStyle, calType); 356 } 357 return pattern; 358 } 359 360 private String getDateTimePattern(String prefix, int timeStyle, int dateStyle, String calType) { 361 String pattern; 362 String timePattern = null; 363 String datePattern = null; 364 365 if (timeStyle >= 0) { 366 if (prefix != null) { 367 timePattern = getDateTimePattern(prefix, "TimePatterns", timeStyle, calType); 368 } 369 if (timePattern == null) { 370 timePattern = getDateTimePattern(null, "TimePatterns", timeStyle, calType); 371 } 372 } 373 if (dateStyle >= 0) { 374 if (prefix != null) { 375 datePattern = getDateTimePattern(prefix, "DatePatterns", dateStyle, calType); 376 } 377 if (datePattern == null) { 378 datePattern = getDateTimePattern(null, "DatePatterns", dateStyle, calType); 379 } 380 } 381 if (timeStyle >= 0) { 382 if (dateStyle >= 0) { 383 String dateTimePattern = null; 384 if (prefix != null) { 385 dateTimePattern = getDateTimePattern(prefix, "DateTimePatterns", 0, calType); 386 } 387 if (dateTimePattern == null) { 388 dateTimePattern = getDateTimePattern(null, "DateTimePatterns", 0, calType); 389 } 390 switch (dateTimePattern) { 391 case "{1} {0}": 392 pattern = datePattern + " " + timePattern; 393 break; 394 case "{0} {1}": 395 pattern = timePattern + " " + datePattern; 396 break; 397 default: 398 pattern = MessageFormat.format(dateTimePattern, timePattern, datePattern); 399 break; 400 } 401 } else { 402 pattern = timePattern; 403 } 404 } else if (dateStyle >= 0) { 405 pattern = datePattern; 406 } else { 407 throw new IllegalArgumentException("No date or time style specified"); 408 } 409 return pattern; 417 418 if (data == null || ((numberPatterns = (String[]) data.get()) == null)) { 419 ResourceBundle resource = localeData.getNumberFormatData(locale); 420 numberPatterns = resource.getStringArray("NumberPatterns"); 421 cache.put(NUMBER_PATTERNS_CACHEKEY, 422 new ResourceReference(NUMBER_PATTERNS_CACHEKEY, (Object) numberPatterns, referenceQueue)); 423 } 424 425 return numberPatterns; 426 } 427 428 /** 429 * Returns the FormatData resource bundle of this LocaleResources. 430 * The FormatData should be used only for accessing extra 431 * resources required by JSR 310. 432 */ 433 public ResourceBundle getFormatData() { 434 return localeData.getDateFormatData(locale); 435 } 436 437 private String getDateTimePattern(String prefix, String key, int styleIndex, String calendarType) { 438 StringBuilder sb = new StringBuilder(); 439 if (prefix != null) { 440 sb.append(prefix); 441 } 442 if (!"gregory".equals(calendarType)) { 443 sb.append(calendarType).append('.'); 444 } 445 sb.append(key); 446 String resourceKey = sb.toString(); 447 String cacheKey = sb.insert(0, DATE_TIME_PATTERN).toString(); 448 449 removeEmptyReferences(); 450 ResourceReference data = cache.get(cacheKey); 451 Object value = NULLOBJECT; 452 453 if (data == null || ((value = data.get()) == null)) { 454 ResourceBundle r = localeData.getDateFormatData(locale); 455 if (r.containsKey(resourceKey)) { 456 value = r.getStringArray(resourceKey); 457 } else { 458 assert !resourceKey.equals(key); 459 if (r.containsKey(key)) { 460 value = r.getStringArray(key); 461 } 462 } 463 cache.put(cacheKey, 464 new ResourceReference(cacheKey, value, referenceQueue)); 465 } 466 if (value == NULLOBJECT) { 467 assert prefix != null; 468 return null; 469 } 470 return ((String[])value)[styleIndex]; 471 } 472 473 private static class ResourceReference extends SoftReference<Object> { 474 private final String cacheKey; 475 476 ResourceReference(String cacheKey, Object o, ReferenceQueue<Object> q) { 477 super(o, q); 478 this.cacheKey = cacheKey; 479 } 480 481 String getCacheKey() { 482 return cacheKey; 483 } 484 } 485 } |