1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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 static java.util.Calendar.*;
  29 import java.util.Locale;
  30 import java.util.Map;
  31 import java.util.spi.CalendarDataProvider;
  32 import java.util.spi.CalendarNameProvider;
  33 
  34 /**
  35  * {@code CalendarDataUtility} is a utility class for calling the
  36  * {@link CalendarDataProvider} methods.
  37  *
  38  * @author Masayoshi Okutsu
  39  * @author Naoto Sato
  40  */
  41 public class CalendarDataUtility {
  42     public final static String FIRST_DAY_OF_WEEK = "firstDayOfWeek";
  43     public final static String MINIMAL_DAYS_IN_FIRST_WEEK = "minimalDaysInFirstWeek";
  44 
  45     // No instantiation
  46     private CalendarDataUtility() {
  47     }
  48 
  49     public static int retrieveFirstDayOfWeek(Locale locale) {
  50         LocaleServiceProviderPool pool =
  51                 LocaleServiceProviderPool.getPool(CalendarDataProvider.class);
  52         Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE,
  53                                                 locale, FIRST_DAY_OF_WEEK);
  54         return (value != null && (value >= SUNDAY && value <= SATURDAY)) ? value : SUNDAY;
  55     }
  56 
  57     public static int retrieveMinimalDaysInFirstWeek(Locale locale) {
  58         LocaleServiceProviderPool pool =
  59                 LocaleServiceProviderPool.getPool(CalendarDataProvider.class);
  60         Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE,
  61                                                 locale, MINIMAL_DAYS_IN_FIRST_WEEK);
  62         return (value != null && (value >= 1 && value <= 7)) ? value : 1;
  63     }
  64 
  65     public static String retrieveFieldValueName(String id, int field, int value, int style, Locale locale) {
  66         LocaleServiceProviderPool pool =
  67                 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
  68         return pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id),
  69                                        field, value, style);
  70     }
  71 
  72     public static Map<String, Integer> retrieveFieldValueNames(String id, int field, int style, Locale locale) {
  73         LocaleServiceProviderPool pool =
  74             LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
  75         return pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale,
  76                                        normalizeCalendarType(id), field, style);
  77     }
  78 
  79     private static String normalizeCalendarType(String requestID) {
  80         String type;
  81         if (requestID.equals("gregorian") || requestID.equals("iso8601")) {
  82             type = "gregory";
  83         } else if (requestID.startsWith("islamic")) {
  84             type = "islamic";
  85         } else {
  86             type = requestID;
  87         }
  88         return type;
  89     }
  90 
  91     /**
  92      * Obtains a localized field value string from a CalendarDataProvider
  93      * implementation.
  94      */
  95     private static class CalendarFieldValueNameGetter
  96         implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarNameProvider,
  97                                                                    String> {
  98         private static final CalendarFieldValueNameGetter INSTANCE =
  99             new CalendarFieldValueNameGetter();
 100 
 101         @Override
 102         public String getObject(CalendarNameProvider calendarNameProvider,
 103                                 Locale locale,
 104                                 String requestID, // calendarType
 105                                 Object... params) {
 106             assert params.length == 3;
 107             int field = (int) params[0];
 108             int value = (int) params[1];
 109             int style = (int) params[2];
 110             return calendarNameProvider.getDisplayName(requestID, field, value, style, locale);
 111         }
 112     }
 113 
 114     /**
 115      * Obtains a localized field-value pairs from a CalendarDataProvider
 116      * implementation.
 117      */
 118     private static class CalendarFieldValueNamesMapGetter
 119         implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarNameProvider,
 120                                                                    Map<String, Integer>> {
 121         private static final CalendarFieldValueNamesMapGetter INSTANCE =
 122             new CalendarFieldValueNamesMapGetter();
 123 
 124         @Override
 125         public Map<String, Integer> getObject(CalendarNameProvider calendarNameProvider,
 126                                               Locale locale,
 127                                               String requestID, // calendarType
 128                                               Object... params) {
 129             assert params.length == 2;
 130             int field = (int) params[0];
 131             int style = (int) params[1];
 132             return calendarNameProvider.getDisplayNames(requestID, field, style, locale);
 133         }
 134     }
 135 
 136      private static class CalendarWeekParameterGetter
 137         implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarDataProvider,
 138                                                                    Integer> {
 139         private static final CalendarWeekParameterGetter INSTANCE =
 140             new CalendarWeekParameterGetter();
 141 
 142         @Override
 143         public Integer getObject(CalendarDataProvider calendarDataProvider,
 144                                  Locale locale,
 145                                  String requestID,    // resource key
 146                                  Object... params) {
 147             assert params.length == 0;
 148             int value;
 149             switch (requestID) {
 150             case FIRST_DAY_OF_WEEK:
 151                 value = calendarDataProvider.getFirstDayOfWeek(locale);
 152                 break;
 153             case MINIMAL_DAYS_IN_FIRST_WEEK:
 154                 value = calendarDataProvider.getMinimalDaysInFirstWeek(locale);
 155                 break;
 156             default:
 157                 throw new InternalError("invalid requestID: " + requestID);
 158             }
 159             return (value != 0) ? value : null;
 160         }
 161     }
 162 }