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 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 }