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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.locale.provider;
  27 


  28 import java.util.Locale;
  29 import java.util.Map;
  30 import java.util.spi.CalendarDataProvider;

  31 
  32 /**
  33  * {@code CalendarDataUtility} is a utility class for calling the
  34  * {@link CalendarDataProvider} methods.
  35  *
  36  * @author Masayoshi Okutsu
  37  * @author Naoto Sato
  38  */
  39 public class CalendarDataUtility {
  40     public final static String FIRST_DAY_OF_WEEK = "firstDayOfWeek";
  41     public final static String MINIMAL_DAYS_IN_FIRST_WEEK = "minimalDaysInFirstWeek";
  42 
  43     // No instantiation
  44     private CalendarDataUtility() {
  45     }
  46 
  47     public static String retrieveFieldValueName(String id, int field, int value, int style, Locale locale) {








  48         LocaleServiceProviderPool pool =
  49                 LocaleServiceProviderPool.getPool(CalendarDataProvider.class);








  50         return pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, id,
  51                                        field, value, style);
  52     }
  53 
  54     public static Map<String, Integer> retrieveFieldValueNames(String id, int field, int style, Locale locale) {
  55         LocaleServiceProviderPool pool =
  56             LocaleServiceProviderPool.getPool(CalendarDataProvider.class);
  57         return pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale, id, field, style);
  58     }
  59 
  60     /**
  61      * Obtains a localized field value string from a CalendarDataProvider
  62      * implementation.
  63      */
  64     private static class CalendarFieldValueNameGetter
  65         implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarDataProvider,
  66                                                                    String> {
  67         private static final CalendarFieldValueNameGetter INSTANCE =
  68             new CalendarFieldValueNameGetter();
  69 
  70         @Override
  71         public String getObject(CalendarDataProvider calendarDataProvider,
  72                                 Locale locale,
  73                                 String requestID, // calendarType
  74                                 Object... params) {
  75             assert params.length == 3;
  76             int field = (int) params[0];
  77             int value = (int) params[1];
  78             int style = (int) params[2];
  79             return calendarDataProvider.getDisplayName(requestID, field, value, style, locale);
  80         }
  81     }
  82 
  83     /**
  84      * Obtains a localized field-value pairs from a CalendarDataProvider
  85      * implementation.
  86      */
  87     private static class CalendarFieldValueNamesMapGetter
  88         implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarDataProvider,
  89                                                                    Map<String, Integer>> {
  90         private static final CalendarFieldValueNamesMapGetter INSTANCE =
  91             new CalendarFieldValueNamesMapGetter();
  92 
  93         @Override
  94         public Map<String, Integer> getObject(CalendarDataProvider calendarDataProvider,
  95                                               Locale locale,
  96                                               String requestID, // calendarType
  97                                               Object... params) {
  98             assert params.length == 2;
  99             int field = (int) params[0];
 100             int style = (int) params[1];
 101             return calendarDataProvider.getDisplayNames(requestID, field, style, locale);



























 102         }
 103     }
 104 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.locale.provider;
  27 
  28 import java.util.Calendar;
  29 import static java.util.Calendar.*;
  30 import java.util.Locale;
  31 import java.util.Map;
  32 import java.util.spi.CalendarDataProvider;
  33 import java.util.spi.CalendarNameProvider;
  34 
  35 /**
  36  * {@code CalendarDataUtility} is a utility class for calling the
  37  * {@link CalendarDataProvider} methods.
  38  *
  39  * @author Masayoshi Okutsu
  40  * @author Naoto Sato
  41  */
  42 public class CalendarDataUtility {
  43     public final static String FIRST_DAY_OF_WEEK = "firstDayOfWeek";
  44     public final static String MINIMAL_DAYS_IN_FIRST_WEEK = "minimalDaysInFirstWeek";
  45 
  46     // No instantiation
  47     private CalendarDataUtility() {
  48     }
  49 
  50     public static int retrieveFirstDayOfWeek(Locale locale) {
  51         LocaleServiceProviderPool pool =
  52                 LocaleServiceProviderPool.getPool(CalendarDataProvider.class);
  53         Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE,
  54                                                 locale, FIRST_DAY_OF_WEEK);
  55         return (value != null && (value >= SUNDAY && value <= SATURDAY)) ? value : SUNDAY;
  56     }
  57 
  58     public static int retrieveMinimalDaysInFirstWeek(Locale locale) {
  59         LocaleServiceProviderPool pool =
  60                 LocaleServiceProviderPool.getPool(CalendarDataProvider.class);
  61         Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE,
  62                                                 locale, MINIMAL_DAYS_IN_FIRST_WEEK);
  63         return (value != null && (value >= 1 && value <= 7)) ? value : 1;
  64     }
  65 
  66     public static String retrieveFieldValueName(String id, int field, int value, int style, Locale locale) {
  67         LocaleServiceProviderPool pool =
  68                 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
  69         return pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, id,
  70                                        field, value, style);
  71     }
  72 
  73     public static Map<String, Integer> retrieveFieldValueNames(String id, int field, int style, Locale locale) {
  74         LocaleServiceProviderPool pool =
  75             LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
  76         return pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale, id, field, style);
  77     }
  78 
  79     /**
  80      * Obtains a localized field value string from a CalendarDataProvider
  81      * implementation.
  82      */
  83     private static class CalendarFieldValueNameGetter
  84         implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarNameProvider,
  85                                                                    String> {
  86         private static final CalendarFieldValueNameGetter INSTANCE =
  87             new CalendarFieldValueNameGetter();
  88 
  89         @Override
  90         public String getObject(CalendarNameProvider calendarNameProvider,
  91                                 Locale locale,
  92                                 String requestID, // calendarType
  93                                 Object... params) {
  94             assert params.length == 3;
  95             int field = (int) params[0];
  96             int value = (int) params[1];
  97             int style = (int) params[2];
  98             return calendarNameProvider.getDisplayName(requestID, field, value, style, locale);
  99         }
 100     }
 101 
 102     /**
 103      * Obtains a localized field-value pairs from a CalendarDataProvider
 104      * implementation.
 105      */
 106     private static class CalendarFieldValueNamesMapGetter
 107         implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarNameProvider,
 108                                                                    Map<String, Integer>> {
 109         private static final CalendarFieldValueNamesMapGetter INSTANCE =
 110             new CalendarFieldValueNamesMapGetter();
 111 
 112         @Override
 113         public Map<String, Integer> getObject(CalendarNameProvider calendarNameProvider,
 114                                               Locale locale,
 115                                               String requestID, // calendarType
 116                                               Object... params) {
 117             assert params.length == 2;
 118             int field = (int) params[0];
 119             int style = (int) params[1];
 120             return calendarNameProvider.getDisplayNames(requestID, field, style, locale);
 121         }
 122     }
 123 
 124      private static class CalendarWeekParameterGetter
 125         implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarDataProvider,
 126                                                                    Integer> {
 127         private static final CalendarWeekParameterGetter INSTANCE =
 128             new CalendarWeekParameterGetter();
 129 
 130         @Override
 131         public Integer getObject(CalendarDataProvider calendarDataProvider,
 132                                  Locale locale,
 133                                  String requestID,    // resource key
 134                                  Object... params) {
 135             assert params.length == 0;
 136             int value;
 137             switch (requestID) {
 138             case FIRST_DAY_OF_WEEK:
 139                 value = calendarDataProvider.getFirstDayOfWeek(locale);
 140                 break;
 141             case MINIMAL_DAYS_IN_FIRST_WEEK:
 142                 value = calendarDataProvider.getMinimalDaysInFirstWeek(locale);
 143                 break;
 144             default:
 145                 throw new InternalError("invalid requestID: " + requestID);
 146             }
 147             return (value != 0) ? value : null;
 148         }
 149     }
 150 }