src/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java

Print this page




  33 import java.util.spi.CalendarNameProvider;
  34 
  35 /**
  36  * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
  37  * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
  38  *
  39  * @author Masayoshi Okutsu
  40  * @author Naoto Sato
  41  */
  42 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
  43     private final LocaleProviderAdapter.Type type;
  44     private final Set<String> langtags;
  45 
  46     public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  47         this.type = type;
  48         this.langtags = langtags;
  49     }
  50 
  51     @Override
  52     public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {








  53         String name = null;
  54         String key = getResourceKey(calendarType, field, style);
  55         if (key != null) {
  56             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
  57             if (strings != null && strings.length > 0) {
  58                 if (field == DAY_OF_WEEK || field == YEAR) {
  59                     --value;
  60                 }
  61                 if (value < 0 || value >= strings.length) {
  62                     return null;
  63                 }
  64                 name = strings[value];
  65                 // If name is empty in standalone, try its `format' style.
  66                 if (name.length() == 0
  67                         && (style == SHORT_STANDALONE || style == LONG_STANDALONE
  68                             || style == NARROW_STANDALONE)) {
  69                     name = getDisplayName(calendarType, field, value,
  70                                           getBaseStyle(style),
  71                                           locale);
  72                 }
  73             }
  74         }
  75         return name;
  76     }
  77 
  78     private static int[] REST_OF_STYLES = {
  79         SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
  80         NARROW_FORMAT, NARROW_STANDALONE
  81     };

  82     @Override
  83     public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
  84         Map<String, Integer> names;
  85         if (style == ALL_STYLES) {
  86             names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale);
  87             for (int st : REST_OF_STYLES) {
  88                 names.putAll(getDisplayNamesImpl(calendarType, field, st, locale));
  89             }
  90         } else {
  91             // specific style
  92             names = getDisplayNamesImpl(calendarType, field, style, locale);


  93         }





  94         return names.isEmpty() ? null : names;
  95     }
  96 
  97     private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,
  98                                                      int style, Locale locale) {
  99         String key = getResourceKey(calendarType, field, style);
 100         Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);
 101         if (key != null) {
 102             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
 103             if (strings != null) {
 104                 if (!hasDuplicates(strings)) {
 105                     if (field == YEAR) {
 106                         if (strings.length > 0) {
 107                             map.put(strings[0], 1);
 108                         }
 109                     } else {
 110                         int base = (field == DAY_OF_WEEK) ? 1 : 0;
 111                         for (int i = 0; i < strings.length; i++) {
 112                             String name = strings[i];
 113                             // Ignore any empty string (some standalone month names
 114                             // are not defined)
 115                             if (name.length() == 0) {
 116                                 continue;
 117                             }
 118                             map.put(name, base + i);
 119                         }


 187     @Override
 188     public Set<String> getAvailableLanguageTags() {
 189         return langtags;
 190     }
 191 
 192     private boolean hasDuplicates(String[] strings) {
 193         int len = strings.length;
 194         for (int i = 0; i < len - 1; i++) {
 195             String a = strings[i];
 196             if (a != null) {
 197                 for (int j = i + 1; j < len; j++) {
 198                     if (a.equals(strings[j]))  {
 199                         return true;
 200                     }
 201                 }
 202             }
 203         }
 204         return false;
 205     }
 206 
 207     private String getResourceKey(String type, int field, int style) {
 208         int baseStyle = getBaseStyle(style);
 209         boolean isStandalone = (style != baseStyle);
 210 
 211         if ("gregory".equals(type)) {
 212             type = null;
 213         }
 214         boolean isNarrow = (baseStyle == NARROW_FORMAT);
 215         StringBuilder key = new StringBuilder();




 216         switch (field) {
 217         case ERA:
 218             if (type != null) {
 219                 key.append(type).append('.');
 220             }
 221             if (isNarrow) {
 222                 key.append("narrow.");
 223             } else {
 224                 // JRE and CLDR use different resource key conventions
 225                 // due to historical reasons. (JRE DateFormatSymbols.getEras returns
 226                 // abbreviations while other getShort*() return abbreviations.)
 227                 if (this.type == LocaleProviderAdapter.Type.JRE) {





 228                     if (baseStyle == SHORT) {
 229                         key.append("short.");
 230                     }
 231                 } else { // CLDR
 232                     if (baseStyle == LONG) {
 233                         key.append("long.");
 234                     }
 235                 }
 236             }
 237             key.append("Eras");
 238             break;
 239 
 240         case YEAR:
 241             if (!isNarrow) {
 242                 key.append(type).append(".FirstYear");
 243             }
 244             break;
 245 
 246         case MONTH:
 247             if ("islamic".equals(type)) {




  33 import java.util.spi.CalendarNameProvider;
  34 
  35 /**
  36  * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
  37  * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
  38  *
  39  * @author Masayoshi Okutsu
  40  * @author Naoto Sato
  41  */
  42 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
  43     private final LocaleProviderAdapter.Type type;
  44     private final Set<String> langtags;
  45 
  46     public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  47         this.type = type;
  48         this.langtags = langtags;
  49     }
  50 
  51     @Override
  52     public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
  53         return getDisplayNameImpl(calendarType, field, value, style, locale, false);
  54     }
  55 
  56     public String getCldrDisplayName(String calendarType, int field, int value, int style, Locale locale) {
  57         return getDisplayNameImpl(calendarType, field, value, style, locale, true);
  58     }
  59 
  60     public String getDisplayNameImpl(String calendarType, int field, int value, int style, Locale locale, boolean cldr) {
  61         String name = null;
  62         String key = getResourceKey(calendarType, field, style, cldr);
  63         if (key != null) {
  64             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
  65             if (strings != null && strings.length > 0) {
  66                 if (field == DAY_OF_WEEK || field == YEAR) {
  67                     --value;
  68                 }
  69                 if (value < 0 || value >= strings.length) {
  70                     return null;
  71                 }
  72                 name = strings[value];
  73                 // If name is empty in standalone, try its `format' style.
  74                 if (name.length() == 0
  75                         && (style == SHORT_STANDALONE || style == LONG_STANDALONE
  76                             || style == NARROW_STANDALONE)) {
  77                     name = getDisplayName(calendarType, field, value,
  78                                           getBaseStyle(style),
  79                                           locale);
  80                 }
  81             }
  82         }
  83         return name;
  84     }
  85 
  86     private static int[] REST_OF_STYLES = {
  87         SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
  88         NARROW_FORMAT, NARROW_STANDALONE
  89     };
  90 
  91     @Override
  92     public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
  93         Map<String, Integer> names;
  94         if (style == ALL_STYLES) {
  95             names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale, false);
  96             for (int st : REST_OF_STYLES) {
  97                 names.putAll(getDisplayNamesImpl(calendarType, field, st, locale, false));
  98             }
  99         } else {
 100             // specific style
 101             names = getDisplayNamesImpl(calendarType, field, style, locale, false);
 102         }
 103         return names.isEmpty() ? null : names;
 104     }
 105 
 106     // NOTE: This method should be used ONLY BY JSR 310 classes.
 107     public Map<String, Integer> getCldrDisplayNames(String calendarType, int field, int style, Locale locale) {
 108         Map<String, Integer> names;
 109         names = getDisplayNamesImpl(calendarType, field, style, locale, true);
 110         return names.isEmpty() ? null : names;
 111     }
 112 
 113     private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,
 114                                                      int style, Locale locale, boolean cldr) {
 115         String key = getResourceKey(calendarType, field, style, cldr);
 116         Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);
 117         if (key != null) {
 118             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
 119             if (strings != null) {
 120                 if (!hasDuplicates(strings)) {
 121                     if (field == YEAR) {
 122                         if (strings.length > 0) {
 123                             map.put(strings[0], 1);
 124                         }
 125                     } else {
 126                         int base = (field == DAY_OF_WEEK) ? 1 : 0;
 127                         for (int i = 0; i < strings.length; i++) {
 128                             String name = strings[i];
 129                             // Ignore any empty string (some standalone month names
 130                             // are not defined)
 131                             if (name.length() == 0) {
 132                                 continue;
 133                             }
 134                             map.put(name, base + i);
 135                         }


 203     @Override
 204     public Set<String> getAvailableLanguageTags() {
 205         return langtags;
 206     }
 207 
 208     private boolean hasDuplicates(String[] strings) {
 209         int len = strings.length;
 210         for (int i = 0; i < len - 1; i++) {
 211             String a = strings[i];
 212             if (a != null) {
 213                 for (int j = i + 1; j < len; j++) {
 214                     if (a.equals(strings[j]))  {
 215                         return true;
 216                     }
 217                 }
 218             }
 219         }
 220         return false;
 221     }
 222 
 223     private String getResourceKey(String type, int field, int style, boolean cldr) {
 224         int baseStyle = getBaseStyle(style);
 225         boolean isStandalone = (style != baseStyle);
 226 
 227         if ("gregory".equals(type)) {
 228             type = null;
 229         }
 230         boolean isNarrow = (baseStyle == NARROW_FORMAT);
 231         StringBuilder key = new StringBuilder();
 232         // If cldr is true, use prefix "cldr.".
 233         if (cldr) {
 234             key.append("cldr.");
 235         }
 236         switch (field) {
 237         case ERA:
 238             if (type != null) {
 239                 key.append(type).append('.');
 240             }
 241             if (isNarrow) {
 242                 key.append("narrow.");
 243             } else {
 244                 // JRE and CLDR use different resource key conventions
 245                 // due to historical reasons. (JRE DateFormatSymbols.getEras returns
 246                 // abbreviations while other getShort*() return abbreviations.)
 247                 if (this.type == LocaleProviderAdapter.Type.JRE) {
 248                     if (cldr) {
 249                         if (baseStyle == LONG) {
 250                             key.append("long.");
 251                         }
 252                     }
 253                     if (baseStyle == SHORT) {
 254                         key.append("short.");
 255                     }
 256                 } else { // CLDR
 257                     if (baseStyle == LONG) {
 258                         key.append("long.");
 259                     }
 260                 }
 261             }
 262             key.append("Eras");
 263             break;
 264 
 265         case YEAR:
 266             if (!isNarrow) {
 267                 key.append(type).append(".FirstYear");
 268             }
 269             break;
 270 
 271         case MONTH:
 272             if ("islamic".equals(type)) {