25 package sun.util.locale.provider; 26 27 import static java.util.Calendar.*; 28 import java.util.Comparator; 29 import java.util.Locale; 30 import java.util.Map; 31 import java.util.Set; 32 import java.util.TreeMap; 33 import java.util.spi.CalendarNameProvider; 34 import sun.util.calendar.CalendarSystem; 35 import sun.util.calendar.Era; 36 37 /** 38 * Concrete implementation of the {@link java.util.spi.CalendarNameProvider 39 * CalendarNameProvider} class for the JRE LocaleProviderAdapter. 40 * 41 * @author Masayoshi Okutsu 42 * @author Naoto Sato 43 */ 44 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags { 45 private final LocaleProviderAdapter.Type type; 46 private final Set<String> langtags; 47 48 public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) { 49 this.type = type; 50 this.langtags = langtags; 51 } 52 53 @Override 54 public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) { 55 return getDisplayNameImpl(calendarType, field, value, style, locale, false); 56 } 57 58 public String getJavaTimeDisplayName(String calendarType, int field, int value, int style, Locale locale) { 59 return getDisplayNameImpl(calendarType, field, value, style, locale, true); 60 } 61 62 public String getDisplayNameImpl(String calendarType, int field, int value, int style, Locale locale, boolean javatime) { 63 String name = null; 64 String key = getResourceKey(calendarType, field, style, javatime); 65 if (key != null) { 66 LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale); 231 calendarType = locale.getUnicodeLocaleType("ca"); 232 locale = locale.stripExtensions(); 233 } 234 235 if (calendarType != null) { 236 switch (calendarType) { 237 case "buddhist": 238 case "japanese": 239 case "gregory": 240 case "islamic": 241 case "roc": 242 break; 243 default: 244 // Unknown calendar type 245 return false; 246 } 247 } 248 if (langtags.contains(locale.toLanguageTag())) { 249 return true; 250 } 251 if (type == LocaleProviderAdapter.Type.JRE) { 252 String oldname = locale.toString().replace('_', '-'); 253 return langtags.contains(oldname); 254 } 255 return false; 256 } 257 258 @Override 259 public Set<String> getAvailableLanguageTags() { 260 return langtags; 261 } 262 263 private boolean hasDuplicates(String[] strings) { 264 int len = strings.length; 265 for (int i = 0; i < len - 1; i++) { 266 String a = strings[i]; 267 if (a != null) { 268 for (int j = i + 1; j < len; j++) { 269 if (a.equals(strings[j])) { 270 return true; 271 } 272 } 273 } 274 } 275 return false; 276 } | 25 package sun.util.locale.provider; 26 27 import static java.util.Calendar.*; 28 import java.util.Comparator; 29 import java.util.Locale; 30 import java.util.Map; 31 import java.util.Set; 32 import java.util.TreeMap; 33 import java.util.spi.CalendarNameProvider; 34 import sun.util.calendar.CalendarSystem; 35 import sun.util.calendar.Era; 36 37 /** 38 * Concrete implementation of the {@link java.util.spi.CalendarNameProvider 39 * CalendarNameProvider} class for the JRE LocaleProviderAdapter. 40 * 41 * @author Masayoshi Okutsu 42 * @author Naoto Sato 43 */ 44 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags { 45 protected final LocaleProviderAdapter.Type type; 46 protected final Set<String> langtags; 47 48 public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) { 49 this.type = type; 50 this.langtags = langtags; 51 } 52 53 @Override 54 public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) { 55 return getDisplayNameImpl(calendarType, field, value, style, locale, false); 56 } 57 58 public String getJavaTimeDisplayName(String calendarType, int field, int value, int style, Locale locale) { 59 return getDisplayNameImpl(calendarType, field, value, style, locale, true); 60 } 61 62 public String getDisplayNameImpl(String calendarType, int field, int value, int style, Locale locale, boolean javatime) { 63 String name = null; 64 String key = getResourceKey(calendarType, field, style, javatime); 65 if (key != null) { 66 LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale); 231 calendarType = locale.getUnicodeLocaleType("ca"); 232 locale = locale.stripExtensions(); 233 } 234 235 if (calendarType != null) { 236 switch (calendarType) { 237 case "buddhist": 238 case "japanese": 239 case "gregory": 240 case "islamic": 241 case "roc": 242 break; 243 default: 244 // Unknown calendar type 245 return false; 246 } 247 } 248 if (langtags.contains(locale.toLanguageTag())) { 249 return true; 250 } 251 String oldname = locale.toString().replace('_', '-'); 252 return langtags.contains(oldname); 253 } 254 255 @Override 256 public Set<String> getAvailableLanguageTags() { 257 return langtags; 258 } 259 260 private boolean hasDuplicates(String[] strings) { 261 int len = strings.length; 262 for (int i = 0; i < len - 1; i++) { 263 String a = strings[i]; 264 if (a != null) { 265 for (int j = i + 1; j < len; j++) { 266 if (a.equals(strings[j])) { 267 return true; 268 } 269 } 270 } 271 } 272 return false; 273 } |